Skip to content

Commit

Permalink
fixup! fix(examples): assorted fixes in the async example
Browse files Browse the repository at this point in the history
  • Loading branch information
bavshin-f5 committed Feb 15, 2025
1 parent 327beb3 commit 7aa597b
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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_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_posted_next_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};
Expand Down Expand Up @@ -92,12 +92,10 @@ 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
Expand Down Expand Up @@ -145,11 +143,11 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
}

if let Some(ctx) = unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) } {
if ctx.done.load(Ordering::Relaxed) {
return core::Status::NGX_OK;
} else {
return core::Status::NGX_DONE;
if !ctx.done.load(Ordering::Relaxed) {
return core::Status::NGX_AGAIN;
}

return core::Status::NGX_OK;
}

let ctx = request.pool().allocate(RequestCTX::default());
Expand All @@ -164,9 +162,6 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
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);

// Request is no longer needed and can be converted to something movable to the async block
let req = AtomicPtr::new(request.into());
let done_flag = ctx.done.clone();
Expand All @@ -187,7 +182,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
// to wake up the event loop. (or patch nginx and use the same trick as the thread pool)
}));

core::Status::NGX_DONE
core::Status::NGX_AGAIN
});

extern "C" fn ngx_http_async_commands_set_enable(
Expand Down

0 comments on commit 7aa597b

Please sign in to comment.