Skip to content

Commit 1ce0b66

Browse files
committed
use lua_setfield vs lua_rawset
1 parent 3551e40 commit 1ce0b66

File tree

1 file changed

+62
-140
lines changed

1 file changed

+62
-140
lines changed

src/ngx_dynamic_upstream_lua.c

Lines changed: 62 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -160,68 +160,46 @@ ngx_dynamic_upstream_lua_create_response(ngx_http_upstream_rr_peers_t *primary,
160160
{
161161
ngx_http_upstream_rr_peer_t *peer;
162162
ngx_http_upstream_rr_peers_t *peers, *backup;
163-
int size = 0, n, i = 1;
163+
int i = 1;
164164

165165
backup = primary->next;
166166

167167
if (flags & LOCK) {
168168
ngx_http_upstream_rr_peers_rlock(primary);
169169
}
170170

171-
if (flags & PRIMARY) {
172-
size += primary->number;
173-
}
174-
if (flags & BACKUP && backup) {
175-
size += backup->number;
176-
}
177-
178171
lua_newtable(L);
179-
lua_createtable(L, size, 0);
180172

181173
for (peers = primary; peers; peers = peers->next) {
182174
if ( (flags & PRIMARY && peers == primary) || (flags & BACKUP && peers == backup) ) {
183175
for (peer = peers->peer; peer; peer = peer->next, ++i) {
184-
n = 7;
185-
186-
if (peer->down) {
187-
n++;
188-
}
176+
lua_newtable(L);
189177

190-
lua_createtable(L, 0, n);
191-
192-
lua_pushliteral(L, "name");
193178
lua_pushlstring(L, (char *) peer->name.data,
194179
peer->name.len);
195-
lua_rawset(L, -3);
180+
lua_setfield(L, -2, "name");
196181

197-
lua_pushliteral(L, "weight");
198182
lua_pushinteger(L, (lua_Integer) peer->weight);
199-
lua_rawset(L, -3);
183+
lua_setfield(L, -2, "weight");
200184

201-
lua_pushliteral(L, "max_conns");
202185
lua_pushinteger(L, (lua_Integer) peer->max_conns);
203-
lua_rawset(L, -3);
186+
lua_setfield(L, -2, "max_conns");
204187

205-
lua_pushliteral(L, "conns");
206188
lua_pushinteger(L, (lua_Integer) peer->conns);
207-
lua_rawset(L, -3);
189+
lua_setfield(L, -2, "conns");
208190

209-
lua_pushliteral(L, "max_fails");
210191
lua_pushinteger(L, (lua_Integer) peer->max_fails);
211-
lua_rawset(L, -3);
192+
lua_setfield(L, -2, "max_fails");
212193

213-
lua_pushliteral(L, "fail_timeout");
214194
lua_pushinteger(L, (lua_Integer) peer->fail_timeout);
215-
lua_rawset(L, -3);
195+
lua_setfield(L, -2, "fail_timeout");
216196

217-
lua_pushliteral(L, "backup");
218197
lua_pushboolean(L, peers != primary);
219-
lua_rawset(L, -3);
198+
lua_setfield(L, -2, "backup");
220199

221200
if (peer->down) {
222-
lua_pushliteral(L, "down");
223201
lua_pushboolean(L, 1);
224-
lua_rawset(L, -3);
202+
lua_setfield(L, -2, "down");
225203
}
226204

227205
lua_rawseti(L, -2, i);
@@ -385,147 +363,101 @@ static void
385363
ngx_http_dynamic_upstream_lua_push_healthcheck(lua_State *L, ngx_http_upstream_srv_conf_t *uscf)
386364
{
387365
ngx_http_dynamic_upstream_lua_srv_conf_t *ucscf;
388-
int n = 4;
389366
ngx_uint_t i;
390367

391368
ucscf = ngx_http_get_dynamic_upstream_lua_srv_conf(uscf);
392369

393370
if (ucscf == NULL || ucscf->data == NULL || ucscf->data->type.data == NULL) {
394-
goto empty;
371+
lua_pushnil(L);
372+
return;
395373
}
396374

397375
ngx_shmtx_lock(&ucscf->shpool->mutex);
398376

399-
n += ucscf->data->interval != NGX_CONF_UNSET_UINT;
400-
n += ucscf->data->request_uri.len != NGX_CONF_UNSET_UINT;
401-
402-
lua_pushliteral(L, "healthcheck");
403-
lua_createtable(L, 0, n);
377+
lua_newtable(L);
404378

405-
lua_pushliteral(L, "typ");
406-
lua_pushlstring(L, (char *) ucscf->data->type.data,
407-
ucscf->data->type.len);
408-
lua_rawset(L, -3);
379+
lua_pushlstring(L, (char *) ucscf->data->type.data, ucscf->data->type.len);
380+
lua_setfield(L, -2, "typ");
409381

410-
lua_pushliteral(L, "fall");
411382
lua_pushinteger(L, (lua_Integer) ucscf->data->fall);
412-
lua_rawset(L, -3);
383+
lua_setfield(L, -2, "fall");
413384

414-
lua_pushliteral(L, "rise");
415385
lua_pushinteger(L, (lua_Integer) ucscf->data->rise);
416-
lua_rawset(L, -3);
386+
lua_setfield(L, -2, "rise");
417387

418-
lua_pushliteral(L, "timeout");
419388
lua_pushinteger(L, (lua_Integer) ucscf->data->timeout);
420-
lua_rawset(L, -3);
389+
lua_setfield(L, -2, "timeout");
421390

422391
if (ucscf->data->interval != NGX_CONF_UNSET_UINT) {
423-
lua_pushliteral(L, "interval");
424392
lua_pushinteger(L, (lua_Integer) ucscf->data->interval);
425-
lua_rawset(L, -3);
393+
lua_setfield(L, -2, "interval");
426394
}
427395

428396
if (ucscf->data->request_uri.len != 0) {
429-
n = 2;
430-
431-
if (ucscf->data->request_headers) {
432-
++n;
433-
}
434-
if (ucscf->data->request_body.len != 0) {
435-
++n;
436-
}
437-
if (ucscf->data->response_codes || ucscf->data->response_body.len != 0) {
438-
++n;
439-
}
397+
lua_newtable(L);
440398

441-
lua_pushliteral(L, "command");
442-
lua_createtable(L, 0, n);
399+
lua_pushlstring(L, (char *) ucscf->data->request_uri.data,
400+
ucscf->data->request_uri.len);
401+
lua_setfield(L, -2, "uri");
443402

444-
{
445-
lua_pushliteral(L, "uri");
446-
lua_pushlstring(L, (char *) ucscf->data->request_uri.data,
447-
ucscf->data->request_uri.len);
448-
lua_rawset(L, -3);
449-
450-
lua_pushliteral(L, "method");
451-
lua_pushlstring(L, (char *) ucscf->data->request_method.data,
452-
ucscf->data->request_method.len);
453-
lua_rawset(L, -3);
454-
455-
if (ucscf->data->request_headers) {
456-
lua_pushliteral(L, "headers");
457-
lua_createtable(L, 0, ucscf->data->request_headers_count);
458-
459-
for (i = 0; i < ucscf->data->request_headers_count; ++i) {
460-
lua_pushlstring(L, (char *) ucscf->data->request_headers[i].name.data,
461-
ucscf->data->request_headers[i].name.len);
462-
lua_pushlstring(L, (char *) ucscf->data->request_headers[i].value.data,
463-
ucscf->data->request_headers[i].value.len);
464-
lua_rawset(L, -3);
465-
}
403+
lua_pushlstring(L, (char *) ucscf->data->request_method.data,
404+
ucscf->data->request_method.len);
405+
lua_setfield(L, -2, "method");
466406

467-
lua_rawset(L, -3);
468-
}
407+
if (ucscf->data->request_headers) {
408+
lua_newtable(L);
469409

470-
if (ucscf->data->request_body.len != 0) {
471-
lua_pushliteral(L, "body");
472-
lua_pushlstring(L, (char *) ucscf->data->request_body.data,
473-
ucscf->data->request_body.len);
410+
for (i = 0; i < ucscf->data->request_headers_count; ++i) {
411+
lua_pushlstring(L, (char *) ucscf->data->request_headers[i].name.data,
412+
ucscf->data->request_headers[i].name.len);
413+
lua_pushlstring(L, (char *) ucscf->data->request_headers[i].value.data,
414+
ucscf->data->request_headers[i].value.len);
474415
lua_rawset(L, -3);
475416
}
476417

477-
if (ucscf->data->response_codes || ucscf->data->response_body.len != 0) {
478-
n = 0;
479-
n = n + (ucscf->data->response_codes ? 1 : 0);
480-
n = n + (ucscf->data->response_body.len != 0 ? 1 : 0);
418+
lua_setfield(L, -2, "headers");
419+
}
481420

482-
lua_pushliteral(L, "expected");
483-
lua_createtable(L, 0, n);
421+
if (ucscf->data->request_body.len != 0) {
422+
lua_pushlstring(L, (char *) ucscf->data->request_body.data,
423+
ucscf->data->request_body.len);
424+
lua_setfield(L, -2, "body");
425+
}
484426

485-
if (ucscf->data->response_codes) {
486-
lua_pushliteral(L, "codes");
487-
lua_createtable(L, ucscf->data->response_codes_count, 0);
427+
if (ucscf->data->response_codes || ucscf->data->response_body.len != 0) {
428+
lua_newtable(L);
488429

489-
for (i = 0; i < ucscf->data->response_codes_count; ++i) {
490-
lua_pushinteger(L, (lua_Integer) ucscf->data->response_codes[i]);
491-
lua_rawseti(L, -2, i + 1);
492-
}
430+
if (ucscf->data->response_codes) {
431+
lua_newtable(L);
493432

494-
lua_rawset(L, -3);
433+
for (i = 0; i < ucscf->data->response_codes_count; ++i) {
434+
lua_pushinteger(L, (lua_Integer) ucscf->data->response_codes[i]);
435+
lua_rawseti(L, -2, i + 1);
495436
}
496437

497-
if (ucscf->data->response_body.len != 0) {
498-
lua_pushliteral(L, "body");
499-
lua_pushlstring(L, (char *) ucscf->data->response_body.data,
500-
ucscf->data->response_body.len);
501-
lua_rawset(L, -3);
502-
}
438+
lua_setfield(L, -2, "codes");
439+
}
503440

504-
lua_rawset(L, -3);
441+
if (ucscf->data->response_body.len != 0) {
442+
lua_pushlstring(L, (char *) ucscf->data->response_body.data,
443+
ucscf->data->response_body.len);
444+
lua_setfield(L, -2, "body");
505445
}
446+
447+
lua_setfield(L, -2, "expected");
506448
}
507449

508-
lua_rawset(L, -3);
450+
lua_setfield(L, -2, "command");
509451
}
510452

511-
lua_rawset(L, -3);
512-
513453
ngx_shmtx_unlock(&ucscf->shpool->mutex);
514-
515-
return;
516-
517-
empty:
518-
519-
lua_pushliteral(L, "healthcheck");
520-
lua_pushnil(L);
521-
lua_rawset(L, -3);
522454
}
523455

524456

525457
static int
526458
ngx_http_dynamic_upstream_lua_get_healthcheck(lua_State * L)
527459
{
528-
ngx_uint_t i, j, count = 0;
460+
ngx_uint_t i, j;
529461
ngx_http_upstream_srv_conf_t **uscfp, *uscf;
530462
ngx_http_upstream_main_conf_t *umcf;
531463

@@ -538,15 +470,7 @@ ngx_http_dynamic_upstream_lua_get_healthcheck(lua_State * L)
538470

539471
lua_pushboolean(L, 1);
540472

541-
for (i = 0; i < umcf->upstreams.nelts; i++) {
542-
uscf = uscfp[i];
543-
if (uscf->srv_conf != NULL) {
544-
++count;
545-
}
546-
}
547-
548473
lua_newtable(L);
549-
lua_createtable(L, count, 0);
550474

551475
umcf = ngx_http_lua_upstream_get_upstream_main_conf(L);
552476
uscfp = umcf->upstreams.elts;
@@ -555,21 +479,19 @@ ngx_http_dynamic_upstream_lua_get_healthcheck(lua_State * L)
555479
uscf = uscfp[i];
556480

557481
if (uscf->srv_conf != NULL) {
558-
lua_createtable(L, 0, 2);
482+
lua_newtable(L);
559483

560-
lua_pushliteral(L, "name");
561484
lua_pushlstring(L, (char *) uscf->host.data, uscf->host.len);
562-
lua_rawset(L, -3);
485+
lua_setfield(L, -2, "name");
563486

564487
ngx_http_dynamic_upstream_lua_push_healthcheck(L, uscf);
488+
lua_setfield(L, -2, "healthcheck");
565489

566490
lua_rawseti(L, -2, j++);
567491
}
568492
}
569493

570-
lua_pushnil(L);
571-
572-
return 3;
494+
return 2;
573495
}
574496

575497

@@ -1092,4 +1014,4 @@ ngx_http_dynamic_upstream_lua_update_healthcheck(lua_State *L)
10921014
lua_settop(L, top);
10931015

10941016
return ngx_http_dynamic_upstream_lua_error(L, error);
1095-
}
1017+
}

0 commit comments

Comments
 (0)