Permalink
Browse files

Skip "backup" servers; support health checks

  • Loading branch information...
1 parent 2580b21 commit f149459b4967ed4ce293f8bed7fa90ab4ad9669b Evan Miller committed Feb 29, 2012
Showing with 28 additions and 3 deletions.
  1. +28 −3 ngx_http_upstream_hash_module.c
@@ -11,6 +11,10 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#if (NGX_HTTP_HEALTHCHECK)
+#include <ngx_http_healthcheck_module.h>
+#endif
+
#define ngx_bitvector_index(index) (index / (8 * sizeof(uintptr_t)))
#define ngx_bitvector_bit(index) ((uintptr_t) 1 << (index % (8 * sizeof(uintptr_t))))
@@ -19,6 +23,9 @@ typedef struct {
socklen_t socklen;
ngx_str_t name;
ngx_uint_t down;
+#if (NGX_HTTP_HEALTHCHECK)
+ ngx_int_t health_index;
+#endif
#if (NGX_HTTP_SSL)
ngx_ssl_session_t *ssl_session; /* local to a process */
#endif
@@ -61,7 +68,6 @@ static ngx_int_t ngx_http_upstream_init_hash(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us);
static ngx_uint_t ngx_http_upstream_hash_crc32(u_char *keydata, size_t keylen);
-
static ngx_command_t ngx_http_upstream_hash_commands[] = {
{ ngx_string("hash"),
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
@@ -118,7 +124,9 @@ ngx_http_upstream_init_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
ngx_uint_t i, j, n;
ngx_http_upstream_server_t *server;
ngx_http_upstream_hash_peers_t *peers;
-
+#if (NGX_HTTP_HEALTHCHECK)
+ ngx_int_t health_index;
+#endif
us->peer.init = ngx_http_upstream_init_hash_peer;
if (!us->servers) {
@@ -144,10 +152,23 @@ ngx_http_upstream_init_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
/* one hostname can have multiple IP addresses in DNS */
for (n = 0, i = 0; i < us->servers->nelts; i++) {
for (j = 0; j < server[i].naddrs; j++, n++) {
+ if (server[i].backup)
+ continue;
peers->peer[n].sockaddr = server[i].addrs[j].sockaddr;
peers->peer[n].socklen = server[i].addrs[j].socklen;
peers->peer[n].name = server[i].addrs[j].name;
peers->peer[n].down = server[i].down;
+#if (NGX_HTTP_HEALTHCHECK)
+ if (!server[i].down) {
+ health_index =
+ ngx_http_healthcheck_add_peer(us,
+ &server[i].addrs[j], cf->pool);
+ if (health_index == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+ peers->peer[n].health_index = health_index;
+ }
+#endif
}
}
@@ -345,7 +366,11 @@ static void ngx_http_upstream_hash_next_peer(ngx_http_upstream_hash_peer_data_t
// the current peer isn't marked down
while ((*tries)-- && (
(uhpd->tried[ngx_bitvector_index(current)] & ngx_bitvector_bit(current))
- || uhpd->peers->peer[current].down)) {
+ || uhpd->peers->peer[current].down
+#if (NGX_HTTP_HEALTHCHECK)
+ || ngx_http_healthcheck_is_down(uhpd->peers->peer[current].health_index, log)
+#endif
+ )) {
uhpd->current_key.len = ngx_sprintf(uhpd->current_key.data, "%d%V",
++uhpd->try_i, &uhpd->original_key) - uhpd->current_key.data;
uhpd->hash += ngx_http_upstream_hash_crc32(uhpd->current_key.data,

1 comment on commit f149459

cep21 commented on f149459 Feb 29, 2012

yippe!

Please sign in to comment.