@@ -19,6 +19,8 @@ ngx_stream_dynamic_upstream_lua_get_upstreams(lua_State *L);
1919static int
2020ngx_stream_dynamic_upstream_lua_get_peers (lua_State * L );
2121static int
22+ ngx_stream_dynamic_upstream_lua_get_peers_locked (lua_State * L );
23+ static int
2224ngx_stream_dynamic_upstream_lua_get_primary_peers (lua_State * L );
2325static int
2426ngx_stream_dynamic_upstream_lua_get_backup_peers (lua_State * L );
@@ -51,6 +53,9 @@ ngx_stream_dynamic_upstream_lua_create_module(lua_State *L)
5153 lua_pushcfunction (L , ngx_stream_dynamic_upstream_lua_get_peers );
5254 lua_setfield (L , -2 , "get_peers" );
5355
56+ lua_pushcfunction (L , ngx_stream_dynamic_upstream_lua_get_peers_locked );
57+ lua_setfield (L , -2 , "get_peers_locked" );
58+
5459 lua_pushcfunction (L , ngx_stream_dynamic_upstream_lua_get_primary_peers );
5560 lua_setfield (L , -2 , "get_primary_peers" );
5661
@@ -115,6 +120,7 @@ ngx_dynamic_upstream_get(lua_State *L, ngx_dynamic_upstream_op_t *op)
115120
116121static const int PRIMARY = 1 ;
117122static const int BACKUP = 2 ;
123+ static const int LOCK = 4 ;
118124
119125
120126static void
@@ -127,7 +133,9 @@ ngx_dynamic_upstream_lua_create_response(ngx_stream_upstream_rr_peers_t *primary
127133
128134 backup = primary -> next ;
129135
130- ngx_stream_upstream_rr_peers_rlock (primary );
136+ if (flags & LOCK ) {
137+ ngx_stream_upstream_rr_peers_rlock (primary );
138+ }
131139
132140 lua_newtable (L );
133141
@@ -139,6 +147,10 @@ ngx_dynamic_upstream_lua_create_response(ngx_stream_upstream_rr_peers_t *primary
139147 for (peer = peers -> peer ; peer ; peer = peer -> next , ++ i ) {
140148 lua_newtable (L );
141149
150+ lua_pushlstring (L , (char * ) peer -> server .data ,
151+ peer -> server .len );
152+ lua_setfield (L , -2 , "server" );
153+
142154 lua_pushlstring (L , (char * ) peer -> name .data ,
143155 peer -> name .len );
144156 lua_setfield (L , -2 , "name" );
@@ -171,7 +183,9 @@ ngx_dynamic_upstream_lua_create_response(ngx_stream_upstream_rr_peers_t *primary
171183 }
172184 }
173185
174- ngx_stream_upstream_rr_peers_unlock (primary );
186+ if (flags & LOCK ) {
187+ ngx_stream_upstream_rr_peers_unlock (primary );
188+ }
175189}
176190
177191
@@ -191,6 +205,7 @@ ngx_stream_dynamic_upstream_lua_op_defaults(lua_State *L,
191205 op -> verbose = 0 ;
192206 op -> backup = 0 ;
193207 op -> op_param = NGX_DYNAMIC_UPSTEAM_OP_PARAM_STREAM ;
208+ op -> op_param |= NGX_DYNAMIC_UPSTEAM_OP_PARAM_RESOLVE ;
194209
195210 op -> upstream .data = (u_char * ) luaL_checklstring (L , 1 , & op -> upstream .len );
196211}
@@ -219,19 +234,20 @@ ngx_stream_dynamic_upstream_lua_op(lua_State *L, ngx_dynamic_upstream_op_t *op,
219234 return ngx_stream_dynamic_upstream_lua_error (L , "upstream not found" );
220235 }
221236
222- if (op -> op & (NGX_DYNAMIC_UPSTEAM_OP_ADD | NGX_DYNAMIC_UPSTEAM_OP_REMOVE )) {
223- if (uscf -> shm_zone == NULL ) {
237+ if (op -> op != NGX_DYNAMIC_UPSTEAM_OP_LIST ) {
238+ if (flags & LOCK ) {
239+ rc = ngx_dynamic_upstream_stream_op (ngx_http_lua_get_request (L )->
240+ connection -> log , op , uscf );
241+ if (rc != NGX_OK && rc != NGX_AGAIN ) {
242+ return ngx_stream_dynamic_upstream_lua_error (L ,
243+ op -> err );
244+ }
245+ } else {
224246 return ngx_stream_dynamic_upstream_lua_error (L ,
225- "shared zone segment is not defined in upstream " );
247+ "locked operations can be used only with readonly functions " );
226248 }
227249 }
228250
229- rc = ngx_dynamic_upstream_stream_op (ngx_http_lua_get_request (L )->
230- connection -> log , op , uscf );
231- if (rc != NGX_OK ) {
232- return ngx_stream_dynamic_upstream_lua_error (L , "internal error" );
233- }
234-
235251 lua_pushboolean (L , 1 );
236252
237253 if (op -> verbose ) {
@@ -298,6 +314,21 @@ ngx_stream_dynamic_upstream_lua_get_peers(lua_State *L)
298314 ngx_stream_dynamic_upstream_lua_op_defaults (L , & op ,
299315 NGX_DYNAMIC_UPSTEAM_OP_LIST );
300316 op .verbose = 1 ;
317+ return ngx_stream_dynamic_upstream_lua_op (L , & op , PRIMARY |BACKUP |LOCK );
318+ }
319+
320+
321+ static int
322+ ngx_stream_dynamic_upstream_lua_get_peers_locked (lua_State * L )
323+ {
324+ ngx_dynamic_upstream_op_t op ;
325+ if (lua_gettop (L ) != 1 ) {
326+ return ngx_stream_dynamic_upstream_lua_error (L ,
327+ "exactly one argument expected" );
328+ }
329+ ngx_stream_dynamic_upstream_lua_op_defaults (L , & op ,
330+ NGX_DYNAMIC_UPSTEAM_OP_LIST );
331+ op .verbose = 1 ;
301332 return ngx_stream_dynamic_upstream_lua_op (L , & op , PRIMARY |BACKUP );
302333}
303334
@@ -313,7 +344,7 @@ ngx_stream_dynamic_upstream_lua_get_primary_peers(lua_State *L)
313344 ngx_stream_dynamic_upstream_lua_op_defaults (L , & op ,
314345 NGX_DYNAMIC_UPSTEAM_OP_LIST );
315346 op .verbose = 1 ;
316- return ngx_stream_dynamic_upstream_lua_op (L , & op , PRIMARY );
347+ return ngx_stream_dynamic_upstream_lua_op (L , & op , PRIMARY | LOCK );
317348}
318349
319350
@@ -328,7 +359,7 @@ ngx_stream_dynamic_upstream_lua_get_backup_peers(lua_State *L)
328359 ngx_stream_dynamic_upstream_lua_op_defaults (L , & op ,
329360 NGX_DYNAMIC_UPSTEAM_OP_LIST );
330361 op .verbose = 1 ;
331- return ngx_stream_dynamic_upstream_lua_op (L , & op , BACKUP );
362+ return ngx_stream_dynamic_upstream_lua_op (L , & op , BACKUP | LOCK );
332363}
333364
334365
@@ -350,7 +381,7 @@ ngx_stream_dynamic_upstream_lua_set_peer_down(lua_State *L)
350381
351382 op .server .data = (u_char * ) luaL_checklstring (L , 2 , & op .server .len );
352383
353- return ngx_stream_dynamic_upstream_lua_op (L , & op , 0 );
384+ return ngx_stream_dynamic_upstream_lua_op (L , & op , LOCK );
354385}
355386
356387
@@ -372,7 +403,7 @@ ngx_stream_dynamic_upstream_lua_set_peer_up(lua_State *L)
372403
373404 op .server .data = (u_char * ) luaL_checklstring (L , 2 , & op .server .len );
374405
375- return ngx_stream_dynamic_upstream_lua_op (L , & op , 0 );
406+ return ngx_stream_dynamic_upstream_lua_op (L , & op , LOCK );
376407}
377408
378409
@@ -393,7 +424,7 @@ ngx_stream_dynamic_upstream_lua_add_peer_impl(lua_State *L, int backup)
393424
394425 op .backup = backup ;
395426
396- return ngx_stream_dynamic_upstream_lua_op (L , & op , 0 );
427+ return ngx_stream_dynamic_upstream_lua_op (L , & op , LOCK );
397428}
398429
399430
@@ -426,7 +457,7 @@ ngx_stream_dynamic_upstream_lua_remove_peer(lua_State *L)
426457
427458 op .server .data = (u_char * ) luaL_checklstring (L , 2 , & op .server .len );
428459
429- return ngx_stream_dynamic_upstream_lua_op (L , & op , 0 );
460+ return ngx_stream_dynamic_upstream_lua_op (L , & op , LOCK );
430461}
431462
432463
@@ -490,5 +521,5 @@ ngx_stream_dynamic_upstream_lua_update_peer(lua_State *L)
490521
491522 ngx_stream_dynamic_upstream_lua_update_peer_parse_params (L , & op );
492523
493- return ngx_stream_dynamic_upstream_lua_op (L , & op , 0 );
524+ return ngx_stream_dynamic_upstream_lua_op (L , & op , LOCK );
494525}
0 commit comments