Recently I faced the necessity to manage request handlers order inside phase. I found code for ngx_http_lua_access_handler:
if (!lmcf->postponed_to_access_phase_end) {
lmcf->postponed_to_access_phase_end = 1;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
ph = cmcf->phase_engine.handlers;
cur_ph = &ph[r->phase_handler];
/* we should skip the post_access phase handler here too */
last_ph = &ph[cur_ph->next - 2];
dd("ph cur: %d, ph next: %d", (int) r->phase_handler,
(int) (cur_ph->next - 2));
#if 0
if (cur_ph == last_ph) {
dd("XXX our handler is already the last access phase handler");
}
#endif
if (cur_ph < last_ph) {
dd("swaping the contents of cur_ph and last_ph...");
tmp = *cur_ph;
memmove(cur_ph, cur_ph + 1,
(last_ph - cur_ph) * sizeof (ngx_http_phase_handler_t));
*last_ph = tmp;
r->phase_handler--; /* redo the current ph */
return NGX_DECLINED;
}
}
and asked about it in nginx mailing list. They answered: don't do it, t's silly because of duplicating code in different working processes. Could you please comment it ?