public
Created

A patch for NGINX to allow limiting upstream retries by using the directive "proxy_next_tries N"

  • Download Gist
nginx-upstream-tries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#=================================================================================
# NGINX directive: proxy_next_tries
#
# Usage: proxy_next_tries N
# Description: Will attempt only N upstreams and then fail with last error
# Diff original version: 1.2.8
# Author: Shai Duvdevani
#
#=================================================================================
 
diff -ur /old/src/http/modules/ngx_http_proxy_module.c /new/src/http/modules/ngx_http_proxy_module.c
--- /old/src/http/modules/ngx_http_proxy_module.c 2013-04-21 18:25:09.616438202 +0000
+++ /new/src/http/modules/ngx_http_proxy_module.c 2013-04-23 21:40:57.679313148 +0000
@@ -477,6 +477,13 @@
offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
&ngx_http_proxy_next_upstream_masks },
 
+ { ngx_string("proxy_next_tries"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream_tries),
+ NULL },
+
{ ngx_string("proxy_pass_header"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_array_slot,
@@ -2630,6 +2637,7 @@
 
conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
+ conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
 
conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
diff -ur /old/src/http/ngx_http_core_module.c /new/src/http/ngx_http_core_module.c
--- /old/src/http/ngx_http_core_module.c 2013-04-21 18:25:09.619437856 +0000
+++ /new/src/http/ngx_http_core_module.c 2013-04-23 21:06:07.323563662 +0000
@@ -2412,6 +2412,7 @@
sr->loc_conf = cscf->ctx->loc_conf;
 
sr->pool = r->pool;
+ sr->us_tries = 0;
 
sr->headers_in = r->headers_in;
 
diff -ur /old/src/http/ngx_http_request.c /new/src/http/ngx_http_request.c
--- /old/src/http/ngx_http_request.c 2013-04-21 18:25:09.618437773 +0000
+++ /new/src/http/ngx_http_request.c 2013-04-23 21:06:40.746313325 +0000
@@ -488,6 +488,7 @@
 
r->main = r;
r->count = 1;
+ r->us_tries = 0;
 
tp = ngx_timeofday();
r->start_sec = tp->sec;
diff -ur /old/src/http/ngx_http_request.h /new/src/http/ngx_http_request.h
--- /old/src/http/ngx_http_request.h 2013-04-21 18:25:09.619437856 +0000
+++ /new/src/http/ngx_http_request.h 2013-04-23 21:05:25.802313195 +0000
@@ -425,6 +425,7 @@
off_t request_length;
 
ngx_uint_t err_status;
+ ngx_uint_t us_tries;
 
ngx_http_connection_t *http_connection;
 
diff -ur /old/src/http/ngx_http_upstream.c /new/src/http/ngx_http_upstream.c
--- /old/src/http/ngx_http_upstream.c 2013-04-21 18:25:09.619437856 +0000
+++ /new/src/http/ngx_http_upstream.c 2013-04-23 21:29:06.106568703 +0000
@@ -2904,6 +2904,11 @@
if (status) {
u->state->status = status;
 
+ if (u->conf->next_upstream_tries != NGX_CONF_UNSET_UINT && ++r->us_tries >= u->conf->next_upstream_tries) {
+ ngx_http_upstream_finalize_request(r, u, status);
+ return;
+ }
+
if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
 
#if (NGX_HTTP_CACHE)
diff -ur /old/src/http/ngx_http_upstream.h /new/src/http/ngx_http_upstream.h
--- /old/src/http/ngx_http_upstream.h 2013-04-21 18:25:09.618437773 +0000
+++ /new/src/http/ngx_http_upstream.h 2013-04-23 21:18:03.530313197 +0000
@@ -142,6 +142,7 @@
 
ngx_uint_t ignore_headers;
ngx_uint_t next_upstream;
+ ngx_uint_t next_upstream_tries;
ngx_uint_t store_access;
ngx_flag_t buffering;
ngx_flag_t pass_request_headers;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.