forked from mkoppanen/php-tokyo_tyrant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_pool.c
149 lines (122 loc) · 3.84 KB
/
server_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
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Tokyo tyrant |
+----------------------------------------------------------------------+
| Copyright (c) 2009 Mikko Koppanen |
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Mikko Koppanen <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_tokyo_tyrant_session.h"
#include "ext/standard/php_rand.h"
php_tt_server *php_tt_server_init(char *host, int port TSRMLS_DC)
{
php_tt_server *server = emalloc(sizeof(server));
server->host = estrdup(host);
server->port = port;
return server;
}
void php_tt_server_deinit(php_tt_server *server TSRMLS_DC)
{
efree(server->host);
efree(server);
}
php_tt_server_pool *php_tt_pool_init(TSRMLS_D)
{
php_tt_server_pool *pool = emalloc(sizeof(php_tt_server_pool));
pool->servers = emalloc(sizeof(php_tt_server *));
pool->num_servers = 0;
return pool;
}
void php_tt_pool_append(php_tt_server_pool *pool, php_tt_server *server TSRMLS_DC)
{
pool->servers = erealloc(pool->servers, (pool->num_servers + 1) * sizeof(php_tt_server));
pool->servers[pool->num_servers] = server;
pool->num_servers += 1;
}
void php_tt_pool_append2(php_tt_server_pool *pool, char *host, int port TSRMLS_DC)
{
php_tt_server *server = emalloc(sizeof(php_tt_server));
server->host = estrdup(host);
server->port = port;
php_tt_pool_append(pool, server);
}
void php_tt_pool_deinit(php_tt_server_pool *pool TSRMLS_DC)
{
if (pool->num_servers > 0) {
int i;
for (i = 0; i < pool->num_servers; i++) {
php_tt_server_deinit(pool->servers[i]);
pool->servers[i] = NULL;
}
efree(pool->servers);
}
efree(pool);
}
php_tt_server_pool *php_tt_pool_init2(const char *save_path TSRMLS_DC)
{
php_tt_server_pool *pool = php_tt_pool_init(TSRMLS_C);
php_url *url = NULL;
char *ptr = NULL, *pch = NULL;
ptr = estrdup(save_path);
pch = strtok(ptr, ",");
while (pch != NULL) {
url = php_url_parse(pch);
if (!url) {
if (ptr) {
efree(ptr);
}
return NULL;
}
php_tt_pool_append2(pool, url->host, url->port TSRMLS_CC);
php_url_free(url);
url = NULL;
pch = strtok(NULL, ",");
}
efree(ptr);
return pool;
}
int php_tt_pool_map(php_tt_server_pool *pool, char *key TSRMLS_DC)
{
php_tt_server *server;
int idx = -1;
idx = php_rand(TSRMLS_C) % pool->num_servers;
server = pool->servers[idx];
/* Server has failed. Get a new random server */
if (!php_tt_server_ok(server->host, server->port TSRMLS_CC)) {
int i, x = php_rand(TSRMLS_C) % pool->num_servers;
for (i = x; i < pool->num_servers; i++) {
if (i != idx) {
server = pool->servers[i];
if (php_tt_server_ok(server->host, server->port TSRMLS_CC)) {
return i;
}
}
}
for (i = x; i >= 0; i--) {
if (i != idx) {
server = pool->servers[i];
if (php_tt_server_ok(server->host, server->port TSRMLS_CC)) {
return i;
}
}
}
idx = -1;
}
return idx;
}
php_tt_server *php_tt_pool_get_server(php_tt_server_pool *pool, int idx TSRMLS_DC)
{
if (idx >= pool->num_servers || idx < 0) {
return NULL;
}
return pool->servers[idx];
}