-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: support of non-preemptive time slicing when running CPU intensive Lua code. #1052
Conversation
src/ngx_http_lua_sleep.c
Outdated
ngx_add_timer(&coctx->sleep, (ngx_msec_t) delay); | ||
coctx->sleep.delayed = 1; | ||
ngx_post_event(&coctx->sleep, &ngx_posted_delayed_events); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: need a blank line before this } else {
line.
src/ngx_http_lua_sleep.c
Outdated
|
||
ngx_add_timer(&coctx->sleep, (ngx_msec_t) delay); | ||
coctx->sleep.delayed = 1; | ||
ngx_post_event(&coctx->sleep, &ngx_posted_delayed_events); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this does not present in older nginx cores like 1.11.2 and 1.9.15? Better protect it with macros?
e5032b5
to
589db0f
Compare
This solution does not seem to work
|
@yourenyouyu Your example works fine for me:
Are you sure you have applied the NGINX core patch as well? |
Whether your configuration is only one worker |
@yourenyouyu it is single worker. |
@yourenyouyu if it still does not work for you after patching, recompile using:
and send me the NGINX |
Ok, wait a little @dndx |
|
@dndx ok, Thank you very much。Can you teach me how to use it, give me a detail step? |
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd better do not duplicate this code path for delay > 0
. It is duplicated below in the #else
branch.
I think we still need to handle the case delay == 0
when the patch is absent. In this case, we should emit an error log message so that the user can be notified about the lack of nginx core patch instead of silently ignoring it.
@dndx I wonder if it's feasible to add some tests to cover this change? |
@dndx How about the following test case?
|
@agentzh yes that is a good test, although it may not work without the core patch. Do you want me to add it to the PR nevertheless? |
@dndx Okay, merged with the following extra patch. Thanks! diff --git a/t/077-sleep.t b/t/077-sleep.t
index 7d295c2b..96f04bd9 100644
--- a/t/077-sleep.t
+++ b/t/077-sleep.t
@@ -9,10 +9,10 @@ log_level('debug');
repeat_each(2);
-plan tests => repeat_each() * 63;
+plan tests => repeat_each() * 71;
#no_diff();
-#no_long_string();
+no_long_string();
run_tests();
__DATA__
@@ -404,3 +404,99 @@ ok
--- no_error_log
[error]
[alert]
+
+
+
+=== TEST 16: sleep 0
+--- config
+ location /t {
+ content_by_lua_block {
+ local function f (n)
+ print("f begin ", n)
+ ngx.sleep(0)
+ print("f middle ", n)
+ ngx.sleep(0)
+ print("f end ", n)
+ ngx.sleep(0)
+ end
+
+ for i = 1, 3 do
+ assert(ngx.thread.spawn(f, i))
+ end
+
+ ngx.say("ok")
+ }
+ }
+--- request
+GET /t
+--- response_body
+ok
+--- no_error_log
+[error]
+--- grep_error_log eval: qr/\bf (?:begin|middle|end)\b|\bworker cycle$|\be?poll timer: \d+$/
+--- grep_error_log_out eval
+qr/f begin
+f begin
+f begin
+worker cycle
+e?poll timer: 0
+f middle
+f middle
+f middle
+worker cycle
+e?poll timer: 0
+f end
+f end
+f end
+worker cycle
+e?poll timer: 0
+/
+
+
+
+=== TEST 17: sleep short times less than 1ms
+--- config
+ location /t {
+ content_by_lua_block {
+ local delay = 0.0005
+
+ local function f (n)
+ print("f begin ", n)
+ ngx.sleep(delay)
+ print("f middle ", n)
+ ngx.sleep(delay)
+ print("f end ", n)
+ ngx.sleep(delay)
+ end
+
+ for i = 1, 3 do
+ assert(ngx.thread.spawn(f, i))
+ end
+
+ ngx.say("ok")
+ }
+ }
+--- request
+GET /t
+--- response_body
+ok
+--- no_error_log
+[error]
+--- grep_error_log eval: qr/\bf (?:begin|middle|end)\b|\bworker cycle$|\be?poll timer: \d+$/
+--- grep_error_log_out eval
+qr/f begin
+f begin
+f begin
+worker cycle
+e?poll timer: 0
+f middle
+f middle
+f middle
+worker cycle
+e?poll timer: 0
+f end
+f end
+f end
+worker cycle
+e?poll timer: 0
+/ |
I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.
Note that the
delayed
field is not being used by thecoctx->sleep
event and we reuse it for keeping track of whether at least one event loop has passed.Need to be used with: openresty/openresty#252
Test config:
Before the patch, curl
/
in one terminal while curl/404
in another window,/404
will not return until/
finished.After the patch, curl
/404
is always responsible even if/
is running at the same time.