best way to get a 1-second timer-tick?

Neil Mckee neil.mckee.ca at gmail.com
Fri Sep 6 19:14:34 UTC 2013


I finally got back to looking at this again.   Turns out I had been calling ngx_add_timer() too soon -- before the event rbtree was up and running.   I moved it to the callback that comes when an nginx process starts,  and now it works.  I now get my "tick" even if nothing else is happening.

https://code.google.com/p/nginx-sflow-module/source/browse/trunk/ngx_http_sflow_module.c#172

Just one question:  I had to declare my ngx_event_t as a static var so I could access it here,  because I couldn't see how to follow the ngx_cycle_t pointer back to the module's heap-allocated data.   Is there a good way,  or should I just be content to use a static var?

Neil



On Apr 23, 2011, at 3:39 AM, Arnaud GRANAL <serphen at gmail.com> wrote:

> 2011/4/23 Maxim Dounin <mdounin at mdounin.ru>:
>> Hello!
>> 
> 
> Hi!
> 
>> On Fri, Apr 22, 2011 at 09:09:15PM +0300, Arnaud GRANAL wrote:
>> 
>>> On Fri, Apr 22, 2011 at 9:02 PM, Neil Mckee <neil.mckee.ca at gmail.com> wrote:
>>>> Hello,
>>>> 
>>> 
>>> Hi Neil,
>>> 
>>>> I have written a module to implement sFlow in nginx  (nginx-sflow-module.googlecode.com).   I'm simulating a 1-second timer-tick by assuming that the request handler will be called at least once per second.   That's probably a safe assumption for any server that would care about sFlow monitoring,  but I expect there's a better way...
>>>> 
>>>> I tried asking for a timer callback like this:
>>>> 
>>>> ngx_event_t *ev = ngx_pcalloc(pool, sizeof(ngx_event_t));
>>>> ev->hander = ngx_http_sflow_tick_event_hander;
>>>> ngx_add_timer(ev, 1000);
>>>> 
>>>> but (like most russian girls) the event never called me back.  It looks like I might have to hang this on a file-descriptor somehow,  but that's where I'm getting lost.  Any pointers would be most appreciated.
>>>> 
>>> 
>>> The main thing is that you should use ngx_add_event() here instead of
>>> callocing the event struct directly and be careful of what you do with
>>> ngx_http_finalize_request.
>> 
>> No, you are wrong.
>> 
> 
> Then i'll know for the next guy :o)
> 
> A.
> 
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://nginx.org/mailman/listinfo/nginx-devel



More information about the nginx-devel mailing list