How can a content handler block on certain events before sending a response?

Maxim Dounin mdounin at
Wed Nov 24 04:11:04 MSK 2010


On Wed, Nov 24, 2010 at 12:43:46AM +0100, Hongli Lai wrote:

> Maxim Dounin wrote in post #963250:
> > Working with events isn't something trivial, but basically you'll
> > have to connect via ngx_event_connect_peer(), set up appropriate
> > read/write handlers in resulting connection structure and call
> > ngx_handle_(read|write)_event() as appropriate.
> I need to use ngx_add_event for registering a read/write watcher, right? 

No.  Depending on event method used there are different things 
required, e.g. with epoll and rtsig you have to call 
ngx_add_conn() to add connection to the event loop first.  And 
calling ngx_add_event() isn't needed (unless you are trying to do 
something non-standard or optimize specific case), 
ngx_handle_(read|write)_event() will do it for you.

Use ngx_event_connect_peer(), it will handle these details for 

> So I need to allocate an ngx_event_s struct, set its 'handler' member to 
> my handler function, and call ngx_add_event(&evt, NGX_READ_EVENT, 0)? Do 
> I need to set any other members for it to work? Do I need to zero the 
> structure if I don't set anything else?

See above, use ngx_event_connect_peer().

You may do the same steps by hand but this anyway will require 
almost the same code, connection structure and so on.

> What are appropriate times to call ngx_handle_read/write_event? What do 
> those functions do? Isn't the main loop supposed to call event handler 
> functions for me?

You should call ngx_handle_(read|write)_event() when you've done 
processing event (to re-enable this particular event in event 
loop, or to disable it if level-triggered event method used and 
event is still ready) or when you are going to wait for another 
event (to activate particular event in the even loop).

Don't hesitate to look into event/ngx_event.c.  These functions 
basically call ngx_add_event/ngx_del_event based on various 
conditions depending on event method used.

Maxim Dounin

More information about the nginx mailing list