@@ -35,7 +35,7 @@ static ngx_http_upstream_rr_peer_t *
35
35
ngx_http_lua_upstream_lookup_peer (lua_State * L );
36
36
static int ngx_http_lua_upstream_set_peer_down (lua_State * L );
37
37
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 );
39
39
40
40
static ngx_http_module_t ngx_http_lua_upstream_ctx = {
41
41
NULL , /* preconfiguration */
@@ -102,6 +102,9 @@ ngx_http_lua_upstream_create_module(lua_State * L)
102
102
lua_pushcfunction (L , ngx_http_lua_upstream_current_upstream_name );
103
103
lua_setfield (L , -2 , "current_upstream_name" );
104
104
105
+ lua_pushcfunction (L , ngx_http_lua_upstream_set_peer_weight );
106
+ lua_setfield (L , -2 , "set_peer_weight" );
107
+
105
108
return 1 ;
106
109
}
107
110
@@ -356,6 +359,50 @@ ngx_http_lua_upstream_set_peer_down(lua_State * L)
356
359
return 1 ;
357
360
}
358
361
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
+ }
359
406
360
407
static ngx_http_upstream_rr_peer_t *
361
408
ngx_http_lua_upstream_lookup_peer (lua_State * L )
0 commit comments