Project

General

Profile

Feature #2117 ยป lighttpd-sticky.patch

bra, 2009-12-10 13:49

View differences:

lighttpd-1.4.25-sticky/src/mod_proxy.c 2009-12-09 15:35:15.000000000 +0100
58 58
	PROXY_BALANCE_UNSET,
59 59
	PROXY_BALANCE_FAIR,
60 60
	PROXY_BALANCE_HASH,
61
	PROXY_BALANCE_RR
61
	PROXY_BALANCE_RR,
62
	PROXY_BALANCE_STICKY
62 63
} proxy_balance_t;
63 64

  
64 65
typedef struct {
......
225 226
			s->balance = PROXY_BALANCE_RR;
226 227
		} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
227 228
			s->balance = PROXY_BALANCE_HASH;
229
		} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("sticky"))) {
230
					s->balance = PROXY_BALANCE_STICKY;
228 231
		} else {
229 232
			log_error_write(srv, __FILE__, __LINE__, "sb",
230
				        "proxy.balance has to be one of: fair, round-robin, hash, but not:", p->balance_buf);
233
				        "proxy.balance has to be one of: fair, round-robin, hash, sticky, but not:", p->balance_buf);
231 234
			return HANDLER_ERROR;
232 235
		}
233 236

  
......
1153 1156
			ndx = 0;
1154 1157
		}
1155 1158
	} else if (extension->value->used != 0) switch(p->conf.balance) {
1159
	case PROXY_BALANCE_STICKY:
1160
		/* source sticky balancing */
1161

  
1162
		if (p->conf.debug) {
1163
			log_error_write(srv, __FILE__, __LINE__,  "sd",
1164
					"proxy - used sticky balancing, hosts:", extension->value->used);
1165
		}
1166

  
1167
		for (k = 0, ndx = -1, last_max = ULONG_MAX; k < extension->value->used; k++) {
1168
			data_proxy *host = (data_proxy *)extension->value->data[k];
1169
			unsigned long cur_max;
1170

  
1171
			if (host->is_disabled) continue;
1172

  
1173
			char *remote_ip;
1174
			remote_ip = strdup(inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
1175
			cur_max = generate_crc32c(remote_ip, strlen(remote_ip)) +
1176
				generate_crc32c(CONST_BUF_LEN(host->host)) +
1177
				host->port;
1178

  
1179
			if (p->conf.debug) {
1180
				log_error_write(srv, __FILE__, __LINE__,  "ssbdd",
1181
						"proxy - election:",
1182
						remote_ip,
1183
						host->host,
1184
						host->port,
1185
						cur_max);
1186
			}
1187

  
1188
			if ((last_max == ULONG_MAX) || /* first round */
1189
				(cur_max > last_max)) {
1190
				last_max = cur_max;
1191

  
1192
				ndx = k;
1193
			}
1194
		}
1195

  
1196
		break;
1156 1197
	case PROXY_BALANCE_HASH:
1157 1198
		/* hash balancing */
1158 1199

  
    (1-1/1)