Welcome! Log In Create A New Profile

Advanced

integrating event_add_timer, am I doing this right?

July 07, 2012 01:56PM
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;
}
Subject Author Posted

integrating event_add_timer, am I doing this right?

arosolino July 07, 2012 01:56PM

Re: integrating event_add_timer, am I doing this right?

arosolino July 08, 2012 12:52AM

Re: integrating event_add_timer, am I doing this right?

arosolino July 08, 2012 03:00AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 285
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready