This repository was archived by the owner on Jan 18, 2023. It is now read-only.
forked from openresty/lua-upstream-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] Add and remove servers from upstreams #1
Open
wfarr
wants to merge
32
commits into
shopify
Choose a base branch
from
add-and-remove-servers-from-upstreams
base: shopify
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
cc57776
A little bit of README Driven Development
wfarr 508c71e
Spike out upstream.add_server_to_upstream()
wfarr 63b8d44
minor fixes
wfarr c2f2863
test things
wfarr 30047af
get_servers is a function in upstream module
wfarr 1874f9f
typo
wfarr c8011bc
test update
wfarr 4b503ce
typo
wfarr c17fb18
extra newline
wfarr e75bf46
correct retval
wfarr 75e87a6
rename add_server_to_upstream to add_peer
wfarr 095f50a
Rename add_peer to add_upstream_peer
wfarr 8007566
suspecting race conditions
wfarr c201d4f
accurate debug logs
wfarr 6f5c7a7
try %d for bools
wfarr 7eef0bb
ok is probably for chumps
wfarr 4253c5d
ngx_http_lua_upstream_find_server should take ngx_url_t pointer
wfarr 621e9cd
Provide default values for upstream.add_upstream_peer()
wfarr 8713b97
method header fixup
wfarr 47b1c0d
misc
wfarr 7bb736d
update test to check primary peers over servers
wfarr 7b77d6b
misc
wfarr 140bb43
attempt to 0 out u correctly
wfarr 49078fa
Add peers, not servers
wfarr ac2e4d2
fixup
wfarr da070e0
should be a ptr
wfarr 66326cf
p is gone
wfarr 3a8db01
remove unused var
wfarr 5d03293
say error in test
wfarr 0d162b8
better wording
wfarr d825697
lolidunno
wfarr 389ad8c
remove find_server
wfarr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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_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); | ||
|
|
@@ -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 */ | ||
|
|
@@ -90,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_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"); | ||
|
|
||
|
|
@@ -141,6 +148,92 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) | |
| return 1; | ||
| } | ||
|
|
||
| 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; | ||
| 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,%d\n", __FILE__, __FUNCTION__, host.data, p, weight, max_fails, fail_timeout, backup); | ||
| #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 +644,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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be some sane defaults here.