Thanks Nick. Are you saying that between
evbuffer_add_reference() call and eventual call to write the tcp buffer to the socket, the code needs to through event_loop? I added a small debug code and observed the following.
Some of the events are more process intensive then others. When such intensive events happen, we have periodic writes to TCP via evbuffer_add_reference(). I was monitoring TCP outQ (via netstat/ss) and also output of evbuffer_get_length(). I see that TCP outQ is 0, i.e. there is no data in the pipe, but after every writes, output of evbuffer_get_length() keeps going up, which means that bufferevent is buffering the data even when it could have flushed out the data inline. So it looks like the actual write to TCP socket is happening in the next iteration of event_loop but not in the same.
Is there anything I can use to force immediate write (on calling evbuffer_add_reference or bufferevent_write) if socket ready to be written? I see that be_socket_flush() just returns. Can it be used or is there any other alternative?