-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathngx_event_timer_module.c
More file actions
271 lines (197 loc) · 6.31 KB
/
ngx_event_timer_module.c
File metadata and controls
271 lines (197 loc) · 6.31 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (C) AlexWoo(Wu Jie) wj19840501@gmail.com
*/
#include "ngx_event_timer_module.h"
static ngx_int_t ngx_event_timer_process_init(ngx_cycle_t *cycle);
static void *ngx_event_timer_create_conf(ngx_cycle_t *cycle);
static char *ngx_event_timer_init_conf(ngx_cycle_t *cycle, void *conf);
typedef struct {
ngx_uint_t timerid;
ngx_event_t event;
ngx_timer_handler_pt handler;
void *data;
} ngx_event_timer_ctx_t;
typedef struct {
ngx_uint_t timer_n;
ngx_event_timer_ctx_t *timer_ctx; /* array to store timers */
ngx_event_timer_ctx_t *free_timers; /* timer unused */
ngx_uint_t free_timer_n;
} ngx_event_timer_conf_t;
static ngx_str_t event_timer_name = ngx_string("event_timer");
static ngx_command_t ngx_event_timer_commands[] = {
{ ngx_string("worker_timers"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_event_timer_conf_t, timer_n),
NULL },
ngx_null_command
};
ngx_event_module_t ngx_event_timer_module_ctx = {
&event_timer_name,
ngx_event_timer_create_conf, /* create configuration */
ngx_event_timer_init_conf, /* init configuration */
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
/* this module use ngx_cycle->log */
ngx_module_t ngx_event_timer_module = {
NGX_MODULE_V1,
&ngx_event_timer_module_ctx, /* module context */
ngx_event_timer_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_event_timer_process_init, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void *
ngx_event_timer_create_conf(ngx_cycle_t *cycle)
{
ngx_event_timer_conf_t *conf;
conf = ngx_pcalloc(cycle->pool, sizeof(ngx_event_timer_conf_t));
if (conf == NULL) {
return NULL;
}
conf->timer_n = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_event_timer_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_event_timer_conf_t *etcf = conf;
ngx_conf_init_uint_value(etcf->timer_n, 1024);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_event_timer_process_init(ngx_cycle_t *cycle)
{
ngx_event_timer_conf_t *etcf;
ngx_event_timer_ctx_t *t, *next;
ngx_uint_t i;
etcf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_timer_module);
if (etcf->timer_n == 0) {
return NGX_OK;
}
etcf->timer_ctx =
ngx_alloc(sizeof(ngx_event_timer_ctx_t) * etcf->timer_n, cycle->log);
if (etcf->timer_ctx == NULL) {
return NGX_ERROR;
}
t = etcf->timer_ctx;
i = etcf->timer_n;
next = NULL;
do {
--i;
t[i].timerid = i;
t[i].event.timer_set = 0;
t[i].event.log = NULL;
t[i].handler = NULL;
t[i].data = next;
next = &t[i];
} while (i);
etcf->free_timers = next;
etcf->free_timer_n = etcf->timer_n;
return NGX_OK;
}
static ngx_event_timer_ctx_t *
ngx_event_timer_get_timer()
{
ngx_event_timer_conf_t *etcf;
ngx_event_timer_ctx_t *free;
etcf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_timer_module);
free = etcf->free_timers;
if (etcf->free_timer_n == 0) {
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
"nginx event timer module, no free timer");
return NULL;
}
etcf->free_timers = free->data;
--etcf->free_timer_n;
return free;
}
static void
ngx_event_timer_free_timer(ngx_event_timer_ctx_t *ctx)
{
ngx_event_timer_conf_t *etcf;
etcf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_timer_module);
ctx->data = etcf->free_timers;
etcf->free_timers = ctx;
++etcf->free_timer_n;
}
static void
ngx_event_timer_event_handler(ngx_event_t *e)
{
ngx_event_timer_ctx_t *ctx;
ctx = e->data;
if (ctx->handler) {
ctx->handler(ctx->data);
}
ngx_event_timer_free_timer(ctx);
}
ngx_int_t
ngx_event_timer_add_timer(ngx_msec_t tv, ngx_timer_handler_pt h, void *data)
{
ngx_event_timer_ctx_t *ctx;
if (h == NULL) {
return NGX_ERROR;
}
ctx = ngx_event_timer_get_timer();
if (ctx == NULL) {
return NGX_ERROR;
}
ctx->event.handler = ngx_event_timer_event_handler;
ctx->event.data = ctx;
ctx->handler = h;
ctx->data = data;
if (ctx->event.log == NULL) {
ctx->event.log = ngx_cycle->log;
}
ngx_event_add_timer(&ctx->event, tv);
return ctx->timerid;
}
void
ngx_event_timer_del_timer(ngx_uint_t timerid)
{
ngx_event_timer_ctx_t *ctx;
ngx_event_timer_conf_t *etcf;
etcf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_timer_module);
ctx = &etcf->timer_ctx[timerid];
if (!ctx->event.timer_set) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
"timer has already deleted");
return;
}
ngx_event_del_timer(&ctx->event);
ngx_event_timer_free_timer(ctx);
}
ngx_chain_t *
ngx_event_timer_state(ngx_http_request_t *r)
{
ngx_event_timer_conf_t *etcf;
ngx_chain_t *cl;
ngx_buf_t *b;
size_t len;
etcf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_timer_module);
len = sizeof("##########event timer state##########\n") - 1
+ sizeof("ngx_event_timer alloc: \n") - 1 + NGX_OFF_T_LEN
+ sizeof("ngx_event_timer free: \n") - 1 + NGX_OFF_T_LEN;
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NULL;
}
cl->next = NULL;
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NULL;
}
cl->buf = b;
b->last = ngx_snprintf(b->last, len,
"##########event timer state##########\n"
"ngx_event_timer alloc: %ui\nngx_event_timer free: %ui\n",
etcf->timer_n, etcf->free_timer_n);
return cl;
}