Skip to content

Instantly share code, notes, and snippets.

@abecciu
Created September 21, 2010 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abecciu/589241 to your computer and use it in GitHub Desktop.
Save abecciu/589241 to your computer and use it in GitHub Desktop.
nginx patch for queue wait time
--- src/http/modules/ngx_http_proxy_module.c.orig 2010-09-21 02:07:05.000000000 -0300
+++ src/http/modules/ngx_http_proxy_module.c 2010-09-21 02:09:05.000000000 -0300
@@ -74,6 +74,8 @@
ngx_uint_t headers_hash_max_size;
ngx_uint_t headers_hash_bucket_size;
+
+ ngx_flag_t queue_wait_hdrs;
} ngx_http_proxy_loc_conf_t;
@@ -152,6 +154,7 @@
ngx_http_proxy_loc_conf_t *plcf);
#endif
static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);
+static ngx_str_t *queue_wait_headers(const ngx_http_request_t *r);
static ngx_conf_post_t ngx_http_proxy_lowat_post =
@@ -461,6 +464,13 @@
#endif
+ { ngx_string("proxy_queue_wait_headers"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_flag_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_proxy_loc_conf_t, queue_wait_hdrs),
+ NULL },
+
ngx_null_command
};
@@ -878,6 +888,7 @@
ngx_http_script_engine_t e, le;
ngx_http_proxy_loc_conf_t *plcf;
ngx_http_script_len_code_pt lcode;
+ ngx_str_t *qw_headers = NULL;
u = r->upstream;
@@ -990,6 +1001,12 @@
}
}
+ if (plcf->queue_wait_hdrs == 1) {
+ if ( (qw_headers = queue_wait_headers(r)) == NULL) {
+ return NGX_ERROR;
+ }
+ len += qw_headers->len;
+ }
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
@@ -1118,6 +1135,14 @@
}
}
+ if (plcf->queue_wait_hdrs == 1) {
+ b->last = ngx_copy(b->last, qw_headers->data, qw_headers->len);
+ /* this is not necessary since the memory pool is destroyed when
+ * the request ends.
+ * ngx_pfree(r->pool, qw_headers->data);
+ * ngx_pfree(r->pool, qw_headers);
+ */
+ }
/* add "\r\n" at the header end */
*b->last++ = CR; *b->last++ = LF;
@@ -1951,6 +1976,8 @@
conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
+ conf->queue_wait_hdrs = NGX_CONF_UNSET;
+
return conf;
}
@@ -2219,6 +2246,8 @@
}
}
+ ngx_conf_merge_value(conf->queue_wait_hdrs, prev->queue_wait_hdrs, 1);
+
/* STUB */
if (prev->proxy_lengths) {
conf->proxy_lengths = prev->proxy_lengths;
@@ -3048,3 +3077,38 @@
v->uri = u->uri;
}
+
+
+static ngx_str_t *
+queue_wait_headers(const ngx_http_request_t *r)
+{
+ struct timeval tv;
+ uint64_t now_usec, req_start_usec, diff;
+ u_char *p;
+ size_t len;
+ ngx_str_t h1 = ngx_string("X-Request-Start: t=");
+ ngx_str_t h2 = ngx_string("X-Request-Read: t=");
+ ngx_str_t *res;
+
+ ngx_gettimeofday(&tv);
+ now_usec = (((uint64_t)tv.tv_sec * 1000 * 1000) + (uint64_t)tv.tv_usec);
+ req_start_usec = (((uint64_t)r->start_sec * 1000 * 1000) +
+ ((uint64_t)r->start_msec * 1000));
+ diff = now_usec - req_start_usec;
+
+ len = h1.len + h2.len + (2*NGX_INT64_LEN) + (2*sizeof("\r\n"));
+ if ( (p = ngx_pnalloc(r->pool, len)) == NULL) {
+ return NULL;
+ }
+ ngx_memzero(p, len);
+ len = ngx_sprintf(p, "%V%L\r\n%V%L\r\n", &h1, now_usec, &h2, diff) - p;
+
+ if ( (res = (ngx_str_t *)ngx_pnalloc(r->pool, sizeof(ngx_str_t))) == NULL) {
+ return NULL;
+ }
+
+ ngx_memzero(res, sizeof(ngx_str_t));
+ res->len = len;
+ res->data = p;
+ return res;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment