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