Skip to content

Instantly share code, notes, and snippets.

@ctgswallow
Created January 18, 2012 19:01
Show Gist options
  • Save ctgswallow/1634933 to your computer and use it in GitHub Desktop.
Save ctgswallow/1634933 to your computer and use it in GitHub Desktop.
a.patch
--- src/http/ngx_http_upstream_round_robin.c.orig 2011-09-30 14:30:01.000000000 +0000
+++ src/http/ngx_http_upstream_round_robin.c 2012-01-18 18:38:25.438381169 +0000
@@ -7,13 +7,44 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
+#include <ngx_supervisord.h>
+
+#if (NGX_SUPERVISORD_API_VERSION != 2)
+ #error "ngx_supervisord-aware upstream requires NGX_SUPERVISORD_API v2"
+#endif
+/*
+ * disable sorting
static ngx_int_t ngx_http_upstream_cmp_servers(const void *one,
const void *two);
+ */
static ngx_uint_t
ngx_http_upstream_get_peer(ngx_http_upstream_rr_peers_t *peers);
+void ngx_http_upstream_backend_monitor(ngx_http_upstream_srv_conf_t *uscf,
+ ngx_uint_t backend, ngx_uint_t new_status)
+{
+ ngx_http_upstream_rr_peers_t *peers;
+ ngx_uint_t i;
+
+ peers = uscf->peer.data;
+ for (i = 0; i < peers->number; i++) {
+ if (peers->peer[i].onumber == backend) {
+ if (new_status == NGX_SUPERVISORD_SRV_UP) {
+ peers->peer[i].down = 0;
+ peers->peer[i].weight = peers->peer[i].oweight;
+ peers->peer[i].current_weight = peers->peer[i].oweight;
+ } else {
+ peers->peer[i].down = 1;
+ peers->peer[i].weight = 0;
+ peers->peer[i].current_weight = 0;
+ }
+ }
+ }
+}
+
+
#if (NGX_HTTP_SSL)
static ngx_int_t ngx_http_upstream_empty_set_session(ngx_peer_connection_t *pc,
@@ -23,7 +54,6 @@
#endif
-
ngx_int_t
ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us)
@@ -36,6 +66,9 @@
us->peer.init = ngx_http_upstream_init_round_robin_peer;
if (us->servers) {
+ ngx_supervisord_add_backend_monitor(us,
+ ngx_http_upstream_backend_monitor);
+
server = us->servers->elts;
n = 0;
@@ -74,15 +107,20 @@
peers->peer[n].down = server[i].down;
peers->peer[n].weight = server[i].down ? 0 : server[i].weight;
peers->peer[n].current_weight = peers->peer[n].weight;
+ peers->peer[n].onumber = i;
+ peers->peer[n].oweight = server[i].weight;
n++;
}
}
us->peer.data = peers;
+/*
+ * disable sorting
ngx_sort(&peers->peer[0], (size_t) n,
sizeof(ngx_http_upstream_rr_peer_t),
ngx_http_upstream_cmp_servers);
+ */
/* backup servers */
@@ -127,15 +165,20 @@
backup->peer[n].max_fails = server[i].max_fails;
backup->peer[n].fail_timeout = server[i].fail_timeout;
backup->peer[n].down = server[i].down;
+ backup->peer[n].onumber = i;
+ backup->peer[n].oweight = server[i].weight;
n++;
}
}
peers->next = backup;
+/*
+ * disable sorting
ngx_sort(&backup->peer[0], (size_t) n,
sizeof(ngx_http_upstream_rr_peer_t),
ngx_http_upstream_cmp_servers);
+ */
return NGX_OK;
}
@@ -185,6 +228,8 @@
peers->peer[i].current_weight = 1;
peers->peer[i].max_fails = 1;
peers->peer[i].fail_timeout = 10;
+ peers->peer[i].onumber = 0;
+ peers->peer[i].oweight = 1;
}
us->peer.data = peers;
@@ -194,7 +239,8 @@
return NGX_OK;
}
-
+/*
+ * disable sorting
static ngx_int_t
ngx_http_upstream_cmp_servers(const void *one, const void *two)
{
@@ -205,6 +251,7 @@
return (first->weight < second->weight);
}
+ */
ngx_int_t
@@ -225,6 +272,7 @@
r->upstream->peer.data = rrp;
}
+ rrp->uscf = us;
rrp->peers = us->peer.data;
rrp->current = 0;
@@ -257,7 +305,7 @@
ngx_http_upstream_save_round_robin_peer_session;
#endif
- return NGX_OK;
+ return ngx_supervisord_check_servers(r);
}
@@ -415,6 +463,16 @@
/* there are several peers */
+ for (i = 0; i < rrp->peers->number; i++) {
+ if (!rrp->peers->peer[i].down) {
+ break;
+ }
+ }
+
+ if (i == rrp->peers->number) {
+ goto failed;
+ }
+
if (pc->tries == rrp->peers->number) {
/* it's a first try - get a current peer */
@@ -685,6 +743,10 @@
peer->current_weight = 0;
}
+ peer->fails = 0;
+ (void) ngx_supervisord_execute(rrp->uscf, NGX_SUPERVISORD_CMD_STOP,
+ peer->onumber, NULL);
+
/* ngx_unlock_mutex(rrp->peers->mutex); */
}
--- src/http/ngx_http_upstream_round_robin.h.orig 2012-01-18 18:23:10.664996742 +0000
+++ src/http/ngx_http_upstream_round_robin.h 2012-01-18 18:41:18.750456106 +0000
@@ -17,9 +17,11 @@
struct sockaddr *sockaddr;
socklen_t socklen;
ngx_str_t name;
+ ngx_uint_t onumber;
ngx_int_t current_weight;
ngx_int_t weight;
+ ngx_int_t oweight;
ngx_uint_t fails;
time_t accessed;
@@ -54,6 +56,7 @@
typedef struct {
+ ngx_http_upstream_srv_conf_t *uscf;
ngx_http_upstream_rr_peers_t *peers;
ngx_uint_t current;
uintptr_t *tried;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment