# Integrating event\_add\_timer, am I doing this right?

**URL:** <https://www.ruby-forum.com/t/integrating-event-add-timer-am-i-doing-this-right/221297>\
**Category:** NGINX\
**Created:** [July 7, 2012, 7:57pm UTC](https://www.ruby-forum.com/t/integrating-event-add-timer-am-i-doing-this-right/221297 "2012-07-07T19:57:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 7, 2012, 7:57pm UTC](https://www.ruby-forum.com/t/integrating-event-add-timer-am-i-doing-this-right/221297/1 "2012-07-07T19:57:02Z")

</div>

Hi,

So I have module where I need a certain number of events only to run  
every 1 second. As of right now I am running them on every request and  
it’s using a mutex lock. So at 500 requests a second you can see where  
this is unnecessary and causing a problem.

So after some research I attached my own method to the “init process”  
handler. I then noticed it’s called for each worker process, so whatever  
worker\_processes is equal to. So in my case my method was being called  
16 times.

Now I do not need to attach it to 16 processes, only one. So I created a  
shared memory that would store the pid of the first process. If the next  
worker sees a PID is already set then it returns.

Then I attach my method to event\_add\_timer to have it run every second.

This is working great, and with workers I only have one of the workers  
timing my method every second.

Now my question is, was this the right way to go about this? Here is my  
code for the init process handler.

/\* Initialize process \*/  
static ngx\_int\_t ngx\_http\_mymodule\_init\_proc(ngx\_cycle\_t \*cycle)  
{

// There is already an event owner set.  
if(ngx\_http\_mymodule\_shm-\>event\_owner)  
return NGX\_OK;

// Set the event owner.  
ngx\_http\_mymodule\_shm-\>event\_owner = ngx\_pid;

// Let’s try to add a timer just for fun.  
ngx\_http\_mymodule\_timer-\>log = cycle-\>log;  
ngx\_http\_mymodule\_timer-\>data = “”; // Blank for now  
ngx\_http\_mymodule\_timer-\>handler = ngx\_http\_mymodule\_timer\_test;  
ngx\_add\_timer(ngx\_http\_mymodule\_timer, 1000);

return NGX\_OK;  
}

Posted at Nginx Forum:

> **[integrating event\_add\_timer, am I doing this right?](https://forum.nginx.org/read.php?2,228369,228369#msg-228369)**
>
> Hi, So I have module where I need a certain number of events only to run every 1 second. As of right now I am running them on every request and it's using a mutex lock. So at 500 requests a second you can see where this is unnecessary and causing a...

---

<div class="post-metadata">

**Author:** ![arosolino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@arosolino](https://www.ruby-forum.com/u/arosolino)\
**Post date:** [July 8, 2012, 6:53am UTC](https://www.ruby-forum.com/t/integrating-event-add-timer-am-i-doing-this-right/221297/2 "2012-07-08T06:53:09Z")

</div>

I need to allocate some memory inside the event, can i use the  
cycle-\>pool?

Posted at Nginx Forum:

> **[Re: integrating event\_add\_timer, am I doing this right?](https://forum.nginx.org/read.php?2,228369,228381#msg-228381)**
>
> Hi, So I have module where I need a certain number of events only to run every 1 second. As of right now I am running them on every request and it's using a mutex lock. So at 500 requests a second you can see where this is unnecessary and causing a...

---

<div class="post-metadata">

**Author:** ![arosolino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@arosolino](https://www.ruby-forum.com/u/arosolino)\
**Post date:** [July 8, 2012, 9:00am UTC](https://www.ruby-forum.com/t/integrating-event-add-timer-am-i-doing-this-right/221297/3 "2012-07-08T09:00:29Z")

</div>

I think I figured this out on my own, there posed an issue if a worker  
thread died. NGINX will automatically respawn a new one, and I need to  
make sure the event attaches to the new one. This code ended up working  
good.

// There is already an event owner set.  
if(dosstop\_shm-\>event\_owner)  
{  
// Does the PID still exist?  
for (s = 0; s \< ngx\_last\_process; s++) {  
if (ngx\_processes[s].pid == dosstop\_shm-\>event\_owner &&  
!ngx\_processes[s].exited) {  
ngx\_shmtx\_unlock(&shpool-\>mutex);  
return NGX\_OK;  
}  
}  
}

Posted at Nginx Forum:

> **[Re: integrating event\_add\_timer, am I doing this right?](https://forum.nginx.org/read.php?2,228369,228383#msg-228383)**
>
> Hi, So I have module where I need a certain number of events only to run every 1 second. As of right now I am running them on every request and it's using a mutex lock. So at 500 requests a second you can see where this is unnecessary and causing a...
