1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
|
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_cache.h"
#include "ngx_http_lua_balancer.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_directive.h"
struct ngx_http_lua_balancer_peer_data_s {
/* the round robin data must be first */
ngx_http_upstream_rr_peer_data_t rrp;
ngx_http_lua_srv_conf_t *conf;
ngx_http_request_t *request;
ngx_uint_t more_tries;
ngx_uint_t total_tries;
struct sockaddr *sockaddr;
socklen_t socklen;
ngx_str_t *host;
in_port_t port;
int last_peer_state;
#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
unsigned cloned_upstream_conf; /* :1 */
#endif
};
#if (NGX_HTTP_SSL)
static ngx_int_t ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc,
void *data);
static void ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc,
void *data);
#endif
static ngx_int_t ngx_http_lua_balancer_init(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us);
static ngx_int_t ngx_http_lua_balancer_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us);
static ngx_int_t ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc,
void *data);
static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
ngx_http_request_t *r);
static void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
ngx_int_t
ngx_http_lua_balancer_handler_file(ngx_http_request_t *r,
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
{
ngx_int_t rc;
rc = ngx_http_lua_cache_loadfile(r->connection->log, L,
lscf->balancer.src.data,
&lscf->balancer.src_ref,
lscf->balancer.src_key);
if (rc != NGX_OK) {
return rc;
}
/* make sure we have a valid code chunk */
ngx_http_lua_assert(lua_isfunction(L, -1));
return ngx_http_lua_balancer_by_chunk(L, r);
}
ngx_int_t
ngx_http_lua_balancer_handler_inline(ngx_http_request_t *r,
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
{
ngx_int_t rc;
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
lscf->balancer.src.data,
lscf->balancer.src.len,
&lscf->balancer.src_ref,
lscf->balancer.src_key,
(const char *) lscf->balancer.chunkname);
if (rc != NGX_OK) {
return rc;
}
/* make sure we have a valid code chunk */
ngx_http_lua_assert(lua_isfunction(L, -1));
return ngx_http_lua_balancer_by_chunk(L, r);
}
char *
ngx_http_lua_balancer_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *rv;
ngx_conf_t save;
save = *cf;
cf->handler = ngx_http_lua_balancer_by_lua;
cf->handler_conf = conf;
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd);
*cf = save;
return rv;
}
char *
ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
size_t chunkname_len;
u_char *chunkname;
u_char *cache_key = NULL;
u_char *name;
ngx_str_t *value;
ngx_http_lua_srv_conf_t *lscf = conf;
ngx_http_upstream_srv_conf_t *uscf;
dd("enter");
/* must specify a content handler */
if (cmd->post == NULL) {
return NGX_CONF_ERROR;
}
if (lscf->balancer.handler) {
return "is duplicate";
}
value = cf->args->elts;
lscf->balancer.handler = (ngx_http_lua_srv_conf_handler_pt) cmd->post;
if (cmd->post == ngx_http_lua_balancer_handler_file) {
/* Lua code in an external file */
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
value[1].len);
if (name == NULL) {
return NGX_CONF_ERROR;
}
cache_key = ngx_http_lua_gen_file_cache_key(cf, value[1].data,
value[1].len);
if (cache_key == NULL) {
return NGX_CONF_ERROR;
}
lscf->balancer.src.data = name;
lscf->balancer.src.len = ngx_strlen(name);
} else {
cache_key = ngx_http_lua_gen_chunk_cache_key(cf, "balancer_by_lua",
value[1].data,
value[1].len);
if (cache_key == NULL) {
return NGX_CONF_ERROR;
}
chunkname = ngx_http_lua_gen_chunk_name(cf, "balancer_by_lua",
sizeof("balancer_by_lua") - 1,
&chunkname_len);
if (chunkname == NULL) {
return NGX_CONF_ERROR;
}
/* Don't eval nginx variables for inline lua code */
lscf->balancer.src = value[1];
lscf->balancer.chunkname = chunkname;
}
lscf->balancer.src_key = cache_key;
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
if (uscf->peer.init_upstream) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"load balancing method redefined");
}
uscf->peer.init_upstream = ngx_http_lua_balancer_init;
uscf->flags = NGX_HTTP_UPSTREAM_CREATE
|NGX_HTTP_UPSTREAM_WEIGHT
|NGX_HTTP_UPSTREAM_MAX_FAILS
|NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|NGX_HTTP_UPSTREAM_DOWN;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_lua_balancer_init(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *us)
{
if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
return NGX_ERROR;
}
/* this callback is called upon individual requests */
us->peer.init = ngx_http_lua_balancer_init_peer;
return NGX_OK;
}
static ngx_int_t
ngx_http_lua_balancer_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us)
{
ngx_http_lua_srv_conf_t *bcf;
ngx_http_lua_balancer_peer_data_t *bp;
bp = ngx_pcalloc(r->pool, sizeof(ngx_http_lua_balancer_peer_data_t));
if (bp == NULL) {
return NGX_ERROR;
}
r->upstream->peer.data = &bp->rrp;
if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
return NGX_ERROR;
}
r->upstream->peer.get = ngx_http_lua_balancer_get_peer;
r->upstream->peer.free = ngx_http_lua_balancer_free_peer;
#if (NGX_HTTP_SSL)
r->upstream->peer.set_session = ngx_http_lua_balancer_set_session;
r->upstream->peer.save_session = ngx_http_lua_balancer_save_session;
#endif
bcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_lua_module);
bp->conf = bcf;
bp->request = r;
return NGX_OK;
}
static ngx_int_t
ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
{
lua_State *L;
ngx_int_t rc;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_srv_conf_t *lscf;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_balancer_peer_data_t *bp = data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"lua balancer peer, tries: %ui", pc->tries);
lscf = bp->conf;
r = bp->request;
ngx_http_lua_assert(lscf->balancer.handler && r);
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
ctx = ngx_http_lua_create_ctx(r);
if (ctx == NULL) {
return NGX_ERROR;
}
L = ngx_http_lua_get_lua_vm(r, ctx);
} else {
L = ngx_http_lua_get_lua_vm(r, ctx);
dd("reset ctx");
ngx_http_lua_reset_ctx(r, L, ctx);
}
ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER;
bp->sockaddr = NULL;
bp->socklen = 0;
bp->more_tries = 0;
bp->total_tries++;
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
/* balancer_by_lua does not support yielding and
* there cannot be any conflicts among concurrent requests,
* thus it is safe to store the peer data in the main conf.
*/
lmcf->balancer_peer_data = bp;
rc = lscf->balancer.handler(r, lscf, L);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if (ctx->exited && ctx->exit_code != NGX_OK) {
rc = ctx->exit_code;
if (rc == NGX_ERROR
|| rc == NGX_BUSY
|| rc == NGX_DECLINED
#ifdef HAVE_BALANCER_STATUS_CODE_PATCH
|| rc >= NGX_HTTP_SPECIAL_RESPONSE
#endif
) {
return rc;
}
if (rc > NGX_OK) {
return NGX_ERROR;
}
}
if (bp->sockaddr && bp->socklen) {
pc->sockaddr = bp->sockaddr;
pc->socklen = bp->socklen;
pc->cached = 0;
pc->connection = NULL;
pc->name = bp->host;
bp->rrp.peers->single = 0;
if (bp->more_tries) {
r->upstream->peer.tries += bp->more_tries;
}
dd("tries: %d", (int) r->upstream->peer.tries);
return NGX_OK;
}
return ngx_http_upstream_get_round_robin_peer(pc, &bp->rrp);
}
static ngx_int_t
ngx_http_lua_balancer_by_chunk(lua_State *L, ngx_http_request_t *r)
{
u_char *err_msg;
size_t len;
ngx_int_t rc;
/* init nginx context in Lua VM */
ngx_http_lua_set_req(L, r);
#ifndef OPENRESTY_LUAJIT
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);
/* {{{ make new env inheriting main thread's globals table */
lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new env */
ngx_http_lua_get_globals_table(L);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */
/* }}} */
lua_setfenv(L, -2); /* set new running env for the code closure */
#endif /* OPENRESTY_LUAJIT */
lua_pushcfunction(L, ngx_http_lua_traceback);
lua_insert(L, 1); /* put it under chunk and args */
/* protected call user code */
rc = lua_pcall(L, 0, 1, 1);
lua_remove(L, 1); /* remove traceback function */
dd("rc == %d", (int) rc);
if (rc != 0) {
/* error occurred when running loaded code */
err_msg = (u_char *) lua_tolstring(L, -1, &len);
if (err_msg == NULL) {
err_msg = (u_char *) "unknown reason";
len = sizeof("unknown reason") - 1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to run balancer_by_lua*: %*s", len, err_msg);
lua_settop(L, 0); /* clear remaining elems on stack */
return NGX_ERROR;
}
lua_settop(L, 0); /* clear remaining elems on stack */
return rc;
}
static void
ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state)
{
ngx_http_lua_balancer_peer_data_t *bp = data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"lua balancer free peer, tries: %ui", pc->tries);
if (bp->sockaddr && bp->socklen) {
bp->last_peer_state = (int) state;
if (pc->tries) {
pc->tries--;
}
return;
}
/* fallback */
ngx_http_upstream_free_round_robin_peer(pc, data, state);
}
#if (NGX_HTTP_SSL)
static ngx_int_t
ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
{
ngx_http_lua_balancer_peer_data_t *bp = data;
if (bp->sockaddr && bp->socklen) {
/* TODO */
return NGX_OK;
}
return ngx_http_upstream_set_round_robin_peer_session(pc, &bp->rrp);
}
static void
ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
{
ngx_http_lua_balancer_peer_data_t *bp = data;
if (bp->sockaddr && bp->socklen) {
/* TODO */
return;
}
ngx_http_upstream_save_round_robin_peer_session(pc, &bp->rrp);
return;
}
#endif
int
ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
const u_char *addr, size_t addr_len, int port, char **err)
{
ngx_url_t url;
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_balancer_peer_data_t *bp;
if (r == NULL) {
*err = "no request found";
return NGX_ERROR;
}
u = r->upstream;
if (u == NULL) {
*err = "no upstream found";
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
*err = "no ctx found";
return NGX_ERROR;
}
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
*err = "API disabled in the current context";
return NGX_ERROR;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
/* we cannot read r->upstream->peer.data here directly because
* it could be overridden by other modules like
* ngx_http_upstream_keepalive_module.
*/
bp = lmcf->balancer_peer_data;
if (bp == NULL) {
*err = "no upstream peer data found";
return NGX_ERROR;
}
ngx_memzero(&url, sizeof(ngx_url_t));
url.url.data = ngx_palloc(r->pool, addr_len);
if (url.url.data == NULL) {
*err = "no memory";
return NGX_ERROR;
}
ngx_memcpy(url.url.data, addr, addr_len);
url.url.len = addr_len;
url.default_port = (in_port_t) port;
url.uri_part = 0;
url.no_resolve = 1;
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
if (url.err) {
*err = url.err;
}
return NGX_ERROR;
}
if (url.addrs && url.addrs[0].sockaddr) {
bp->sockaddr = url.addrs[0].sockaddr;
bp->socklen = url.addrs[0].socklen;
bp->host = &url.addrs[0].name;
} else {
*err = "no host allowed";
return NGX_ERROR;
}
return NGX_OK;
}
int
ngx_http_lua_ffi_balancer_set_timeouts(ngx_http_request_t *r,
long connect_timeout, long send_timeout, long read_timeout,
char **err)
{
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
ngx_http_upstream_conf_t *ucf;
#endif
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_balancer_peer_data_t *bp;
if (r == NULL) {
*err = "no request found";
return NGX_ERROR;
}
u = r->upstream;
if (u == NULL) {
*err = "no upstream found";
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
*err = "no ctx found";
return NGX_ERROR;
}
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
*err = "API disabled in the current context";
return NGX_ERROR;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
bp = lmcf->balancer_peer_data;
if (bp == NULL) {
*err = "no upstream peer data found";
return NGX_ERROR;
}
#if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
if (!bp->cloned_upstream_conf) {
/* we clone the upstream conf for the current request so that
* we do not affect other requests at all. */
ucf = ngx_palloc(r->pool, sizeof(ngx_http_upstream_conf_t));
if (ucf == NULL) {
*err = "no memory";
return NGX_ERROR;
}
ngx_memcpy(ucf, u->conf, sizeof(ngx_http_upstream_conf_t));
u->conf = ucf;
bp->cloned_upstream_conf = 1;
} else {
ucf = u->conf;
}
#endif
if (connect_timeout > 0) {
#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
u->connect_timeout = (ngx_msec_t) connect_timeout;
#else
ucf->connect_timeout = (ngx_msec_t) connect_timeout;
#endif
}
if (send_timeout > 0) {
#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
u->send_timeout = (ngx_msec_t) send_timeout;
#else
ucf->send_timeout = (ngx_msec_t) send_timeout;
#endif
}
if (read_timeout > 0) {
#if (HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
u->read_timeout = (ngx_msec_t) read_timeout;
#else
ucf->read_timeout = (ngx_msec_t) read_timeout;
#endif
}
return NGX_OK;
}
int
ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
int count, char **err)
{
#if (nginx_version >= 1007005)
ngx_uint_t max_tries, total;
#endif
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_balancer_peer_data_t *bp;
if (r == NULL) {
*err = "no request found";
return NGX_ERROR;
}
u = r->upstream;
if (u == NULL) {
*err = "no upstream found";
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
*err = "no ctx found";
return NGX_ERROR;
}
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
*err = "API disabled in the current context";
return NGX_ERROR;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
bp = lmcf->balancer_peer_data;
if (bp == NULL) {
*err = "no upstream peer data found";
return NGX_ERROR;
}
#if (nginx_version >= 1007005)
max_tries = r->upstream->conf->next_upstream_tries;
total = bp->total_tries + r->upstream->peer.tries - 1;
if (max_tries && total + count > max_tries) {
count = max_tries - total;
*err = "reduced tries due to limit";
} else {
*err = NULL;
}
#else
*err = NULL;
#endif
bp->more_tries = count;
return NGX_OK;
}
int
ngx_http_lua_ffi_balancer_get_last_failure(ngx_http_request_t *r,
int *status, char **err)
{
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
ngx_http_upstream_state_t *state;
ngx_http_lua_balancer_peer_data_t *bp;
ngx_http_lua_main_conf_t *lmcf;
if (r == NULL) {
*err = "no request found";
return NGX_ERROR;
}
u = r->upstream;
if (u == NULL) {
*err = "no upstream found";
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
*err = "no ctx found";
return NGX_ERROR;
}
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
*err = "API disabled in the current context";
return NGX_ERROR;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
bp = lmcf->balancer_peer_data;
if (bp == NULL) {
*err = "no upstream peer data found";
return NGX_ERROR;
}
if (r->upstream_states && r->upstream_states->nelts > 1) {
state = r->upstream_states->elts;
*status = (int) state[r->upstream_states->nelts - 2].status;
} else {
*status = 0;
}
return bp->last_peer_state;
}
int
ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,
char **err)
{
ngx_http_lua_ctx_t *ctx;
ngx_http_upstream_t *u;
if (r == NULL) {
*err = "no request found";
return NGX_ERROR;
}
u = r->upstream;
if (u == NULL) {
*err = "no upstream found";
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
*err = "no ctx found";
return NGX_ERROR;
}
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
*err = "API disabled in the current context";
return NGX_ERROR;
}
/* u->create_request can not be NULL since we are in balancer phase */
ngx_http_lua_assert(u->create_request != NULL);
*err = NULL;
if (u->request_bufs != NULL && u->request_bufs != r->request_body->bufs) {
/* u->request_bufs already contains a valid request buffer
* remove it from chain first
*/
u->request_bufs = u->request_bufs->next;
}
return u->create_request(r);
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
|