forked from haakonnessjoen/php-beanstalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beanstalk_pool.c
240 lines (200 loc) · 6.48 KB
/
beanstalk_pool.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Antony Dovgal <tony2001@phpclub.net> |
| Mikael Johansson <mikael AT synd DOT info> |
+----------------------------------------------------------------------+
*/
/* $Id: beanstalk_pool.c 310129 2011-04-11 04:44:27Z hradtke $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/crc32.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_smart_str.h"
#include "beanstalk_pool.h"
//ZEND_EXTERN_MODULE_GLOBALS(beanstalk)
static unsigned int bsc_hash_crc32_init() { return ~0; }
static unsigned int bsc_hash_crc32_finish(unsigned int seed) { return ~seed; }
static unsigned int bsc_hash_crc32_combine(unsigned int seed, const void *key, unsigned int key_len) /*
CRC32 hash {{{ */
{
const char *p = (const char *)key, *end = p + key_len;
while (p < end) {
CRC32(seed, *(p++));
}
return seed;
}
/* }}} */
bsc_hash_function_t bsc_hash_crc32 = {
bsc_hash_crc32_init,
bsc_hash_crc32_combine,
bsc_hash_crc32_finish
};
static unsigned int bsc_hash_fnv1a_combine(unsigned int seed, const void *key, unsigned int key_len) /*
FNV-1a hash {{{ */
{
const char *p = (const char *)key, *end = p + key_len;
while (p < end) {
seed ^= (unsigned int)*(p++);
seed *= FNV_32_PRIME;
}
return seed;
}
/* }}} */
static unsigned int bsc_hash_fnv1a_init() { return FNV_32_INIT; }
static unsigned int bsc_hash_fnv1a_finish(unsigned int seed) { return seed; }
bsc_hash_function_t bsc_hash_fnv1a = {
bsc_hash_fnv1a_init,
bsc_hash_fnv1a_combine,
bsc_hash_fnv1a_finish
};
/*
double timeval_to_double(struct timeval tv) {
return (double)tv.tv_sec + ((double)tv.tv_usec) / 1000000;
}
struct timeval double_to_timeval(double sec) {
struct timeval tv;
tv.tv_sec = (long)sec;
tv.tv_usec = (sec - tv.tv_sec) * 1000000;
return tv;
}
*/
void bsc_error_callback(bsc *svr, bsc_error_t error)
{
char errorstr[BSC_ERRSTR_LEN];
switch (error) {
case BSC_ERROR_INTERNAL:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "critical error: recieved BSC_ERROR_INTERNAL, quitting");
break;
case BSC_ERROR_MEMORY:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "critical error: recieved BSC_ERROR_MEMORY, quitting");
break;
case BSC_ERROR_SOCKET:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: recieved BSC_ERROR_SOCKET, quitting ...");
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "critical error: got unknown error (%d)\n", error);
}
}
bsc *bsc_server_new(
const char *host, int host_len, unsigned short tcp_port,
double timeout, int retry_interval TSRMLS_DC) /* {{{ */
{
bsc * svr;
char errorstr[BSC_ERRSTR_LEN];
char port[BSC_PORT_LEN];
sprintf(port, "%d", tcp_port);
svr = bsc_new_w_defaults(host, port, BSC_DEFAULT_TUBE, bsc_error_callback, errorstr);
return svr;
}
/* }}} */
void bsc_server_free(bsc *svr TSRMLS_DC) /* {{{ */
{
bsc_free(svr);
}
/* }}} */
static void bsc_pool_init_hash(bsc_pool_t *pool TSRMLS_DC) /* {{{ */
{
bsc_hash_function_t *hash;
switch (BSC_STANDARD_HASH) {
case BSC_CONSISTENT_HASH:
pool->hash = &bsc_consistent_hash;
break;
default:
pool->hash = &bsc_standard_hash;
}
switch (BSC_HASH_CRC32) {
case BSC_HASH_FNV1A:
hash = &bsc_hash_fnv1a;
break;
default:
hash = &bsc_hash_crc32;
}
pool->hash_state = pool->hash->create_state(hash);
}
/* }}} */
bsc_pool_t *bsc_pool_new(TSRMLS_D) /* {{{ */
{
bsc_pool_t *pool = emalloc(sizeof(bsc_pool_t));
memset(pool, 0, sizeof(*pool));
bsc_pool_init_hash(pool TSRMLS_CC);
return pool;
}
/* }}} */
void bsc_pool_free(bsc_pool_t *pool TSRMLS_DC) /* {{{ */
{
int i;
for (i=0; i<pool->num_servers; i++) {
if (pool->servers[i] != NULL) {
bsc_server_free(pool->servers[i]);
pool->servers[i] = NULL;
}
}
if (pool->num_servers) {
efree(pool->servers);
}
pool->hash->free_state(pool->hash_state);
efree(pool);
}
/* }}} */
void bsc_pool_add(bsc_pool_t *pool, bsc *svr, unsigned int weight) /*
adds a server to the pool and hash strategy {{{ */
{
pool->hash->add_server(pool->hash_state, svr, weight);
pool->servers = erealloc(pool->servers, sizeof(*pool->servers) * (pool->num_servers + 1));
pool->servers[pool->num_servers] = svr;
/* store the smallest timeout for any server */
/*
if (!pool->num_servers || timeval_to_double(bsc->timeout) < timeval_to_double(pool->timeout)) {
pool->timeout = bsc->timeout;
}
*/
pool->num_servers++;
}
/* }}} */
void bsc_pool_close(bsc_pool_t *pool TSRMLS_DC) /*
disconnects and removes all servers in the pool {{{ */
{
if (pool->num_servers) {
int i;
for (i=0; i<pool->num_servers; i++) {
bsc_server_free(pool->servers[i] TSRMLS_CC);
}
efree(pool->servers);
pool->servers = NULL;
pool->num_servers = 0;
/* reallocate the hash strategy state */
pool->hash->free_state(pool->hash_state);
bsc_pool_init_hash(pool TSRMLS_CC);
}
}
/* }}} */
bsc *bsc_pool_find(bsc_pool_t *pool, const char *key, unsigned int key_len TSRMLS_DC) /*
maps a key to a non-failed server {{{ */
{
bsc *svr = pool->hash->find_server(pool->hash_state, key, key_len TSRMLS_CC);
return svr;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/