From cc57776f5f6bd8d2fa39d6852f5d072260a7eb7f Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Thu, 14 Jul 2016 11:33:37 -0400 Subject: [PATCH 01/32] A little bit of README Driven Development --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c559021..0f2a23c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Table of Contents * [Functions](#functions) * [get_upstreams](#get_upstreams) * [get_servers](#get_servers) + * [add_server_to_upstream](#add_server_to_upstream) + * [remove_server_from_upstream](#remove_server_from_upstream) * [get_primary_peers](#get_primary_peers) * [get_backup_peers](#get_backup_peers) * [set_peer_down](#set_peer_down) @@ -126,6 +128,34 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua [Back to TOC](#table-of-contents) +add_server_to_upstream +----------- +`syntax: err = upstream.add_server_to_upstream(upstream_name, server)` + +Add a server to a given upstream. The server should be a hash-like Lua table and may contain the following keys (bolded keys are required): + +* **name** +* **addr** + socket address(es). can be either a Lua string or an array-like Lua table of Lua strings. +* backup +* fail_timeout +* max_fails +* weight + +The return value is an error string or nil. + +[Back to TOC](#table-of-contents) + +remove_server_from_upstream +----------- +`syntax: err = upstream.remove_server_from_upstream(upstream_name, server_name)` + +Removes a server from an upstream given the upstream and server names. The server name is expected to match the `name` field on the server in the upstream. + +The return value is an error string or nil. + +[Back to TOC](#table-of-contents) + get_primary_peers --------- `syntax: peers = upstream.get_primary_peers(upstream_name)` @@ -329,4 +359,3 @@ See Also * the [lua-resty-upstream-healthcheck](https://github.com/openresty/lua-resty-upstream-healthcheck) library which makes use of the Lua API provided by this module. [Back to TOC](#table-of-contents) - From 508c71e090b4f867c00d80ce289df913f2a16c9c Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Thu, 14 Jul 2016 15:04:19 -0400 Subject: [PATCH 02/32] Spike out upstream.add_server_to_upstream() This is based heavily on the work in https://github.com/openresty/lua-upstream-nginx-module/pull/13 but involves slightly fewer changes to the module as a whole. --- README.md | 14 +--- src/ngx_http_lua_upstream_module.c | 116 +++++++++++++++++++++++++++++ t/sanity.t | 49 ++++++++++++ 3 files changed, 167 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0f2a23c..bf20398 100644 --- a/README.md +++ b/README.md @@ -130,19 +130,9 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua add_server_to_upstream ----------- -`syntax: err = upstream.add_server_to_upstream(upstream_name, server)` +`syntax: ok, err = upstream.add_server(upstream_name, ip:port, weight, max_fails, fail_timeout)` -Add a server to a given upstream. The server should be a hash-like Lua table and may contain the following keys (bolded keys are required): - -* **name** -* **addr** - socket address(es). can be either a Lua string or an array-like Lua table of Lua strings. -* backup -* fail_timeout -* max_fails -* weight - -The return value is an error string or nil. +The return values are a boolean denoting the success of the operation and an associated error if one occurs. [Back to TOC](#table-of-contents) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 361ee92..6e522ad 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -23,6 +23,7 @@ static ngx_int_t ngx_http_lua_upstream_init(ngx_conf_t *cf); static int ngx_http_lua_upstream_create_module(lua_State * L); static int ngx_http_lua_upstream_get_upstreams(lua_State * L); static int ngx_http_lua_upstream_get_servers(lua_State * L); +static int ngx_http_lua_upstream_add_server_to_upstream(lua_State * L); static ngx_http_upstream_main_conf_t * ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L); static int ngx_http_lua_upstream_get_primary_peers(lua_State * L); @@ -31,12 +32,15 @@ static int ngx_http_lua_get_peer(lua_State *L, ngx_http_upstream_rr_peer_t *peer, ngx_uint_t id); static ngx_http_upstream_srv_conf_t * ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host); +static ngx_http_upstream_server_t* + ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t u); static ngx_http_upstream_rr_peer_t * ngx_http_lua_upstream_lookup_peer(lua_State *L); static int ngx_http_lua_upstream_set_peer_down(lua_State * L); static int ngx_http_lua_upstream_current_upstream_name(lua_State *L); + static ngx_http_module_t ngx_http_lua_upstream_ctx = { NULL, /* preconfiguration */ ngx_http_lua_upstream_init, /* postconfiguration */ @@ -141,6 +145,92 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) return 1; } +static int +ngx_http_lua_upstream_add_server_to_upstream(lua_State * L) +{ + ngx_str_t host; + ngx_http_upstream_server_t *us; + ngx_http_upstream_srv_conf_t *uscf; + ngx_url_t u; + ngx_http_request_t *r; + ngx_int_t weight, max_fails; + ngx_uint_t backup; + time_t fail_timeout; + u_char *p; + + if (lua_gettop(L) != 6) { + /* + * "upstream name", "host:port", "weight", "max_fails", "fail_timeout", "backup" + */ + return luaL_error(L, "exactly 6 arguments expected"); + } + + r = ngx_http_lua_get_request(L); + if (r == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "get request error \n"); + return 2; + } + + host.data = (u_char *) luaL_checklstring(L, 1, &host.len); + + ngx_memzero(&u, sizeof(ngx_url_t)); + p = (u_char *) luaL_checklstring(L, 2, &u.url.len); + u.default_port = 80; + + weight = (ngx_int_t) luaL_checkint(L, 3); + max_fails = (ngx_int_t) luaL_checkint(L, 4); + fail_timeout = (time_t) luaL_checklong(L, 5); + backup = lua_toboolean(L, 6); +#if (NGX_DEBUG) + ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout); +#endif + + uscf = ngx_http_lua_upstream_find_upstream(L, &host); + if (uscf == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "upstream not found\n"); + return 2; + } + + u.url.data = ngx_pcalloc(uscf->servers->pool, u.url.len+1); + ngx_memcpy(u.url.data, p, u.url.len); + + if (ngx_http_lua_upstream_find_server(uscf, u) != NULL) { + lua_pushnil(L); + lua_pushliteral(L, "server already exists\n"); + return 2; + } else { + // validate URL + if (ngx_parse_url(uscf->servers->pool, &u) != NGX_OK) { + if (u.err) { + lua_pushnil(L); + lua_pushliteral(L, "url parser error\n"); + return 2; + } + } + + us = ngx_array_push(uscf->servers); + if (us == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "could not append server to upstream\n"); + return 2; + } + + ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); + + us->name = u.url; + us->addrs = u.addrs; + us->naddrs = u.naddrs; + us->weight = weight; + us->max_fails = max_fails; + us->fail_timeout = fail_timeout; + us->backup = backup; + } + + lua_pushboolean(L, 1); + return 1; +} static int ngx_http_lua_upstream_get_servers(lua_State * L) @@ -551,6 +641,32 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) return NULL; } +static ngx_http_upstream_server_t* +ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t u) +{ + ngx_uint_t i, j; + size_t len; + ngx_http_upstream_server_t *server = NULL; + + if (us->servers == NULL || us->servers->nelts == 0) { + return NULL; + } + + server = us->servers->elts; + + for (i = 0; i < us->servers->nelts; ++i) { + for (j = 0; j < server[i].naddrs; ++j) { + len = server[i].addrs[j].name.len; + + if (len == u.url.len + && ngx_memcmp(u.url.data, server[i].addrs[j].name.data, u.url.len) == 0) { + return &server[i]; + } + } + } + + return NULL; +} static int ngx_http_lua_upstream_current_upstream_name(lua_State *L) diff --git a/t/sanity.t b/t/sanity.t index c713c78..4185505 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -648,3 +648,52 @@ nil --- no_error_log [error] + +=== TEST 19: add server to pre-defined upstream +--- http_config + upstream foo { + } + +--- config + location /t { + content_by_lua ' + local upstream = require "ngx.upstream" + local err = upstream.add_server_to_upstream("foo", { name = "127.0.0.1:8080", addr = "127.0.0.1:8080" }) + if err then + ngx.say(err) + else + local srvs, err = get_servers("foo") + if not srvs then + ngx.say("failed to get servers in upstream foo") + else + ngx.say("upstream foo:") + for _, srv in ipairs(srvs) do + local first = true + for k, v in pairs(srv) do + if first then + first = false + ngx.print(" ") + else + ngx.print(", ") + end + if type(v) == "table" then + ngx.print(k, " = {", concat(v, ", "), "}") + else + ngx.print(k, " = ", v) + end + end + ngx.print("\\n") + end + end + ngs.say("done") + end + '; + } +--- request + GET /t +--- response_body +foo: + addr = 127.0.0.1:8080, name = 127.0.0.1:8080 +done +--- no_error_log +[error] From 63b8d44f9c760dd1e1ff8b4005ab74b855f07ebe Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Thu, 14 Jul 2016 18:31:42 -0400 Subject: [PATCH 03/32] minor fixes --- README.md | 2 +- src/ngx_http_lua_upstream_module.c | 3 +++ t/sanity.t | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf20398..a480220 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua add_server_to_upstream ----------- -`syntax: ok, err = upstream.add_server(upstream_name, ip:port, weight, max_fails, fail_timeout)` +`syntax: ok, err = upstream.add_server_to_upstream(upstream_name, ip:port, weight, max_fails, fail_timeout)` The return values are a boolean denoting the success of the operation and an associated error if one occurs. diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 6e522ad..f25f8cd 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -94,6 +94,9 @@ ngx_http_lua_upstream_create_module(lua_State * L) lua_pushcfunction(L, ngx_http_lua_upstream_get_servers); lua_setfield(L, -2, "get_servers"); + lua_pushcfunction(L, ngx_http_lua_upstream_add_server_to_upstream); + lua_setfield(L, -2, "add_server_to_upstream"); + lua_pushcfunction(L, ngx_http_lua_upstream_get_primary_peers); lua_setfield(L, -2, "get_primary_peers"); diff --git a/t/sanity.t b/t/sanity.t index 4185505..5a00ae5 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -658,7 +658,7 @@ nil location /t { content_by_lua ' local upstream = require "ngx.upstream" - local err = upstream.add_server_to_upstream("foo", { name = "127.0.0.1:8080", addr = "127.0.0.1:8080" }) + local err = upstream.add_server_to_upstream("foo", "127.0.0.1:8080", 1, 1, 1, false) if err then ngx.say(err) else From c2f2863870499f2ffded3ed3e04f64c1c530f150 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Thu, 14 Jul 2016 19:18:14 -0400 Subject: [PATCH 04/32] test things --- t/sanity.t | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/sanity.t b/t/sanity.t index 5a00ae5..f287877 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -652,6 +652,7 @@ nil === TEST 19: add server to pre-defined upstream --- http_config upstream foo { + server 127.0.0.2; } --- config @@ -693,6 +694,7 @@ nil GET /t --- response_body foo: + addr = 127.0.0.2, name = 127.0.0.2 addr = 127.0.0.1:8080, name = 127.0.0.1:8080 done --- no_error_log From 30047af2712355846f057fb82bb52fce227190f5 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 15:55:21 -0400 Subject: [PATCH 05/32] get_servers is a function in upstream module --- t/sanity.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index f287877..4a1a070 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -663,7 +663,7 @@ nil if err then ngx.say(err) else - local srvs, err = get_servers("foo") + local srvs, err = upstream.get_servers("foo") if not srvs then ngx.say("failed to get servers in upstream foo") else From 1874f9fe2d781c6090e8c29b8ba5d6ab0625af72 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:00:45 -0400 Subject: [PATCH 06/32] typo --- t/sanity.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index 4a1a070..84647be 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -686,7 +686,7 @@ nil ngx.print("\\n") end end - ngs.say("done") + ngx.say("done") end '; } From c8011bc948f81274a3678e594a963625be900bb1 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:05:57 -0400 Subject: [PATCH 07/32] test update --- t/sanity.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index 84647be..da2a275 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -693,9 +693,9 @@ nil --- request GET /t --- response_body -foo: - addr = 127.0.0.2, name = 127.0.0.2 - addr = 127.0.0.1:8080, name = 127.0.0.1:8080 +upstream foo: + addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, name = 127.0.0.2, max_fails = 1 + addr = 127.0.0.1:8080, weight = 1, fail_timeout = 10, name = 127.0.0.1:8080, max_fails = 1 done --- no_error_log [error] From 4b503ce8d7570a1028eedf49e6be5d90ab1aca06 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:15:14 -0400 Subject: [PATCH 08/32] typo --- t/sanity.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index da2a275..b6d1d39 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -695,7 +695,7 @@ nil --- response_body upstream foo: addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, name = 127.0.0.2, max_fails = 1 - addr = 127.0.0.1:8080, weight = 1, fail_timeout = 10, name = 127.0.0.1:8080, max_fails = 1 + addr = 127.0.0.1:8080, weight = 1, fail_timeout = 1, name = 127.0.0.1:8080, max_fails = 1 done --- no_error_log [error] From c17fb1852b1960addfe1ffe4737b443de2d7cc89 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:21:36 -0400 Subject: [PATCH 09/32] extra newline --- t/sanity.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/sanity.t b/t/sanity.t index b6d1d39..f5e4726 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -649,6 +649,7 @@ nil [error] + === TEST 19: add server to pre-defined upstream --- http_config upstream foo { From e75bf463bbc35a4cfd42158487fe21b6f8e8bf2e Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:22:57 -0400 Subject: [PATCH 10/32] correct retval --- t/sanity.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index f5e4726..6a23ed8 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -660,8 +660,8 @@ nil location /t { content_by_lua ' local upstream = require "ngx.upstream" - local err = upstream.add_server_to_upstream("foo", "127.0.0.1:8080", 1, 1, 1, false) - if err then + local ok, err = upstream.add_server_to_upstream("foo", "127.0.0.1:8080", 1, 1, 1, false) + if not ok then ngx.say(err) else local srvs, err = upstream.get_servers("foo") From 75e87a6a7b354dfea721f8748e8e8ae2d7d76831 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:23:47 -0400 Subject: [PATCH 11/32] rename add_server_to_upstream to add_peer --- README.md | 6 +++--- src/ngx_http_lua_upstream_module.c | 8 ++++---- t/sanity.t | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a480220..0af9c26 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Table of Contents * [Functions](#functions) * [get_upstreams](#get_upstreams) * [get_servers](#get_servers) - * [add_server_to_upstream](#add_server_to_upstream) + * [add_peer](#add_peer) * [remove_server_from_upstream](#remove_server_from_upstream) * [get_primary_peers](#get_primary_peers) * [get_backup_peers](#get_backup_peers) @@ -128,9 +128,9 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua [Back to TOC](#table-of-contents) -add_server_to_upstream +add_peer ----------- -`syntax: ok, err = upstream.add_server_to_upstream(upstream_name, ip:port, weight, max_fails, fail_timeout)` +`syntax: ok, err = upstream.add_peer(upstream_name, ip:port, weight, max_fails, fail_timeout)` The return values are a boolean denoting the success of the operation and an associated error if one occurs. diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index f25f8cd..4e320c5 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -23,7 +23,7 @@ static ngx_int_t ngx_http_lua_upstream_init(ngx_conf_t *cf); static int ngx_http_lua_upstream_create_module(lua_State * L); static int ngx_http_lua_upstream_get_upstreams(lua_State * L); static int ngx_http_lua_upstream_get_servers(lua_State * L); -static int ngx_http_lua_upstream_add_server_to_upstream(lua_State * L); +static int ngx_http_lua_upstream_add_peer(lua_State * L); static ngx_http_upstream_main_conf_t * ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L); static int ngx_http_lua_upstream_get_primary_peers(lua_State * L); @@ -94,8 +94,8 @@ ngx_http_lua_upstream_create_module(lua_State * L) lua_pushcfunction(L, ngx_http_lua_upstream_get_servers); lua_setfield(L, -2, "get_servers"); - lua_pushcfunction(L, ngx_http_lua_upstream_add_server_to_upstream); - lua_setfield(L, -2, "add_server_to_upstream"); + lua_pushcfunction(L, ngx_http_lua_upstream_add_peer); + lua_setfield(L, -2, "add_peer"); lua_pushcfunction(L, ngx_http_lua_upstream_get_primary_peers); lua_setfield(L, -2, "get_primary_peers"); @@ -149,7 +149,7 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) } static int -ngx_http_lua_upstream_add_server_to_upstream(lua_State * L) +ngx_http_lua_upstream_add_peer(lua_State * L) { ngx_str_t host; ngx_http_upstream_server_t *us; diff --git a/t/sanity.t b/t/sanity.t index 6a23ed8..7bf3e03 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -660,7 +660,7 @@ nil location /t { content_by_lua ' local upstream = require "ngx.upstream" - local ok, err = upstream.add_server_to_upstream("foo", "127.0.0.1:8080", 1, 1, 1, false) + local ok, err = upstream.add_peer("foo", "127.0.0.1:8080", 1, 1, 1, false) if not ok then ngx.say(err) else From 095f50a9355b6c2d551d44971658b4fca4315c5c Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:24:34 -0400 Subject: [PATCH 12/32] Rename add_peer to add_upstream_peer --- README.md | 6 +++--- src/ngx_http_lua_upstream_module.c | 8 ++++---- t/sanity.t | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0af9c26..0f16ac5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Table of Contents * [Functions](#functions) * [get_upstreams](#get_upstreams) * [get_servers](#get_servers) - * [add_peer](#add_peer) + * [add_upstream_peer](#add_upstream_peer) * [remove_server_from_upstream](#remove_server_from_upstream) * [get_primary_peers](#get_primary_peers) * [get_backup_peers](#get_backup_peers) @@ -128,9 +128,9 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua [Back to TOC](#table-of-contents) -add_peer +add_upstream_peer ----------- -`syntax: ok, err = upstream.add_peer(upstream_name, ip:port, weight, max_fails, fail_timeout)` +`syntax: ok, err = upstream.add_upstream_peer(upstream_name, ip:port, weight, max_fails, fail_timeout)` The return values are a boolean denoting the success of the operation and an associated error if one occurs. diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 4e320c5..35b1596 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -23,7 +23,7 @@ static ngx_int_t ngx_http_lua_upstream_init(ngx_conf_t *cf); static int ngx_http_lua_upstream_create_module(lua_State * L); static int ngx_http_lua_upstream_get_upstreams(lua_State * L); static int ngx_http_lua_upstream_get_servers(lua_State * L); -static int ngx_http_lua_upstream_add_peer(lua_State * L); +static int ngx_http_lua_upstream_add_upstream_peer(lua_State * L); static ngx_http_upstream_main_conf_t * ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L); static int ngx_http_lua_upstream_get_primary_peers(lua_State * L); @@ -94,8 +94,8 @@ ngx_http_lua_upstream_create_module(lua_State * L) lua_pushcfunction(L, ngx_http_lua_upstream_get_servers); lua_setfield(L, -2, "get_servers"); - lua_pushcfunction(L, ngx_http_lua_upstream_add_peer); - lua_setfield(L, -2, "add_peer"); + lua_pushcfunction(L, ngx_http_lua_upstream_add_upstream_peer); + lua_setfield(L, -2, "add_upstream_peer"); lua_pushcfunction(L, ngx_http_lua_upstream_get_primary_peers); lua_setfield(L, -2, "get_primary_peers"); @@ -149,7 +149,7 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) } static int -ngx_http_lua_upstream_add_peer(lua_State * L) +ngx_http_lua_upstream_add_upstream_peer(lua_State * L) { ngx_str_t host; ngx_http_upstream_server_t *us; diff --git a/t/sanity.t b/t/sanity.t index 7bf3e03..098d172 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -660,7 +660,7 @@ nil location /t { content_by_lua ' local upstream = require "ngx.upstream" - local ok, err = upstream.add_peer("foo", "127.0.0.1:8080", 1, 1, 1, false) + local ok, err = upstream.add_upstream_peer("foo", "127.0.0.1:8080", 1, 1, 1, false) if not ok then ngx.say(err) else From 80075665827721bc813d171a3f45eecd61969685 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:29:10 -0400 Subject: [PATCH 13/32] suspecting race conditions --- t/sanity.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index 098d172..e356ace 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -660,7 +660,7 @@ nil location /t { content_by_lua ' local upstream = require "ngx.upstream" - local ok, err = upstream.add_upstream_peer("foo", "127.0.0.1:8080", 1, 1, 1, false) + local ok, err = upstream.add_upstream_peer("foo", "127.0.0.10", 1, 1, 1, false) if not ok then ngx.say(err) else @@ -696,7 +696,7 @@ nil --- response_body upstream foo: addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, name = 127.0.0.2, max_fails = 1 - addr = 127.0.0.1:8080, weight = 1, fail_timeout = 1, name = 127.0.0.1:8080, max_fails = 1 + addr = 127.0.0.10:80, weight = 1, fail_timeout = 1, name = 127.0.0.10, max_fails = 1 done --- no_error_log [error] From c201d4f4e2097ed522e4bfd6939abcc84ad55506 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:30:03 -0400 Subject: [PATCH 14/32] accurate debug logs --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 35b1596..71a78de 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -186,7 +186,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) fail_timeout = (time_t) luaL_checklong(L, 5); backup = lua_toboolean(L, 6); #if (NGX_DEBUG) - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout); + ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%s\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); #endif uscf = ngx_http_lua_upstream_find_upstream(L, &host); From 6f5c7a7bd2ea361d7c74d5f7c6e4dbc661cde5a4 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:36:14 -0400 Subject: [PATCH 15/32] try %d for bools --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 71a78de..9428593 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -186,7 +186,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) fail_timeout = (time_t) luaL_checklong(L, 5); backup = lua_toboolean(L, 6); #if (NGX_DEBUG) - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%s\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); + ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); #endif uscf = ngx_http_lua_upstream_find_upstream(L, &host); From 7eef0bb4459a1053d03860405f631931714a2459 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Fri, 15 Jul 2016 16:42:14 -0400 Subject: [PATCH 16/32] ok is probably for chumps --- t/sanity.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index e356ace..00c7652 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -661,7 +661,7 @@ nil content_by_lua ' local upstream = require "ngx.upstream" local ok, err = upstream.add_upstream_peer("foo", "127.0.0.10", 1, 1, 1, false) - if not ok then + if err then ngx.say(err) else local srvs, err = upstream.get_servers("foo") From 4253c5d09cf0f638761979d7ec045f7d2a7a68a7 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 14:43:31 -0400 Subject: [PATCH 17/32] ngx_http_lua_upstream_find_server should take ngx_url_t pointer --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 9428593..b04d0bf 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -645,7 +645,7 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) } static ngx_http_upstream_server_t* -ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t u) +ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t * u) { ngx_uint_t i, j; size_t len; From 621e9cd13bf5342cb7f372ed7c227650e714c306 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 14:44:27 -0400 Subject: [PATCH 18/32] Provide default values for upstream.add_upstream_peer() Only the upstream and HOST:IP are required. For everything else, we provide sane defaults and treat those arguments as optional. --- README.md | 2 +- src/ngx_http_lua_upstream_module.c | 33 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0f16ac5..af40201 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ The return value is an array-like Lua table. Each table entry is a hash-like Lua add_upstream_peer ----------- -`syntax: ok, err = upstream.add_upstream_peer(upstream_name, ip:port, weight, max_fails, fail_timeout)` +`syntax: ok, err = upstream.add_upstream_peer(upstream_name, ip:port, optional weight = 1, optional max_fails = 1, optional fail_timeout = 10, optional backup = false)` The return values are a boolean denoting the success of the operation and an associated error if one occurs. diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index b04d0bf..504d52e 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -156,16 +156,16 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_http_upstream_srv_conf_t *uscf; ngx_url_t u; ngx_http_request_t *r; - ngx_int_t weight, max_fails; - ngx_uint_t backup; - time_t fail_timeout; + ngx_int_t weight, max_fails = 1, 1; + ngx_uint_t backup = false; + time_t fail_timeout = 10; u_char *p; - if (lua_gettop(L) != 6) { + if ((lua_gettop(L) < 2) || (lua_gettop(L) > 6)) { /* * "upstream name", "host:port", "weight", "max_fails", "fail_timeout", "backup" */ - return luaL_error(L, "exactly 6 arguments expected"); + return luaL_error(L, "at least 2 arguments are required and as many as 6 are allowed"); } r = ngx_http_lua_get_request(L); @@ -177,14 +177,27 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) host.data = (u_char *) luaL_checklstring(L, 1, &host.len); - ngx_memzero(&u, sizeof(ngx_url_t)); + ngx_url_t u = { 0 }; + p = (u_char *) luaL_checklstring(L, 2, &u.url.len); u.default_port = 80; - weight = (ngx_int_t) luaL_checkint(L, 3); - max_fails = (ngx_int_t) luaL_checkint(L, 4); - fail_timeout = (time_t) luaL_checklong(L, 5); - backup = lua_toboolean(L, 6); + if (lua_gettop(L) >= 3) { + weight = (ngx_int_t) luaL_checkint(L, 3); + } + + if (lua_gettop(L) >= 4) { + max_fails = (ngx_int_t) luaL_checkint(L, 4); + } + + if (lua_gettop(L) >= 5) { + fail_timeout = (time_t) luaL_checklong(L, 5); + } + + if (lua_gettop(L) >= 6) { + backup = lua_toboolean(L, 6); + } + #if (NGX_DEBUG) ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); #endif From 8713b9729415019b51d88c3eb3e58e9eebfcab4b Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 14:50:02 -0400 Subject: [PATCH 19/32] method header fixup --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 504d52e..07d319e 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -33,7 +33,7 @@ static int ngx_http_lua_get_peer(lua_State *L, static ngx_http_upstream_srv_conf_t * ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host); static ngx_http_upstream_server_t* - ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t u); + ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t * u); static ngx_http_upstream_rr_peer_t * ngx_http_lua_upstream_lookup_peer(lua_State *L); static int ngx_http_lua_upstream_set_peer_down(lua_State * L); From 47b1c0d93e520d1ab6813da3da5607e2d6bfc333 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 16:35:04 -0400 Subject: [PATCH 20/32] misc --- src/ngx_http_lua_upstream_module.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 07d319e..f6dbb44 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -154,10 +154,11 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_str_t host; ngx_http_upstream_server_t *us; ngx_http_upstream_srv_conf_t *uscf; - ngx_url_t u; + ngx_url_t *u = { 0 }; ngx_http_request_t *r; - ngx_int_t weight, max_fails = 1, 1; - ngx_uint_t backup = false; + ngx_int_t weight = 1; + ngx_int_t max_fails = 1; + ngx_uint_t backup = 0; time_t fail_timeout = 10; u_char *p; @@ -177,8 +178,6 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) host.data = (u_char *) luaL_checklstring(L, 1, &host.len); - ngx_url_t u = { 0 }; - p = (u_char *) luaL_checklstring(L, 2, &u.url.len); u.default_port = 80; From 7bb736daaa7d4141281670531f6e5f3f61f76b3f Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 16:47:50 -0400 Subject: [PATCH 21/32] update test to check primary peers over servers --- t/sanity.t | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index 00c7652..13fb522 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -659,44 +659,24 @@ nil --- config location /t { content_by_lua ' + local ljson = require "ljson" local upstream = require "ngx.upstream" - local ok, err = upstream.add_upstream_peer("foo", "127.0.0.10", 1, 1, 1, false) + local ok, err = upstream.add_upstream_peer("foo", "127.0.0.10") if err then ngx.say(err) else - local srvs, err = upstream.get_servers("foo") + local peers, err = upstream.get_primary_peers("foo") if not srvs then ngx.say("failed to get servers in upstream foo") else - ngx.say("upstream foo:") - for _, srv in ipairs(srvs) do - local first = true - for k, v in pairs(srv) do - if first then - first = false - ngx.print(" ") - else - ngx.print(", ") - end - if type(v) == "table" then - ngx.print(k, " = {", concat(v, ", "), "}") - else - ngx.print(k, " = ", v) - end - end - ngx.print("\\n") - end + ngx.say(json.encode(peers)) end - ngx.say("done") end '; } --- request GET /t --- response_body -upstream foo: - addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, name = 127.0.0.2, max_fails = 1 - addr = 127.0.0.10:80, weight = 1, fail_timeout = 1, name = 127.0.0.10, max_fails = 1 -done +[{"addr": "127.0.0.2:80", "weight": 1, "fail_timeout": 10, "name": "127.0.0.2", "max_fails": 1}, {"addr": "127.0.0.10:80", "weight": 1, "fail_timeout": 1, "name": "127.0.0.10", "max_fails": 1}] --- no_error_log [error] From 7b77d6b6a90a273fe894d69e8588892fbfc005f6 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 16:48:09 -0400 Subject: [PATCH 22/32] misc --- src/ngx_http_lua_upstream_module.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index f6dbb44..b37abc3 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -154,7 +154,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_str_t host; ngx_http_upstream_server_t *us; ngx_http_upstream_srv_conf_t *uscf; - ngx_url_t *u = { 0 }; + ngx_url_t u; ngx_http_request_t *r; ngx_int_t weight = 1; ngx_int_t max_fails = 1; @@ -178,6 +178,8 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) host.data = (u_char *) luaL_checklstring(L, 1, &host.len); + ngx_memzero(&u, sizeof(ngx_url_t)); + p = (u_char *) luaL_checklstring(L, 2, &u.url.len); u.default_port = 80; @@ -211,7 +213,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) u.url.data = ngx_pcalloc(uscf->servers->pool, u.url.len+1); ngx_memcpy(u.url.data, p, u.url.len); - if (ngx_http_lua_upstream_find_server(uscf, u) != NULL) { + if (ngx_http_lua_upstream_find_server(uscf, &u) != NULL) { lua_pushnil(L); lua_pushliteral(L, "server already exists\n"); return 2; From 140bb43cef2dbb1b2584510eea013cc81e341c68 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 16:49:48 -0400 Subject: [PATCH 23/32] attempt to 0 out u correctly --- src/ngx_http_lua_upstream_module.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index b37abc3..3aaa5ec 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -154,7 +154,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_str_t host; ngx_http_upstream_server_t *us; ngx_http_upstream_srv_conf_t *uscf; - ngx_url_t u; + ngx_url_t u = { 0 }; ngx_http_request_t *r; ngx_int_t weight = 1; ngx_int_t max_fails = 1; @@ -178,8 +178,6 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) host.data = (u_char *) luaL_checklstring(L, 1, &host.len); - ngx_memzero(&u, sizeof(ngx_url_t)); - p = (u_char *) luaL_checklstring(L, 2, &u.url.len); u.default_port = 80; From 49078fa34aa7539400ff49831c594c65fb9e419f Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:21:34 -0400 Subject: [PATCH 24/32] Add peers, not servers --- src/ngx_http_lua_upstream_module.c | 90 +++++++++++++++++++----------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 3aaa5ec..2ca2154 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -151,16 +151,22 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) static int ngx_http_lua_upstream_add_upstream_peer(lua_State * L) { - ngx_str_t host; - ngx_http_upstream_server_t *us; - ngx_http_upstream_srv_conf_t *uscf; - ngx_url_t u = { 0 }; ngx_http_request_t *r; + + ngx_http_upstream_srv_conf_t *uscf; + ngx_http_upstream_server_t *us; + + ngx_http_upstream_rr_peers_t *peers; + ngx_http_upstream_rr_peer_t *peer, *last; + + u_char url; + + ngx_url_t upstream; + ngx_str_t host; ngx_int_t weight = 1; ngx_int_t max_fails = 1; - ngx_uint_t backup = 0; time_t fail_timeout = 10; - u_char *p; + ngx_uint_t backup = 0; if ((lua_gettop(L) < 2) || (lua_gettop(L) > 6)) { /* @@ -172,14 +178,14 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) r = ngx_http_lua_get_request(L); if (r == NULL) { lua_pushnil(L); - lua_pushliteral(L, "get request error \n"); + lua_pushliteral(L, "could not retrieve request\n"); return 2; } host.data = (u_char *) luaL_checklstring(L, 1, &host.len); - p = (u_char *) luaL_checklstring(L, 2, &u.url.len); - u.default_port = 80; + url = (u_char *) luaL_checklstring(L, 2, &upstream.url.len); + upstream.default_port = 80; if (lua_gettop(L) >= 3) { weight = (ngx_int_t) luaL_checkint(L, 3); @@ -208,41 +214,57 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) return 2; } - u.url.data = ngx_pcalloc(uscf->servers->pool, u.url.len+1); - ngx_memcpy(u.url.data, p, u.url.len); + peers = uscf->peer.data; - if (ngx_http_lua_upstream_find_server(uscf, &u) != NULL) { - lua_pushnil(L); - lua_pushliteral(L, "server already exists\n"); - return 2; - } else { - // validate URL - if (ngx_parse_url(uscf->servers->pool, &u) != NGX_OK) { - if (u.err) { - lua_pushnil(L); - lua_pushliteral(L, "url parser error\n"); - return 2; - } + for (peer = peers->peer, last = peer; peer; peer = peer->next){ + if (url.len == peer->name.len && ngx_strcmp(url.data, peer->name.data, peer->name.len) == 0) { + lua_pushnil(L); + lua_pushliteral(L, "server already exists\n"); + return 2; } + last = peer; + } - us = ngx_array_push(uscf->servers); - if (us == NULL) { + upstream.url.data = ngx_pcalloc(r->pool, upstream.url.len + 1); + ngx_memcpy(upstream.url.data, url, upstream.url.len); + + // validate URL + if (ngx_parse_url(r->pool, &upstream) != NGX_OK) { + if (u.err) { lua_pushnil(L); - lua_pushliteral(L, "could not append server to upstream\n"); + lua_pushliteral(L, "url parser error\n"); return 2; } + } - ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); + last->next = ngx_pcalloc(); - us->name = u.url; - us->addrs = u.addrs; - us->naddrs = u.naddrs; - us->weight = weight; - us->max_fails = max_fails; - us->fail_timeout = fail_timeout; - us->backup = backup; + if (last->next == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "failed to allocate memory\n"); + return 2; } + last->next->name = upstream.url; + last->next->server = upstream.url; + last->next->sockaddr = upstream.addrs[0].sockaddr; + last->next->socklen = upstream.addrs[0].socklen; + + last->next->weight = weight; + last->next->effective_weight = weight; + last->next->current_weight = weight; + + last->next->max_fails = max_fails; + + last->next->fail_timeout = fail_timeout; + + last->next->backup = backup; + + peers->number++; + peers->total_weight += last->next->weight; + peers->single = (peers->number == 1); + peers->weighted = (peers->total_weight != peers->number); + lua_pushboolean(L, 1); return 1; } From ac2e4d2b7929023e7f80394189286cc02b8c40a7 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:23:28 -0400 Subject: [PATCH 25/32] fixup --- src/ngx_http_lua_upstream_module.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 2ca2154..8011c1c 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -184,6 +184,8 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) host.data = (u_char *) luaL_checklstring(L, 1, &host.len); + ngx_memzero(&upstream, sizeof(ngx_url_t)); + url = (u_char *) luaL_checklstring(L, 2, &upstream.url.len); upstream.default_port = 80; From da070e040bd95690d274e624b807add4fc01860a Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:27:06 -0400 Subject: [PATCH 26/32] should be a ptr --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 8011c1c..8932e4d 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -159,7 +159,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_http_upstream_rr_peers_t *peers; ngx_http_upstream_rr_peer_t *peer, *last; - u_char url; + u_char *url; ngx_url_t upstream; ngx_str_t host; From 66326cff122022f66e9ac7c945df6629c6d6675b Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:27:48 -0400 Subject: [PATCH 27/32] p is gone --- src/ngx_http_lua_upstream_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 8932e4d..374b880 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -206,7 +206,7 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) } #if (NGX_DEBUG) - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); + ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, url, weight, max_fails, fail_timeout, backup); #endif uscf = ngx_http_lua_upstream_find_upstream(L, &host); From 3a8db013644c7b5cd8566550217969e0888d3f66 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:48:24 -0400 Subject: [PATCH 28/32] remove unused var --- src/ngx_http_lua_upstream_module.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 374b880..c2fc6d9 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -154,7 +154,6 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_http_request_t *r; ngx_http_upstream_srv_conf_t *uscf; - ngx_http_upstream_server_t *us; ngx_http_upstream_rr_peers_t *peers; ngx_http_upstream_rr_peer_t *peer, *last; From 5d032935e6a6b622fc39cc4865fde73dad935bf8 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:49:03 -0400 Subject: [PATCH 29/32] say error in test --- t/sanity.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index 13fb522..2fbd96f 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -666,8 +666,8 @@ nil ngx.say(err) else local peers, err = upstream.get_primary_peers("foo") - if not srvs then - ngx.say("failed to get servers in upstream foo") + if not peers then + ngx.say("failed to get servers in upstream foo: " .. err) else ngx.say(json.encode(peers)) end From 0d162b89bf963f1ccd1f1da27455ff3f71b484d2 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 17:49:20 -0400 Subject: [PATCH 30/32] better wording --- t/sanity.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index 2fbd96f..4d9ebb7 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -667,7 +667,7 @@ nil else local peers, err = upstream.get_primary_peers("foo") if not peers then - ngx.say("failed to get servers in upstream foo: " .. err) + ngx.say("failed to get primary peers from upstream foo: " .. err) else ngx.say(json.encode(peers)) end From d825697261779dce7de8fc6d91e9ade909409284 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 18:25:17 -0400 Subject: [PATCH 31/32] lolidunno --- src/ngx_http_lua_upstream_module.c | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index c2fc6d9..d677853 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -151,21 +151,21 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) static int ngx_http_lua_upstream_add_upstream_peer(lua_State * L) { - ngx_http_request_t *r; + ngx_http_request_t *r; - ngx_http_upstream_srv_conf_t *uscf; + ngx_http_upstream_srv_conf_t *us; - ngx_http_upstream_rr_peers_t *peers; - ngx_http_upstream_rr_peer_t *peer, *last; + ngx_http_upstream_rr_peers_t *peers; + ngx_http_upstream_rr_peer_t *peer, *last; - u_char *url; + u_char *url; - ngx_url_t upstream; - ngx_str_t host; - ngx_int_t weight = 1; - ngx_int_t max_fails = 1; - time_t fail_timeout = 10; - ngx_uint_t backup = 0; + ngx_url_t upstream; + ngx_str_t host; + ngx_int_t weight = 1; + ngx_int_t max_fails = 1; + time_t fail_timeout = 10; + ngx_uint_t backup = 0; if ((lua_gettop(L) < 2) || (lua_gettop(L) > 6)) { /* @@ -208,17 +208,17 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d,%d\n", __FILE__, __FUNCTION__, host.data, url, weight, max_fails, fail_timeout, backup); #endif - uscf = ngx_http_lua_upstream_find_upstream(L, &host); - if (uscf == NULL) { + us = ngx_http_lua_upstream_find_upstream(L, &host); + if (us == NULL) { lua_pushnil(L); lua_pushliteral(L, "upstream not found\n"); return 2; } - peers = uscf->peer.data; + peers = us->peer.data; for (peer = peers->peer, last = peer; peer; peer = peer->next){ - if (url.len == peer->name.len && ngx_strcmp(url.data, peer->name.data, peer->name.len) == 0) { + if (len(url) == peer->name.len && ngx_strncmp(url, peer->name.data, peer->name.len) == 0) { lua_pushnil(L); lua_pushliteral(L, "server already exists\n"); return 2; @@ -231,14 +231,14 @@ ngx_http_lua_upstream_add_upstream_peer(lua_State * L) // validate URL if (ngx_parse_url(r->pool, &upstream) != NGX_OK) { - if (u.err) { + if (upstream.err) { lua_pushnil(L); lua_pushliteral(L, "url parser error\n"); return 2; } } - last->next = ngx_pcalloc(); + last->next = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peer_t)); if (last->next == NULL) { lua_pushnil(L); From 389ad8cdc012de4f05ac1af7d4802616ab565b46 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 18 Jul 2016 18:44:35 -0400 Subject: [PATCH 32/32] remove find_server --- src/ngx_http_lua_upstream_module.c | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index d677853..048d6d0 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -32,8 +32,6 @@ static int ngx_http_lua_get_peer(lua_State *L, ngx_http_upstream_rr_peer_t *peer, ngx_uint_t id); static ngx_http_upstream_srv_conf_t * ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host); -static ngx_http_upstream_server_t* - ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t * u); static ngx_http_upstream_rr_peer_t * ngx_http_lua_upstream_lookup_peer(lua_State *L); static int ngx_http_lua_upstream_set_peer_down(lua_State * L); @@ -679,33 +677,6 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) return NULL; } -static ngx_http_upstream_server_t* -ngx_http_lua_upstream_find_server(ngx_http_upstream_srv_conf_t * us, ngx_url_t * u) -{ - ngx_uint_t i, j; - size_t len; - ngx_http_upstream_server_t *server = NULL; - - if (us->servers == NULL || us->servers->nelts == 0) { - return NULL; - } - - server = us->servers->elts; - - for (i = 0; i < us->servers->nelts; ++i) { - for (j = 0; j < server[i].naddrs; ++j) { - len = server[i].addrs[j].name.len; - - if (len == u.url.len - && ngx_memcmp(u.url.data, server[i].addrs[j].name.data, u.url.len) == 0) { - return &server[i]; - } - } - } - - return NULL; -} - static int ngx_http_lua_upstream_current_upstream_name(lua_State *L) {