Skip to content

About

Alternative async executor for nginx modules

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

crates.io docs.rs

ngx-tickle

Thread-safe async executor and event-loop wakeup for nginx modules

Synopsis

use async_compat::CompatExt;
use ngx_tickle::prelude::*;

async fn async_handler(request: &mut ngx::http::Request) {
    let response = reqwest::get("http://example.com").compat().await.unwrap();
    request.add_header_out("x-example-ran", &format!("{}", response.status()));
    finalize_request(request, ngx::http::HTTPStatus::NO_CONTENT.into());
}

ngx::http_request_handler!(access_phase_handler, |request: &mut ngx::http::Request| {
    if request.spawn(async_handler).is_err() {
        return ngx::core::Status::NGX_ERROR;
    }

    ngx::core::Status::NGX_AGAIN
});

// ngx-tickle must be initialized once per worker, before the first spawn() — call this
// from your module's init_process. See ngx_tickle::init() for the module wiring.
ngx_tickle::init();

When do I need this?

ngx provides its own ngx::async_::spawn() for running async tasks on nginx's event loop. This works well for purely single-threaded futures — but as soon as a secondary thread is involved, things get tricky:

  • Wakeups from other threads can cause futures to be polled outside the main thread, violating nginx's single-threaded model. This leads to segfaults and memory corruption.
  • No event-loop wakeup mechanism means that if nginx is blocked in epoll_wait when a background task completes, the result won't be delivered until an unrelated I/O event happens. This causes requests to hang unpredictably.

Secondary threads appear whenever you integrate another runtime — whether through async-compat or a dedicated “sidecar”-runtime like tokio.

ngx-tickle provides a drop-in replacement spawn() that handles all of this correctly: It allows wakeups from any thread, but ensures futures always run on the main thread, to maintain the single-threaded requirement of nginx core and will wake up — “tickle” — the event loop, if needed, to ensure prompt processing.

How it works

Executor: spawn() creates async tasks using the same async_task primitives as ngx. The difference is in scheduling: when a wakeup arrives on a secondary thread, the runnable is placed on a thread-safe queue instead of being executed immediately. A native nginx event is scheduled; its handler is guaranteed to run on the main thread and drains the queue. An event-loop notification (“tickle“) ensures nginx picks it up promptly.

Notify: A lightweight wakeup mechanism — eventfd on Linux, a self-pipe elsewhere — registered as a read event on the nginx event loop. When a runnable is queued from another thread, writing to this fd causes epoll_wait/kqueue to return, and nginx will process our event immediately.

Key invariant: Futures are always polled on the nginx main thread. spawn() does not require Send, so they can freely hold references to nginx structures like ngx::http::Request.

Performance

Per-task overhead is small enough to disappear in nginx's own request pipeline.

ngx-tickle’s scheduler matches ngx-rust’s within a few percent, adds no measurable heap, and keeps latencies tight.

Refer to the benchmark report for data comparing the schedulers, nginx native I/O futures vs tokio, and exploring the impact of batch_size tuning.

For ngx-rust users

Note

Since ngx-tickle 0.3.0, it is required to call init() in your module's init_process

After initialization, ngx::async_::spawn calls translate one-for-one to spawn(). The synopsis above also shows request.spawn, the new request-bound entry point.

License

ngx-tickle is distributed under the terms of the MIT license, or the Apache License (Version 2.0), at your option.

MSRV

1.85+ (edition 2024)

Examples

  • compat.rs — “I just want to use reqwest” via async-compat.
  • sidecar.rs — off-thread work via a sidecar tokio runtime.
  • yielding.rs — cooperative yielding / fairness demo.

About

Alternative async executor for nginx modules

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages