forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_process_pool.c
445 lines (383 loc) · 12.4 KB
/
swoole_process_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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache 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.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 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. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_pool_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_pool_construct, 0, 0, 1)
ZEND_ARG_INFO(0, worker_num)
ZEND_ARG_INFO(0, ipc_type)
ZEND_ARG_INFO(0, msgqueue_key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_pool_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_pool_listen, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, backlog)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_pool_write, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
static PHP_METHOD(swoole_process_pool, __construct);
static PHP_METHOD(swoole_process_pool, __destruct);
static PHP_METHOD(swoole_process_pool, on);
static PHP_METHOD(swoole_process_pool, listen);
static PHP_METHOD(swoole_process_pool, write);
static PHP_METHOD(swoole_process_pool, start);
static const zend_function_entry swoole_process_pool_methods[] =
{
PHP_ME(swoole_process_pool, __construct, arginfo_swoole_process_pool_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_process_pool, __destruct, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_process_pool, on, arginfo_swoole_process_pool_on, ZEND_ACC_PUBLIC)
PHP_ME(swoole_process_pool, listen, arginfo_swoole_process_pool_listen, ZEND_ACC_PUBLIC)
PHP_ME(swoole_process_pool, write, arginfo_swoole_process_pool_write, ZEND_ACC_PUBLIC)
PHP_ME(swoole_process_pool, start, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
typedef struct
{
zval *onWorkerStart;
zval *onWorkerStop;
zval *onMessage;
zval _onWorkerStart;
zval _onWorkerStop;
zval _onMessage;
} process_pool_property;
static zend_class_entry swoole_process_pool_ce;
static zend_class_entry *swoole_process_pool_class_entry_ptr;
void swoole_process_pool_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_process_pool_ce, "swoole_process_pool", "Swoole\\Process\\Pool", swoole_process_pool_methods);
swoole_process_pool_class_entry_ptr = zend_register_internal_class(&swoole_process_pool_ce TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_process_pool, "Swoole\\Process\\Pool");
}
static void php_swoole_process_pool_onWorkerStart(swProcessPool *pool, int worker_id)
{
SWOOLE_GET_TSRMLS;
zval *zobject = (zval *) pool->ptr;
zval *zworker_id;
zval *retval = NULL;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, worker_id);
zval **args[2];
args[0] = &zobject;
args[1] = &zworker_id;
process_pool_property *pp = swoole_get_property(zobject, 0);
if (pp->onWorkerStart == NULL)
{
return;
}
if (sw_call_user_function_ex(EG(function_table), NULL, pp->onWorkerStart, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerStart handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_process_pool_onMessage(swProcessPool *pool, char *data, uint32_t length)
{
SWOOLE_GET_TSRMLS;
zval *zobject = (zval *) pool->ptr;
zval *zdata;
zval *retval;
SW_MAKE_STD_ZVAL(zdata);
SW_ZVAL_STRINGL(zdata, data, length, 1);
zval **args[2];
args[0] = &zobject;
args[1] = &zdata;
process_pool_property *pp = swoole_get_property(zobject, 0);
if (sw_call_user_function_ex(EG(function_table), NULL, pp->onMessage, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerStart handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zdata);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_process_pool_onWorkerStop(swProcessPool *pool, int worker_id)
{
SWOOLE_GET_TSRMLS;
zval *zobject = (zval *) pool->ptr;
zval *zworker_id;
zval *retval = NULL;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, worker_id);
zval **args[2];
args[0] = &zobject;
args[1] = &zworker_id;
process_pool_property *pp = swoole_get_property(zobject, 0);
if (pp->onWorkerStop == NULL)
{
return;
}
if (sw_call_user_function_ex(EG(function_table), NULL, pp->onWorkerStop, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerStop handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_process_pool_signal_hanlder(int sig)
{
switch (sig)
{
case SIGTERM:
SwooleG.running = 0;
break;
case SIGUSR1:
case SIGUSR2:
SwooleGS->event_workers.reloading = 1;
SwooleGS->event_workers.reload_init = 0;
break;
default:
break;
}
}
static PHP_METHOD(swoole_process_pool, __construct)
{
long worker_num;
long ipc_type = SW_IPC_NONE;
long msgq_key = 0;
//only cli env
if (!SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "swoole_process_pool only can be used in PHP CLI mode.");
RETURN_FALSE;
}
if (SwooleG.serv)
{
swoole_php_fatal_error(E_ERROR, "swoole_process_pool cannot use in server process.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &worker_num, &ipc_type, &msgq_key) == FAILURE)
{
RETURN_FALSE;
}
if (worker_num <= 0)
{
zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "invalid worker_num");
RETURN_FALSE;
}
swProcessPool *pool = emalloc(sizeof(swProcessPool));
if (swProcessPool_create(pool, worker_num, 0, (key_t) msgq_key, ipc_type) < 0)
{
zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "failed to create process pool");
RETURN_FALSE;
}
if (ipc_type > 0)
{
if (swProcessPool_set_protocol(pool, 0, SW_BUFFER_INPUT_SIZE) < 0)
{
zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "failed to create process pool");
RETURN_FALSE;
}
}
pool->ptr = sw_zval_dup(getThis());
process_pool_property *pp = emalloc(sizeof(process_pool_property));
bzero(pp, sizeof(process_pool_property));
swoole_set_property(getThis(), 0, pp);
swoole_set_object(getThis(), pool);
}
static PHP_METHOD(swoole_process_pool, on)
{
char *name;
zend_size_t l_name;
zval *callback;
swProcessPool *pool = swoole_get_object(getThis());
if (pool->started > 0)
{
swoole_php_fatal_error(E_WARNING, "process pool is started. unable to register event callback function.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &l_name, &callback) == FAILURE)
{
return;
}
if (!php_swoole_is_callable(callback))
{
RETURN_FALSE;
}
process_pool_property *pp = swoole_get_property(getThis(), 0);
if (strncasecmp("WorkerStart", name, l_name) == 0)
{
if (pp->onWorkerStart)
{
sw_zval_ptr_dtor(&pp->onWorkerStart);
}
pp->onWorkerStart = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(pp->onWorkerStart, pp->_onWorkerStart);
RETURN_TRUE;
}
else if (strncasecmp("Message", name, l_name) == 0)
{
if (pool->ipc_mode == SW_IPC_NONE)
{
swoole_php_fatal_error(E_WARNING, "cannot set onMessage event with ipc_type=0.");
RETURN_TRUE;
}
if (pp->onMessage)
{
sw_zval_ptr_dtor(&pp->onMessage);
}
pp->onMessage = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(pp->onMessage, pp->_onMessage);
RETURN_TRUE;
}
else if (strncasecmp("WorkerStop", name, l_name) == 0)
{
if (pp->onWorkerStop)
{
sw_zval_ptr_dtor(&pp->onWorkerStop);
}
pp->onWorkerStop = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(pp->onWorkerStop, pp->_onWorkerStop);
RETURN_TRUE;
}
else
{
swoole_php_error(E_WARNING, "unknown event type[%s]", name);
RETURN_TRUE;
}
}
static PHP_METHOD(swoole_process_pool, listen)
{
char *host;
zend_size_t l_host;
long port;
long backlog = 2048;
swProcessPool *pool = swoole_get_object(getThis());
if (pool->started > 0)
{
swoole_php_fatal_error(E_WARNING, "process pool is started. unable to listen.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &host, &l_host, &port, &backlog) == FAILURE)
{
return;
}
if (pool->ipc_mode != SW_IPC_SOCKET)
{
swoole_php_fatal_error(E_WARNING, "unsupported ipc type[%d].", pool->ipc_mode);
RETURN_FALSE;
}
SwooleG.reuse_port = 0;
int ret;
//unix socket
if (strncasecmp("unix:/", host, 6) == 0)
{
ret = swProcessPool_create_unix_socket(pool, host + 5, backlog);
}
else
{
ret = swProcessPool_create_tcp_socket(pool, host, port, backlog);
}
SW_CHECK_RETURN(ret);
}
static PHP_METHOD(swoole_process_pool, write)
{
char *data;
zend_size_t length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &length) == FAILURE)
{
return;
}
swProcessPool *pool = swoole_get_object(getThis());
if (pool->ipc_mode != SW_IPC_SOCKET)
{
swoole_php_fatal_error(E_WARNING, "unsupported ipc type[%d].", pool->ipc_mode);
RETURN_FALSE;
}
if (length == 0)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swProcessPool_response(pool, data, length));
}
static PHP_METHOD(swoole_process_pool, start)
{
swProcessPool *pool = swoole_get_object(getThis());
if (pool->started)
{
swoole_php_fatal_error(E_WARNING, "process pool is started. unable to execute swoole_process_pool->start.");
RETURN_FALSE;
}
process_pool_property *pp = swoole_get_property(getThis(), 0);
SwooleG.use_signalfd = 0;
swSignal_add(SIGTERM, php_swoole_process_pool_signal_hanlder);
if (pool->ipc_mode > SW_IPC_NONE)
{
pool->onMessage = php_swoole_process_pool_onMessage;
}
else
{
if (pp->onWorkerStart == NULL)
{
swoole_php_fatal_error(E_ERROR, "require onWorkerStart callback");
RETURN_FALSE;
}
}
pool->onWorkerStart = php_swoole_process_pool_onWorkerStart;
pool->onWorkerStop = php_swoole_process_pool_onWorkerStop;
if (swProcessPool_start(pool) < 0)
{
RETURN_FALSE;
}
swProcessPool_wait(pool);
swProcessPool_shutdown(pool);
}
static PHP_METHOD(swoole_process_pool, __destruct)
{
swProcessPool *pool = swoole_get_object(getThis());
sw_zval_free(pool->ptr);
efree(pool);
process_pool_property *pp = swoole_get_property(getThis(), 0);
if (pp->onWorkerStart)
{
sw_zval_ptr_dtor(&pp->onWorkerStart);
}
if (pp->onMessage)
{
sw_zval_ptr_dtor(&pp->onMessage);
}
if (pp->onWorkerStop)
{
sw_zval_ptr_dtor(&pp->onWorkerStop);
}
efree(pp);
}