-
Notifications
You must be signed in to change notification settings - Fork 66
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
fix(examples): assorted fixes in the async example #133
Merged
bavshin-f5
merged 4 commits into
nginx:master
from
bavshin-f5:async-example-refactoring
Feb 15, 2025
Merged
fix(examples): assorted fixes in the async example #133
bavshin-f5
merged 4 commits into
nginx:master
from
bavshin-f5:async-example-refactoring
Feb 15, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5def64c
to
4a436a6
Compare
Take a look diff --git a/examples/async.rs b/examples/async.rs
index 7fe1584f..bb63a0f7 100644
--- a/examples/async.rs
+++ b/examples/async.rs
@@ -7,10 +7,10 @@ use std::time::Instant;
use ngx::core;
use ngx::ffi::{
- ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_core_module,
- ngx_http_core_run_phases, ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE,
- ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_post_event, ngx_posted_next_events, ngx_str_t, ngx_uint_t,
- NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
+ ngx_add_timer, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_core_module,
+ ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t,
+ ngx_post_event, ngx_posted_events, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF,
+ NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE,
};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_string};
@@ -92,17 +92,13 @@ impl http::Merge for ModuleConfig {
unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
let c: *mut ngx_connection_t = (*event).data.cast();
- let r: *mut ngx_http_request_t = (*c).data.cast();
if (*ctx).done.load(Ordering::Relaxed) {
- let main = &mut *(*r).main;
- main.set_count(main.count() - 1);
- ngx_http_core_run_phases(r);
+ // Triggering async_access_handler again
+ ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
} else {
- // this doesn't have have good performance but works as a simple thread-safe example and doesn't causes
- // segfault. The best method that provides both thread-safety and performance requires
- // an nginx patch.
- ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
+ // Checking again after 100ms to avoid busy loop
+ ngx_add_timer(event, 100);
}
}
@@ -162,10 +158,9 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
ctx.event.handler = Some(check_async_work_done);
ctx.event.data = request.connection().cast();
ctx.event.log = unsafe { (*request.connection()).log };
- unsafe { ngx_post_event(&mut ctx.event, addr_of_mut!(ngx_posted_next_events)) };
-
- let main = unsafe { &mut *request.as_ref().main };
- main.set_count(main.count() + 1);
+ unsafe {
+ ngx_add_timer(&mut ctx.event, 100);
+ }
// Request is no longer needed and can be converted to something movable to the async block
let req = AtomicPtr::new(request.into());
diff --git a/examples/t/async.t b/examples/t/async.t
index 294f551a..98505fdb 100644
--- a/examples/t/async.t
+++ b/examples/t/async.t
@@ -51,6 +51,6 @@ $t->run();
###############################################################################
-like(http_get('/', sleep => 2.1), qr/X-Async-Time:/, 'async handler');
+like(http_get('/index.html'), qr/X-Async-Time:/, 'async handler');
############################################################################### |
4a436a6
to
7aa597b
Compare
xeioex
approved these changes
Feb 15, 2025
* Use single tokio runtime instance. Move runtime initialization to the worker process. * Use `ngx_posted_next_events` to avoid posting events to the tail of currently executed queue. * Move `event` and `done` to the request context, remove `EventData`. * Ensure the async task cancellation when the request is aborted.
7aa597b
to
9b043ca
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ngx_posted_next_events
to avoid posting events to the tail of currently executed queue.event
anddone
to the request context, removeEventData
.feat!: replace
Request.get_inner
withAsRef
/AsMut
technically does not belong here, but it's too small to deserve a PR on its own.