@@ -23,6 +23,7 @@ static ngx_int_t ngx_http_lua_upstream_init(ngx_conf_t *cf);
23
23
static int ngx_http_lua_upstream_create_module (lua_State * L );
24
24
static int ngx_http_lua_upstream_get_upstreams (lua_State * L );
25
25
static int ngx_http_lua_upstream_get_servers (lua_State * L );
26
+ static int ngx_http_lua_upstream_add_server_to_upstream (lua_State * L );
26
27
static ngx_http_upstream_main_conf_t *
27
28
ngx_http_lua_upstream_get_upstream_main_conf (lua_State * L );
28
29
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,
31
32
ngx_http_upstream_rr_peer_t * peer , ngx_uint_t id );
32
33
static ngx_http_upstream_srv_conf_t *
33
34
ngx_http_lua_upstream_find_upstream (lua_State * L , ngx_str_t * host );
35
+ static ngx_http_upstream_server_t *
36
+ ngx_http_lua_upstream_find_server (ngx_http_upstream_srv_conf_t * us , ngx_url_t u );
34
37
static ngx_http_upstream_rr_peer_t *
35
38
ngx_http_lua_upstream_lookup_peer (lua_State * L );
36
39
static int ngx_http_lua_upstream_set_peer_down (lua_State * L );
37
40
static int ngx_http_lua_upstream_current_upstream_name (lua_State * L );
38
41
39
42
43
+
40
44
static ngx_http_module_t ngx_http_lua_upstream_ctx = {
41
45
NULL , /* preconfiguration */
42
46
ngx_http_lua_upstream_init , /* postconfiguration */
@@ -141,6 +145,92 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L)
141
145
return 1 ;
142
146
}
143
147
148
+ static int
149
+ ngx_http_lua_upstream_add_server_to_upstream (lua_State * L )
150
+ {
151
+ ngx_str_t host ;
152
+ ngx_http_upstream_server_t * us ;
153
+ ngx_http_upstream_srv_conf_t * uscf ;
154
+ ngx_url_t u ;
155
+ ngx_http_request_t * r ;
156
+ ngx_int_t weight , max_fails ;
157
+ ngx_uint_t backup ;
158
+ time_t fail_timeout ;
159
+ u_char * p ;
160
+
161
+ if (lua_gettop (L ) != 6 ) {
162
+ /*
163
+ * "upstream name", "host:port", "weight", "max_fails", "fail_timeout", "backup"
164
+ */
165
+ return luaL_error (L , "exactly 6 arguments expected" );
166
+ }
167
+
168
+ r = ngx_http_lua_get_request (L );
169
+ if (r == NULL ) {
170
+ lua_pushnil (L );
171
+ lua_pushliteral (L , "get request error \n" );
172
+ return 2 ;
173
+ }
174
+
175
+ host .data = (u_char * ) luaL_checklstring (L , 1 , & host .len );
176
+
177
+ ngx_memzero (& u , sizeof (ngx_url_t ));
178
+ p = (u_char * ) luaL_checklstring (L , 2 , & u .url .len );
179
+ u .default_port = 80 ;
180
+
181
+ weight = (ngx_int_t ) luaL_checkint (L , 3 );
182
+ max_fails = (ngx_int_t ) luaL_checkint (L , 4 );
183
+ fail_timeout = (time_t ) luaL_checklong (L , 5 );
184
+ backup = lua_toboolean (L , 6 );
185
+ #if (NGX_DEBUG )
186
+ ngx_log_error (NGX_LOG_ALERT , r -> connection -> log , 0 , "%s %s params: %s,%s,%d,%d,%d\n" , __FILE__ , __FUNCTION__ , host .data , p , weight , max_fails , fail_timeout );
187
+ #endif
188
+
189
+ uscf = ngx_http_lua_upstream_find_upstream (L , & host );
190
+ if (uscf == NULL ) {
191
+ lua_pushnil (L );
192
+ lua_pushliteral (L , "upstream not found\n" );
193
+ return 2 ;
194
+ }
195
+
196
+ u .url .data = ngx_pcalloc (uscf -> servers -> pool , u .url .len + 1 );
197
+ ngx_memcpy (u .url .data , p , u .url .len );
198
+
199
+ if (ngx_http_lua_upstream_find_server (uscf , u ) != NULL ) {
200
+ lua_pushnil (L );
201
+ lua_pushliteral (L , "server already exists\n" );
202
+ return 2 ;
203
+ } else {
204
+ // validate URL
205
+ if (ngx_parse_url (uscf -> servers -> pool , & u ) != NGX_OK ) {
206
+ if (u .err ) {
207
+ lua_pushnil (L );
208
+ lua_pushliteral (L , "url parser error\n" );
209
+ return 2 ;
210
+ }
211
+ }
212
+
213
+ us = ngx_array_push (uscf -> servers );
214
+ if (us == NULL ) {
215
+ lua_pushnil (L );
216
+ lua_pushliteral (L , "could not append server to upstream\n" );
217
+ return 2 ;
218
+ }
219
+
220
+ ngx_memzero (us , sizeof (ngx_http_upstream_server_t ));
221
+
222
+ us -> name = u .url ;
223
+ us -> addrs = u .addrs ;
224
+ us -> naddrs = u .naddrs ;
225
+ us -> weight = weight ;
226
+ us -> max_fails = max_fails ;
227
+ us -> fail_timeout = fail_timeout ;
228
+ us -> backup = backup ;
229
+ }
230
+
231
+ lua_pushboolean (L , 1 );
232
+ return 1 ;
233
+ }
144
234
145
235
static int
146
236
ngx_http_lua_upstream_get_servers (lua_State * L )
@@ -551,6 +641,32 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host)
551
641
return NULL ;
552
642
}
553
643
644
+ static ngx_http_upstream_server_t *
645
+ ngx_http_lua_upstream_find_server (ngx_http_upstream_srv_conf_t * us , ngx_url_t u )
646
+ {
647
+ ngx_uint_t i , j ;
648
+ size_t len ;
649
+ ngx_http_upstream_server_t * server = NULL ;
650
+
651
+ if (us -> servers == NULL || us -> servers -> nelts == 0 ) {
652
+ return NULL ;
653
+ }
654
+
655
+ server = us -> servers -> elts ;
656
+
657
+ for (i = 0 ; i < us -> servers -> nelts ; ++ i ) {
658
+ for (j = 0 ; j < server [i ].naddrs ; ++ j ) {
659
+ len = server [i ].addrs [j ].name .len ;
660
+
661
+ if (len == u .url .len
662
+ && ngx_memcmp (u .url .data , server [i ].addrs [j ].name .data , u .url .len ) == 0 ) {
663
+ return & server [i ];
664
+ }
665
+ }
666
+ }
667
+
668
+ return NULL ;
669
+ }
554
670
555
671
static int
556
672
ngx_http_lua_upstream_current_upstream_name (lua_State * L )
0 commit comments