- Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_sleep.c
223 lines (166 loc) · 5.53 KB
/
ngx_http_lua_sleep.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndefDDEBUG
#defineDDEBUG 0
#endif
#include"ddebug.h"
#include"ngx_http_lua_util.h"
#include"ngx_http_lua_sleep.h"
#include"ngx_http_lua_contentby.h"
staticintngx_http_lua_ngx_sleep(lua_State*L);
staticvoidngx_http_lua_sleep_handler(ngx_event_t*ev);
staticvoidngx_http_lua_sleep_cleanup(void*data);
staticngx_int_tngx_http_lua_sleep_resume(ngx_http_request_t*r);
staticint
ngx_http_lua_ngx_sleep(lua_State*L)
{
intn;
ngx_int_tdelay; /* in msec */
ngx_http_request_t*r;
ngx_http_lua_ctx_t*ctx;
ngx_http_lua_co_ctx_t*coctx;
n=lua_gettop(L);
if (n!=1) {
returnluaL_error(L, "attempt to pass %d arguments, but accepted 1", n);
}
r=ngx_http_lua_get_req(L);
if (r==NULL) {
returnluaL_error(L, "no request found");
}
delay= (ngx_int_t) (luaL_checknumber(L, 1) *1000);
if (delay<0) {
returnluaL_error(L, "invalid sleep duration \"%d\"", delay);
}
ctx=ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx==NULL) {
returnluaL_error(L, "no request ctx found");
}
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_YIELDABLE);
coctx=ctx->cur_co_ctx;
if (coctx==NULL) {
returnluaL_error(L, "no co ctx found");
}
ngx_http_lua_cleanup_pending_operation(coctx);
coctx->cleanup=ngx_http_lua_sleep_cleanup;
coctx->data=r;
coctx->sleep.handler=ngx_http_lua_sleep_handler;
coctx->sleep.data=coctx;
coctx->sleep.log=r->connection->log;
if (delay==0) {
#ifdefHAVE_POSTED_DELAYED_EVENTS_PATCH
dd("posting 0 sec sleep event to head of delayed queue");
coctx->sleep.delayed=1;
ngx_post_event(&coctx->sleep, &ngx_posted_delayed_events);
#else
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "ngx.sleep(0)"
" called without delayed events patch, this will"
" hurt performance");
ngx_add_timer(&coctx->sleep, (ngx_msec_t) delay);
#endif
} else {
dd("adding timer with delay %lu ms, r:%.*s", (unsigned long) delay,
(int) r->uri.len, r->uri.data);
ngx_add_timer(&coctx->sleep, (ngx_msec_t) delay);
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua ready to sleep for %d ms", delay);
returnlua_yield(L, 0);
}
void
ngx_http_lua_sleep_handler(ngx_event_t*ev)
{
ngx_connection_t*c;
ngx_http_request_t*r;
ngx_http_lua_ctx_t*ctx;
ngx_http_log_ctx_t*log_ctx;
ngx_http_lua_co_ctx_t*coctx;
coctx=ev->data;
r=coctx->data;
c=r->connection;
ctx=ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx==NULL) {
return;
}
if (c->fd!= (ngx_socket_t) -1) { /* not a fake connection */
log_ctx=c->log->data;
log_ctx->current_request=r;
}
coctx->cleanup=NULL;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"lua sleep timer expired: \"%V?%V\"", &r->uri, &r->args);
ctx->cur_co_ctx=coctx;
if (ctx->entered_content_phase) {
(void) ngx_http_lua_sleep_resume(r);
} else {
ctx->resume_handler=ngx_http_lua_sleep_resume;
ngx_http_core_run_phases(r);
}
ngx_http_run_posted_requests(c);
}
void
ngx_http_lua_inject_sleep_api(lua_State*L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_sleep);
lua_setfield(L, -2, "sleep");
}
staticvoid
ngx_http_lua_sleep_cleanup(void*data)
{
ngx_http_lua_co_ctx_t*coctx=data;
if (coctx->sleep.timer_set) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua clean up the timer for pending ngx.sleep");
ngx_del_timer(&coctx->sleep);
}
#ifdefHAVE_POSTED_DELAYED_EVENTS_PATCH
#if (nginx_version >= 1007005)
if (coctx->sleep.posted) {
#else
if (coctx->sleep.prev) {
#endif
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua clean up the posted event for pending ngx.sleep");
/*
* We need the extra parentheses around the argument
* of ngx_delete_posted_event() just to work around macro issues in
* nginx cores older than 1.7.5 (exclusive).
*/
ngx_delete_posted_event((&coctx->sleep));
}
#endif
}
staticngx_int_t
ngx_http_lua_sleep_resume(ngx_http_request_t*r)
{
lua_State*vm;
ngx_connection_t*c;
ngx_int_trc;
ngx_uint_tnreqs;
ngx_http_lua_ctx_t*ctx;
ctx=ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx==NULL) {
returnNGX_ERROR;
}
ctx->resume_handler=ngx_http_lua_wev_handler;
c=r->connection;
vm=ngx_http_lua_get_lua_vm(r, ctx);
nreqs=c->requests;
rc=ngx_http_lua_run_thread(vm, r, ctx, 0);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua run thread returned %d", rc);
if (rc==NGX_AGAIN) {
returnngx_http_lua_run_posted_threads(c, vm, r, ctx, nreqs);
}
if (rc==NGX_DONE) {
ngx_http_lua_finalize_request(r, NGX_DONE);
returnngx_http_lua_run_posted_threads(c, vm, r, ctx, nreqs);
}
if (ctx->entered_content_phase) {
ngx_http_lua_finalize_request(r, rc);
returnNGX_DONE;
}
returnrc;
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */