Skip to content

Commit

Permalink
iwrr: a wrr with always O(1) time and O(n) memory
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Jan 29, 2023
1 parent d96d598 commit 0e04a49
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static ngx_int_t ngx_http_upstream_init_iwrr_peer(ngx_http_request_t *r,
static ngx_int_t ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc,
void *data);
static ngx_http_upstream_rr_peer_t *ngx_http_upstream_get_iwrr(
ngx_http_upstream_iwrr_peer_data_t *oip);
ngx_http_upstream_iwrr_peer_data_t *uip);
static ngx_http_upstream_iwrr_queue_t *ngx_http_upstream_iwrr_queue_next(
ngx_http_upstream_iwrr_srv_conf_t *uiscf);

Expand Down Expand Up @@ -229,20 +229,20 @@ ngx_http_upstream_init_iwrr_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us)
{
ngx_http_upstream_iwrr_srv_conf_t *uiscf;
ngx_http_upstream_iwrr_peer_data_t *oip;
ngx_http_upstream_iwrr_peer_data_t *uip;

uiscf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_iwrr_module);
if (uiscf == NULL) {
return NGX_ERROR;
}

oip = ngx_palloc(r->pool, sizeof(ngx_http_upstream_iwrr_peer_data_t));
if (oip == NULL) {
uip = ngx_palloc(r->pool, sizeof(ngx_http_upstream_iwrr_peer_data_t));
if (uip == NULL) {
return NGX_ERROR;
}

oip->uiscf = uiscf;
r->upstream->peer.data = &oip->rrp;
uip->uiscf = uiscf;
r->upstream->peer.data = &uip->rrp;

if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
return NGX_ERROR;
Expand All @@ -256,7 +256,7 @@ ngx_http_upstream_init_iwrr_peer(ngx_http_request_t *r,
static ngx_int_t
ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)
{
ngx_http_upstream_iwrr_peer_data_t *oip = data;
ngx_http_upstream_iwrr_peer_data_t *uip = data;

ngx_int_t rc;
ngx_uint_t i, n;
Expand All @@ -270,7 +270,7 @@ ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)
pc->cached = 0;
pc->connection = NULL;

rrp = &oip->rrp;
rrp = &uip->rrp;

peers = rrp->peers;
ngx_http_upstream_rr_peers_wlock(peers);
Expand All @@ -297,7 +297,7 @@ ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)

/* there are several peers */

peer = ngx_http_upstream_get_iwrr(oip);
peer = ngx_http_upstream_get_iwrr(uip);

if (peer == NULL) {
goto failed;
Expand Down Expand Up @@ -330,7 +330,7 @@ ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)

rrp->peers = peers->next;

oip->uiscf = oip->uiscf ? oip->uiscf->next : oip->uiscf;
uip->uiscf = uip->uiscf ? uip->uiscf->next : uip->uiscf;

n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
/ (8 * sizeof(uintptr_t));
Expand All @@ -341,7 +341,7 @@ ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)

ngx_http_upstream_rr_peers_unlock(peers);

rc = ngx_http_upstream_get_iwrr_peer(pc, oip);
rc = ngx_http_upstream_get_iwrr_peer(pc, uip);

if (rc != NGX_BUSY) {
return rc;
Expand All @@ -358,7 +358,7 @@ ngx_http_upstream_get_iwrr_peer(ngx_peer_connection_t *pc, void *data)
}

static ngx_http_upstream_rr_peer_t *
ngx_http_upstream_get_iwrr(ngx_http_upstream_iwrr_peer_data_t *oip)
ngx_http_upstream_get_iwrr(ngx_http_upstream_iwrr_peer_data_t *uip)
{
time_t now;
uintptr_t m;
Expand All @@ -371,9 +371,9 @@ ngx_http_upstream_get_iwrr(ngx_http_upstream_iwrr_peer_data_t *oip)

now = ngx_time();

rrp = &oip->rrp;
rrp = &uip->rrp;
peers = rrp->peers;
uiscf = oip->uiscf;
uiscf = uip->uiscf;

#if (T_NGX_HTTP_UPSTREAM_RANDOM)
if (uiscf->init_number == NGX_CONF_UNSET_UINT) {
Expand Down

0 comments on commit 0e04a49

Please sign in to comment.