Only fire a handler once

Yichun Zhang (agentzh) agentzh at gmail.com
Wed Sep 4 21:38:34 UTC 2013


Hello!

On Fri, Aug 16, 2013 at 10:28 PM, Aaron Bedra wrote:
> I'm looking for a way to make sure a handler only fires once.

If your handler is possible to run multiple times for the same request
(like the post_subrequest handlers) and you want to avoid that, you
can just use a module ctx field to serve as a flag for that. For
example,

    ctx = ngx_http_get_module_ctx(r, ngx_http_foo_module);
    if (ctx == NULL) {
        /* create ctx here and ctx->already_run should be initialized to 0 */
    }

    if (ctx->already_run) {
        return NGX_DONE;
    }
    /* first time */
    ctx->already_run = 1;

    /* process normally */

Regards,
-agentzh



More information about the nginx-devel mailing list