Skip to content

add lua function for update upstream server's weight #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/ngx_http_lua_upstream_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ 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 int ngx_http_lua_upstream_set_peer_weight(lua_State * L);
static int ngx_http_lua_upstream_set_peer_current_weight(lua_State * L);
static int ngx_http_lua_upstream_set_peer_effective_weight(lua_State * L);


static ngx_http_module_t ngx_http_lua_upstream_ctx = {
Expand Down Expand Up @@ -102,6 +105,15 @@ ngx_http_lua_upstream_create_module(lua_State * L)
lua_pushcfunction(L, ngx_http_lua_upstream_current_upstream_name);
lua_setfield(L, -2, "current_upstream_name");

lua_pushcfunction(L, ngx_http_lua_upstream_set_peer_weight);
lua_setfield(L, -2, "set_peer_weight");

lua_pushcfunction(L, ngx_http_lua_upstream_set_peer_current_weight);
lua_setfield(L, -2, "set_peer_current_weight");

lua_pushcfunction(L, ngx_http_lua_upstream_set_peer_effective_weight);
lua_setfield(L, -2, "set_peer_effective_weight");

return 1;
}

Expand Down Expand Up @@ -352,6 +364,66 @@ ngx_http_lua_upstream_set_peer_down(lua_State * L)
return 1;
}

static int
ngx_http_lua_upstream_set_peer_weight(lua_State * L)
{
ngx_http_upstream_rr_peer_t *peer;

if (lua_gettop(L) != 4) {
return luaL_error(L, "exactly 4 arguments expected");
}

peer = ngx_http_lua_upstream_lookup_peer(L);
if (peer == NULL) {
return 2;
}

peer->weight = lua_tointeger(L, 4);

lua_pushboolean(L, 1);
return 1;
}

static int
ngx_http_lua_upstream_set_peer_current_weight(lua_State * L)
{
ngx_http_upstream_rr_peer_t *peer;

if (lua_gettop(L) != 4) {
return luaL_error(L, "exactly 4 arguments expected");
}

peer = ngx_http_lua_upstream_lookup_peer(L);
if (peer == NULL) {
return 2;
}

peer->current_weight = lua_tointeger(L, 4);

lua_pushboolean(L, 1);
return 1;
}

static int
ngx_http_lua_upstream_set_peer_effective_weight(lua_State * L)
{
ngx_http_upstream_rr_peer_t *peer;

if (lua_gettop(L) != 4) {
return luaL_error(L, "exactly 4 arguments expected");
}

peer = ngx_http_lua_upstream_lookup_peer(L);
if (peer == NULL) {
return 2;
}

peer->effective_weight = lua_tointeger(L, 4);

lua_pushboolean(L, 1);
return 1;
}


static ngx_http_upstream_rr_peer_t *
ngx_http_lua_upstream_lookup_peer(lua_State *L)
Expand Down