forked from AlexWoo/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ngx_netcall.c
205 lines (159 loc) · 4.33 KB
/
ngx_netcall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) AlexWoo(Wu Jie) [email protected]
*/
#include "ngx_netcall.h"
#include "ngx_poold.h"
// cleanup only be called when connect failed(exclusive timeout)
static void
ngx_netcall_cleanup(void *data)
{
ngx_netcall_ctx_t *nctx;
nctx = data;
if (nctx->ev.timer_set) {
ngx_del_timer(&nctx->ev);
}
if (nctx->ev.posted) {
ngx_delete_posted_event(&nctx->ev);
}
if (nctx->hcr) {
ngx_http_client_detach(nctx->hcr);
nctx->handler(nctx, NGX_ERROR);
nctx->hcr = NULL;
}
}
// netcall timeout
static void
ngx_netcall_timeout(ngx_event_t *ev)
{
ngx_netcall_ctx_t *nctx;
nctx = ev->data;
if (nctx->ev.timer_set) {
ngx_del_timer(&nctx->ev);
}
if (nctx->ev.posted) {
ngx_delete_posted_event(&nctx->ev);
}
if (nctx->hcr) {
ngx_http_client_detach(nctx->hcr);
nctx->handler(nctx, NGX_ERROR);
nctx->hcr = NULL;
}
}
static void
ngx_netcall_handler(void *data, ngx_http_request_t *hcr)
{
ngx_netcall_ctx_t *nctx;
ngx_int_t code;
nctx = data;
if (nctx->ev.timer_set) {
ngx_del_timer(&nctx->ev);
}
if (nctx->ev.posted) {
ngx_delete_posted_event(&nctx->ev);
}
code = ngx_http_client_status_code(hcr);
if (nctx->hcr) {
ngx_http_client_detach(nctx->hcr);
nctx->handler(nctx, code);
nctx->hcr = NULL;
}
}
static void
ngx_netcall_destroy_handler(ngx_event_t *ev)
{
ngx_netcall_ctx_t *nctx;
nctx = ev->data;
NGX_DESTROY_POOL(nctx->pool);
}
ngx_netcall_ctx_t *
ngx_netcall_create_ctx(ngx_uint_t type, ngx_str_t *groupid, ngx_uint_t stage,
ngx_msec_t timeout, ngx_msec_t update, ngx_uint_t idx)
{
ngx_netcall_ctx_t *ctx;
ngx_pool_t *pool;
pool = NGX_CREATE_POOL(4096, ngx_cycle->log);
if (pool == NULL) {
return NULL;
}
ctx = ngx_pcalloc(pool, sizeof(ngx_netcall_ctx_t));
if (ctx == NULL) {
NGX_DESTROY_POOL(pool);
return NULL;
}
ctx->url.data = ngx_pcalloc(pool, NGX_NETCALL_MAX_URL_LEN);
if (ctx->url.data == NULL) {
NGX_DESTROY_POOL(pool);
return NULL;
}
ctx->pool = pool;
ctx->idx = idx;
ctx->type = type;
ctx->groupid.len = groupid->len;
ctx->groupid.data = ngx_pcalloc(pool, ctx->groupid.len);
if (ctx->groupid.data == NULL) {
NGX_DESTROY_POOL(pool);
return NULL;
}
ngx_memcpy(ctx->groupid.data, groupid->data, groupid->len);
ctx->ev.log = ngx_cycle->log;
ctx->ev.data = ctx;
ctx->stage = stage;
ctx->timeout = timeout;
ctx->update = update;
return ctx;
}
void
ngx_netcall_create(ngx_netcall_ctx_t *nctx, ngx_log_t *log)
{
ngx_http_request_t *hcr;
ngx_http_cleanup_t *cln;
hcr = ngx_http_client_get(log, &nctx->url, NULL, nctx);
if (hcr == NULL) {
return;
}
ngx_http_client_set_read_handler(hcr, ngx_netcall_handler);
cln = ngx_http_client_cleanup_add(hcr, 0);
if (cln == NULL) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"netcall create add cleanup failed");
return;
}
cln->handler = ngx_netcall_cleanup;
cln->data = nctx;
// detach old http client request
if (nctx->hcr) {
ngx_http_client_detach(nctx->hcr);
}
nctx->hcr = hcr;
nctx->ev.log = log;
nctx->ev.handler = ngx_netcall_timeout;
ngx_add_timer(&nctx->ev, nctx->timeout);
}
void
ngx_netcall_destroy(ngx_netcall_ctx_t *nctx)
{
if (nctx->ev.timer_set) {
ngx_del_timer(&nctx->ev);
}
if (nctx->ev.posted) {
ngx_delete_posted_event(&nctx->ev);
}
if (nctx->hcr) { // use detach will keep client connection alive
ngx_http_client_detach(nctx->hcr);
nctx->hcr = NULL;
}
// destroy may called in nctx->handler
// destroy pool may cause memory error
// so we destroy nctx pool asynchronous
nctx->ev.handler = ngx_netcall_destroy_handler;
// reset ev log, use rtmp session log may be destroy
nctx->ev.log = ngx_cycle->log;
ngx_post_event(&nctx->ev, &ngx_posted_events);
}
ngx_str_t *
ngx_netcall_header(ngx_netcall_ctx_t *nctx, ngx_str_t *key)
{
ngx_http_request_t *hcr;
hcr = nctx->hcr;
return ngx_http_client_header_in(hcr, key);
}