integrating event_add_timer, am I doing this right?

arosolino nginx-forum at
Sat Jul 7 17:56:25 UTC 2012


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.
		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:,228369,228369#msg-228369

More information about the nginx mailing list