Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Queue management functions. |
| 3 | * |
Willy Tarreau | a8cff1d | 2007-04-09 16:10:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 13 | #include <common/config.h> |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 14 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 15 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | |
| 17 | #include <types/proxy.h> |
| 18 | #include <types/session.h> |
| 19 | |
| 20 | #include <proto/queue.h> |
| 21 | #include <proto/server.h> |
| 22 | #include <proto/task.h> |
| 23 | |
| 24 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 25 | struct pool_head *pool2_pendconn; |
| 26 | |
| 27 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 28 | int init_pendconn() |
| 29 | { |
| 30 | pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 31 | return pool2_pendconn != NULL; |
| 32 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
| 34 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 35 | * and the proxy's usage relative to its dynamic connections limit. It is |
| 36 | * expected that 0 < s->minconn <= s->maxconn when this is called. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 38 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | { |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 40 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 41 | /* no fullconn or proxy is full */ |
| 42 | return s->maxconn; |
| 43 | |
| 44 | if (s->minconn == s->maxconn) |
| 45 | /* static limit */ |
| 46 | return s->maxconn; |
| 47 | |
| 48 | return MAX(s->minconn, |
| 49 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * Manages a server's connection queue. If woken up, will try to dequeue as |
| 55 | * many pending sessions as possible, and wake them up. The task has nothing |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 56 | * else to do, so it always returns ETERNITY. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 58 | void process_srv_queue(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | { |
| 60 | struct server *s = (struct server*)t->context; |
| 61 | struct proxy *p = s->proxy; |
| 62 | int xferred; |
| 63 | |
| 64 | /* First, check if we can handle some connections queued at the proxy. We |
| 65 | * will take as many as we can handle. |
| 66 | */ |
| 67 | for (xferred = 0; s->cur_sess + xferred < srv_dynamic_maxconn(s); xferred++) { |
| 68 | struct session *sess; |
| 69 | |
| 70 | sess = pendconn_get_next_sess(s, p); |
| 71 | if (sess == NULL) |
| 72 | break; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 73 | task_wakeup(sess->task); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 76 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Detaches the next pending connection from either a server or a proxy, and |
| 80 | * returns its associated session. If no pending connection is found, NULL is |
| 81 | * returned. Note that neither <srv> nor <px> can be NULL. |
| 82 | */ |
| 83 | struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px) |
| 84 | { |
| 85 | struct pendconn *p; |
| 86 | struct session *sess; |
| 87 | |
| 88 | p = pendconn_from_srv(srv); |
| 89 | if (!p) { |
| 90 | p = pendconn_from_px(px); |
| 91 | if (!p) |
| 92 | return NULL; |
| 93 | p->sess->srv = srv; |
| 94 | } |
| 95 | sess = p->sess; |
| 96 | pendconn_free(p); |
| 97 | return sess; |
| 98 | } |
| 99 | |
| 100 | /* Adds the session <sess> to the pending connection list of server <sess>->srv |
| 101 | * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers |
| 102 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
| 103 | * pendconn itself. |
| 104 | */ |
| 105 | struct pendconn *pendconn_add(struct session *sess) |
| 106 | { |
| 107 | struct pendconn *p; |
| 108 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 109 | p = pool_alloc2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | if (!p) |
| 111 | return NULL; |
| 112 | |
| 113 | sess->pend_pos = p; |
| 114 | p->sess = sess; |
| 115 | p->srv = sess->srv; |
| 116 | if (sess->srv) { |
| 117 | LIST_ADDQ(&sess->srv->pendconns, &p->list); |
| 118 | sess->logs.srv_queue_size += sess->srv->nbpend; |
| 119 | sess->srv->nbpend++; |
| 120 | if (sess->srv->nbpend > sess->srv->nbpend_max) |
| 121 | sess->srv->nbpend_max = sess->srv->nbpend; |
| 122 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 123 | LIST_ADDQ(&sess->be->pendconns, &p->list); |
| 124 | sess->logs.prx_queue_size += sess->be->nbpend; |
| 125 | sess->be->nbpend++; |
| 126 | if (sess->be->nbpend > sess->be->nbpend_max) |
| 127 | sess->be->nbpend_max = sess->be->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 128 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 129 | sess->be->totpend++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | return p; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 135 | * the pending connection. The connection might have been queued to a specific |
| 136 | * server as well as to the proxy. The session also gets marked unqueued. |
| 137 | */ |
| 138 | void pendconn_free(struct pendconn *p) |
| 139 | { |
| 140 | LIST_DEL(&p->list); |
| 141 | p->sess->pend_pos = NULL; |
| 142 | if (p->srv) |
| 143 | p->srv->nbpend--; |
| 144 | else |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 145 | p->sess->be->nbpend--; |
| 146 | p->sess->be->totpend--; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 147 | pool_free2(pool2_pendconn, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * Local variables: |
| 153 | * c-indent-level: 8 |
| 154 | * c-basic-offset: 8 |
| 155 | * End: |
| 156 | */ |