Skip to content

Instantly share code, notes, and snippets.

@samizdatco
Created November 17, 2011 21:07
Show Gist options
  • Save samizdatco/1374529 to your computer and use it in GitHub Desktop.
Save samizdatco/1374529 to your computer and use it in GitHub Desktop.
An attempt at a periodic callback in nginx
#define NGX_HTTP_AUTH_DIGEST_CLEANUP_INTERVAL 3000
ngx_event_t *ngx_http_auth_digest_cleanup_timer;
static ngx_atomic_t *ngx_http_auth_digest_cleanup_lock;
// set as the "init process" callback in the ngx_module_t struct
static ngx_int_t ngx_http_auth_digest_worker_init(ngx_cycle_t *cycle){
if (ngx_process != NGX_PROCESS_WORKER){
return NGX_OK;
}
ngx_connection_t *dummy;
dummy = ngx_pcalloc(cycle->pool, sizeof(ngx_connection_t));
if (dummy == NULL) return NGX_ERROR;
dummy->fd = (ngx_socket_t) -1;
dummy->data = cycle;
ngx_http_auth_digest_cleanup_timer->log = ngx_cycle->log;
ngx_http_auth_digest_cleanup_timer->data = dummy;
ngx_http_auth_digest_cleanup_timer->handler = ngx_http_auth_digest_cleanup;
ngx_add_timer(ngx_http_auth_digest_cleanup_timer, NGX_HTTP_AUTH_DIGEST_CLEANUP_INTERVAL);
return NGX_OK;
}
void ngx_http_auth_digest_cleanup(ngx_event_t *ev){
// set up the next tick in n seconds
ngx_add_timer(ev, NGX_HTTP_AUTH_DIGEST_CLEANUP_INTERVAL);
// expire stale items from the rbtree
if (ngx_trylock(ngx_http_auth_digest_cleanup_lock)){
ngx_log_error(NGX_LOG_ERR, ev->log, 0, "cleanup");
ngx_http_auth_digest_rbtree_prune(ev->log);
ngx_unlock(ngx_http_auth_digest_cleanup_lock);
}else{
ngx_log_error(NGX_LOG_ERR, ev->log, 0, "locked");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment