Context for connections

Eugaia ngx.eugaia at
Fri Aug 19 22:18:36 UTC 2011

Hi Brian,

On 19/08/2011 17:50, Akins, Brian wrote:
> I'd like to be able to store some per-module, per-connection data. Like the
> ctx in the requests.  I was wondering if I should just patch nginx, or if
> someone else had done something similar.
Can I assume that in your case just storing the data in the c->data slot 
isn't going to work (easily/nicely)?  e.g.

typedef struct {
     ngx_http_request_t  *r;  /* or whatever other info you need */
     void **ctx;
} ngx_my_connection_data_t;

Assuming you're talking about hooking into the http requests somehow, 
you probably don't want to be playing around with the main connection in 
this way, because there are loads of functions that assume that c->data 
= (some ngx_http_request_t* - either the parent or a subrequest) - it's 
doable, but messy.

However, if you're creating your own connections, then there's no reason 
you can't use such a construction.  You'd have to make sure that all 
your event handlers and other callbacks were able to handle this data 
struct of course, though.

If you are talking about doing this for connections including 'standard' 
ones, like the main http request, I'd patch the ngx_connection_t struct 
to just add an extra ctx to it.  It's a very minor addition, and would 
be much easier than trying to deal with changing event handlers all over 
the place.



More information about the nginx-devel mailing list