Skip to content
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
merged 4 commits into from
Feb 15, 2025

Conversation

bavshin-f5
Copy link
Member

  • 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.
  • Test async example in CI.

feat!: replace Request.get_inner with AsRef/AsMut technically does not belong here, but it's too small to deserve a PR on its own.

@bavshin-f5 bavshin-f5 force-pushed the async-example-refactoring branch from 5def64c to 4a436a6 Compare February 15, 2025 01:11
@xeioex
Copy link
Contributor

xeioex commented Feb 15, 2025

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');
 
 ###############################################################################

@bavshin-f5 bavshin-f5 force-pushed the async-example-refactoring branch from 4a436a6 to 7aa597b Compare February 15, 2025 07:21
* 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.
@bavshin-f5 bavshin-f5 force-pushed the async-example-refactoring branch from 7aa597b to 9b043ca Compare February 15, 2025 07:29
@bavshin-f5 bavshin-f5 merged commit 5bc247f into nginx:master Feb 15, 2025
10 checks passed
@bavshin-f5 bavshin-f5 deleted the async-example-refactoring branch February 15, 2025 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants