Skip to content

Commit f5afcb2

Browse files
author
Pablo Fischer
committed
Sync with latest sync from upstream and re-apply PR openresty#5
1 parent 6ebcda3 commit f5afcb2

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/ngx_http_lua_upstream_module.c

+48-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static ngx_http_upstream_rr_peer_t *
3535
ngx_http_lua_upstream_lookup_peer(lua_State *L);
3636
static int ngx_http_lua_upstream_set_peer_down(lua_State * L);
3737
static int ngx_http_lua_upstream_current_upstream_name(lua_State *L);
38-
38+
static int ngx_http_lua_upstream_set_peer_weight(lua_State * L);
3939

4040
static ngx_http_module_t ngx_http_lua_upstream_ctx = {
4141
NULL, /* preconfiguration */
@@ -102,6 +102,9 @@ ngx_http_lua_upstream_create_module(lua_State * L)
102102
lua_pushcfunction(L, ngx_http_lua_upstream_current_upstream_name);
103103
lua_setfield(L, -2, "current_upstream_name");
104104

105+
lua_pushcfunction(L, ngx_http_lua_upstream_set_peer_weight);
106+
lua_setfield(L, -2, "set_peer_weight");
107+
105108
return 1;
106109
}
107110

@@ -356,6 +359,50 @@ ngx_http_lua_upstream_set_peer_down(lua_State * L)
356359
return 1;
357360
}
358361

362+
static int
363+
ngx_http_lua_upstream_set_peer_weight(lua_State * L)
364+
{
365+
ngx_http_upstream_rr_peer_t *peer;
366+
ngx_str_t host;
367+
ngx_http_upstream_srv_conf_t *us;
368+
ngx_http_upstream_rr_peers_t *peers;
369+
370+
if (lua_gettop(L) != 4) {
371+
return luaL_error(L, "exactly 4 arguments expected");
372+
}
373+
374+
peer = ngx_http_lua_upstream_lookup_peer(L);
375+
if (peer == NULL) {
376+
return 2;
377+
}
378+
379+
int new_weight = (int)lua_tointeger(L, 4);
380+
if (new_weight < 1){
381+
lua_pushnil(L);
382+
lua_pushliteral(L, "ilegal weight");
383+
return 2;
384+
}
385+
386+
387+
int diff = new_weight - peer->weight;
388+
peer->weight = new_weight;
389+
peer->effective_weight = new_weight;
390+
peer->current_weight += diff;
391+
392+
// find upstream, in order to update weighted & total_weight
393+
host.data = (u_char *) luaL_checklstring(L, 1, &host.len);
394+
us = ngx_http_lua_upstream_find_upstream(L, &host);
395+
if (us == NULL) {
396+
lua_pushnil(L);
397+
lua_pushliteral(L, "upstream not found");
398+
return 2;
399+
}
400+
peers = us->peer.data;
401+
peers->total_weight += diff;
402+
peers->weighted = (peers->total_weight == peers->number);
403+
404+
return 1;
405+
}
359406

360407
static ngx_http_upstream_rr_peer_t *
361408
ngx_http_lua_upstream_lookup_peer(lua_State *L)

t/sanity.t

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env perl
2+
# Author: Pablo Fischer <[email protected]>
3+
# Date : 08/05/2018
14
# vim:set ft= ts=4 sw=4 et fdm=marker:
25

36
use Test::Nginx::Socket::Lua;
@@ -663,3 +666,40 @@ GET /test
663666
nil
664667
--- no_error_log
665668
[error]
669+
670+
671+
672+
=== TEST 19: set peer weight
673+
--- http_config
674+
$TEST_NGINX_MY_INIT_CONFIG
675+
upstream bar {
676+
server 127.0.0.2 weight=2;
677+
server 127.0.0.3 weight=3;
678+
server 127.0.0.4 fail_timeout=23 weight=7 max_fails=200 backup;
679+
}
680+
--- config
681+
location /t {
682+
content_by_lua '
683+
local upstream = require "ngx.upstream"
684+
local ljson = require "ljson"
685+
local u = "bar"
686+
local ok, err = upstream.set_peer_weight(u, false, 0, 1)
687+
if not ok then
688+
ngx.say("failed to set peer weight: ", err)
689+
return
690+
end
691+
692+
local peers, err = upstream.get_primary_peers(u)
693+
if not peers then
694+
ngx.say("failed to get peers: ", err)
695+
return
696+
end
697+
ngx.say(ljson.encode(peers))
698+
';
699+
}
700+
--- request
701+
GET /t
702+
--- response_body
703+
[{"current_weight":-1,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"127.0.0.2:80","weight":1},{"current_weight":0,"effective_weight":3,"fail_timeout":10,"fails":0,"id":1,"max_fails":1,"name":"127.0.0.3:80","weight":3}]
704+
--- no_error_log
705+
[error]

0 commit comments

Comments
 (0)