-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpool.c
More file actions
1417 lines (1143 loc) · 37.6 KB
/
pool.c
File metadata and controls
1417 lines (1143 loc) · 37.6 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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: |
| https://www.php.net/license/3_01.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. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
#include "pool.h"
#include "php_async.h"
#include "exceptions.h"
#include "scheduler.h"
#include "coroutine.h"
#include "pool_arginfo.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_enum.h"
#include "internal/zval_circular_buffer.h"
/**
* Pool - Resource pool with automatic lifecycle management.
*
* Architecture (similar to Channel):
* - Waiting queue for coroutines waiting for resources
* - zend_async_resume_when registers with waker (so SUSPEND works)
* - We control wake order via our queue
* - On wake, coroutine retries and takes resource from pool
*/
#define METHOD(name) PHP_METHOD(Async_Pool, name)
#define THIS_POOL_OBJ ASYNC_POOL_OBJ_FROM_OBJ(Z_OBJ_P(ZEND_THIS))
#define THIS_POOL (THIS_POOL_OBJ->base)
#define THROW_IF_CLOSED(pool) \
if (UNEXPECTED(ZEND_ASYNC_POOL_IS_CLOSED(&(pool)->base))) { \
zend_throw_exception(async_ce_pool_exception, "Pool is closed", 0); \
RETURN_THROWS(); \
}
#define ENSURE_COROUTINE_CONTEXT \
if (UNEXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == NULL)) { \
async_scheduler_launch(); \
if (UNEXPECTED(EG(exception) != NULL)) { \
RETURN_THROWS(); \
} \
}
zend_class_entry *async_ce_pool = NULL;
zend_class_entry *async_ce_pool_exception = NULL;
static zend_object_handlers async_pool_handlers;
///////////////////////////////////////////////////////////////////////////////
// Queue operations (copied from channel.c)
///////////////////////////////////////////////////////////////////////////////
static void pool_queue_init(zend_async_callbacks_vector_t *queue)
{
queue->data = NULL;
queue->length = 0;
queue->capacity = 0;
}
static void pool_queue_free(zend_async_callbacks_vector_t *queue)
{
if (queue->data) {
efree(queue->data);
queue->data = NULL;
}
}
static void pool_queue_push(zend_async_callbacks_vector_t *queue, zend_async_pool_waiter_t *waiter)
{
if (queue->length >= queue->capacity) {
uint32_t new_capacity = queue->capacity ? queue->capacity * 2 : 4;
queue->data = erealloc(queue->data, new_capacity * sizeof(void *));
queue->capacity = new_capacity;
}
waiter->callback.base.ref_count++;
queue->data[queue->length++] = (zend_async_event_callback_t *) waiter;
}
static zend_async_pool_waiter_t *pool_queue_pop(zend_async_callbacks_vector_t *queue)
{
if (queue->length == 0) {
return NULL;
}
zend_async_pool_waiter_t *waiter = (zend_async_pool_waiter_t *) queue->data[0];
queue->length--;
/* Swap with last element - O(1) */
queue->data[0] = queue->data[queue->length];
return waiter;
}
static bool pool_queue_remove(zend_async_callbacks_vector_t *queue, zend_async_pool_waiter_t *waiter)
{
for (uint32_t i = 0; i < queue->length; i++) {
if (queue->data[i] == (zend_async_event_callback_t *) waiter) {
queue->length--;
queue->data[i] = queue->data[queue->length];
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Resource creation/destruction
///////////////////////////////////////////////////////////////////////////////
static bool pool_create_resource(async_pool_t *pool, zval *result)
{
zend_async_pool_t *base = &pool->base;
/* Internal C handler */
if (base->handler_flags & ZEND_ASYNC_POOL_F_FACTORY_INTERNAL) {
if (base->factory.internal == NULL) {
return false;
}
return base->factory.internal(base, result);
}
/* PHP callable */
if (base->factory.fcall == NULL) {
return false;
}
zval retval;
ZVAL_UNDEF(&retval);
base->factory.fcall->fci.retval = &retval;
if (zend_call_function(&base->factory.fcall->fci, &base->factory.fcall->fci_cache) == FAILURE) {
return false;
}
if (EG(exception)) {
zval_ptr_dtor(&retval);
return false;
}
ZVAL_COPY_VALUE(result, &retval);
return true;
}
static void pool_destroy_resource(async_pool_t *pool, zval *resource)
{
zend_async_pool_t *base = &pool->base;
/* Internal C handler */
if (base->handler_flags & ZEND_ASYNC_POOL_F_DESTRUCTOR_INTERNAL) {
if (base->destructor.internal != NULL) {
base->destructor.internal(base, resource);
}
zval_ptr_dtor(resource);
return;
}
/* PHP callable */
if (base->destructor.fcall != NULL) {
zval retval;
ZVAL_UNDEF(&retval);
zval args[1];
ZVAL_COPY(&args[0], resource);
/* Use local copy of fci to avoid corrupting the stored structure */
zend_fcall_info fci = base->destructor.fcall->fci;
fci.retval = &retval;
fci.param_count = 1;
fci.params = args;
zend_call_function(&fci, &base->destructor.fcall->fci_cache);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&retval);
}
zval_ptr_dtor(resource);
}
///////////////////////////////////////////////////////////////////////////////
// Callback helpers
///////////////////////////////////////////////////////////////////////////////
/* Call beforeAcquire callback, returns true if resource is OK */
static bool pool_call_before_acquire(async_pool_t *pool, zval *resource)
{
zend_async_pool_t *base = &pool->base;
/* Internal C handler */
if (base->handler_flags & ZEND_ASYNC_POOL_F_BEFORE_ACQUIRE_INTERNAL) {
if (base->before_acquire.internal == NULL) {
return true;
}
return base->before_acquire.internal(base, resource);
}
/* PHP callable */
if (base->before_acquire.fcall == NULL) {
return true;
}
zval retval;
ZVAL_UNDEF(&retval);
zval args[1];
ZVAL_COPY(&args[0], resource);
/* Use local copy of fci to avoid corrupting the stored structure */
zend_fcall_info fci = base->before_acquire.fcall->fci;
fci.retval = &retval;
fci.param_count = 1;
fci.params = args;
zend_call_function(&fci, &base->before_acquire.fcall->fci_cache);
zval_ptr_dtor(&args[0]);
bool result = true;
if (EG(exception)) {
result = false;
zend_clear_exception();
} else if (Z_TYPE(retval) == IS_FALSE) {
result = false;
}
zval_ptr_dtor(&retval);
return result;
}
/* Call beforeRelease callback, returns true if resource should be returned to pool */
static bool pool_call_before_release(async_pool_t *pool, zval *resource)
{
zend_async_pool_t *base = &pool->base;
/* Internal C handler */
if (base->handler_flags & ZEND_ASYNC_POOL_F_BEFORE_RELEASE_INTERNAL) {
if (base->before_release.internal == NULL) {
return true;
}
return base->before_release.internal(base, resource);
}
/* PHP callable */
if (base->before_release.fcall == NULL) {
return true;
}
zval retval;
ZVAL_UNDEF(&retval);
zval args[1];
ZVAL_COPY(&args[0], resource);
/* Use local copy of fci to avoid corrupting the stored structure */
zend_fcall_info fci = base->before_release.fcall->fci;
fci.retval = &retval;
fci.param_count = 1;
fci.params = args;
zend_call_function(&fci, &base->before_release.fcall->fci_cache);
zval_ptr_dtor(&args[0]);
bool result = true;
if (EG(exception)) {
result = false;
zend_clear_exception();
} else if (Z_TYPE(retval) == IS_FALSE) {
result = false;
}
zval_ptr_dtor(&retval);
return result;
}
/* Call strategy reportSuccess */
static void pool_strategy_report_success(async_pool_t *pool)
{
zend_async_pool_t *base = &pool->base;
/* Internal C strategy */
if (base->handler_flags & ZEND_ASYNC_POOL_F_STRATEGY_INTERNAL) {
if (base->strategy.internal && base->strategy.internal->report_success) {
base->strategy.internal->report_success(base);
}
return;
}
/* PHP strategy */
if (base->strategy.object == NULL) {
return;
}
zval retval, source;
ZVAL_UNDEF(&retval);
if (base->wrapper) {
ZVAL_OBJ(&source, base->wrapper);
} else {
ZVAL_NULL(&source);
}
zend_call_method_with_1_params(base->strategy.object, NULL, NULL, "reportSuccess", &retval, &source);
if (EG(exception)) {
zend_clear_exception();
}
zval_ptr_dtor(&retval);
}
/* Call strategy reportFailure */
static void pool_strategy_report_failure(async_pool_t *pool, zend_object *error)
{
zend_async_pool_t *base = &pool->base;
/* Internal C strategy */
if (base->handler_flags & ZEND_ASYNC_POOL_F_STRATEGY_INTERNAL) {
if (base->strategy.internal && base->strategy.internal->report_failure) {
base->strategy.internal->report_failure(base, error);
}
return;
}
/* PHP strategy */
if (base->strategy.object == NULL) {
return;
}
zval retval, source, error_zval;
ZVAL_UNDEF(&retval);
if (base->wrapper) {
ZVAL_OBJ(&source, base->wrapper);
} else {
ZVAL_NULL(&source);
}
if (error) {
ZVAL_OBJ(&error_zval, error);
} else {
/* Create a generic exception if none provided */
zend_object *ex = zend_throw_exception(NULL, "Resource validation failed", 0);
zend_clear_exception();
ZVAL_OBJ(&error_zval, ex);
}
zend_call_method_with_2_params(base->strategy.object, NULL, NULL, "reportFailure", &retval, &source, &error_zval);
if (EG(exception)) {
zend_clear_exception();
}
zval_ptr_dtor(&retval);
}
///////////////////////////////////////////////////////////////////////////////
// Wait/Wake operations (like channel)
///////////////////////////////////////////////////////////////////////////////
static void pool_wait_for_resource(async_pool_t *pool, zend_long timeout_ms)
{
zend_async_pool_t *base = &pool->base;
zend_async_pool_waiter_t *waiter = ecalloc(1, sizeof(zend_async_pool_waiter_t));
waiter->callback.base.callback = zend_async_waker_callback_resolve;
waiter->callback.base.ref_count = 1;
waiter->callback.coroutine = ZEND_ASYNC_CURRENT_COROUTINE;
waiter->callback.event = &base->event;
pool_queue_push(&pool->waiters, waiter);
zend_async_resume_when(
ZEND_ASYNC_CURRENT_COROUTINE, &base->event, false, zend_async_waker_callback_resolve, &waiter->callback);
if (timeout_ms > 0) {
zend_async_resume_when(ZEND_ASYNC_CURRENT_COROUTINE,
&ZEND_ASYNC_NEW_TIMER_EVENT(timeout_ms, false)->base,
true,
zend_async_waker_callback_timeout,
NULL);
}
ZEND_ASYNC_SUSPEND();
/* Cleanup after waking up */
if (pool_queue_remove(&pool->waiters, waiter)) {
/* Was still in queue (timeout/close case) - release queue's ref */
ZEND_ASYNC_EVENT_CALLBACK_RELEASE(&waiter->callback.base);
}
/* Release our initial ref */
ZEND_ASYNC_EVENT_CALLBACK_RELEASE(&waiter->callback.base);
}
static bool pool_wake_waiter(async_pool_t *pool)
{
zend_async_pool_t *base = &pool->base;
zend_async_pool_waiter_t *waiter = pool_queue_pop(&pool->waiters);
if (waiter == NULL) {
return false;
}
base->event.del_callback(&base->event, &waiter->callback.base);
waiter->callback.base.callback(&base->event, &waiter->callback.base, NULL, NULL);
ZEND_ASYNC_EVENT_CALLBACK_RELEASE(&waiter->callback.base);
return true;
}
static void pool_wake_all_with_exception(async_pool_t *pool, zend_object *exception)
{
zend_async_pool_t *base = &pool->base;
zend_async_pool_waiter_t *waiter;
while ((waiter = pool_queue_pop(&pool->waiters)) != NULL) {
base->event.del_callback(&base->event, &waiter->callback.base);
waiter->callback.base.callback(&base->event, &waiter->callback.base, NULL, exception);
ZEND_ASYNC_EVENT_CALLBACK_RELEASE(&waiter->callback.base);
}
}
///////////////////////////////////////////////////////////////////////////////
// Event handlers
///////////////////////////////////////////////////////////////////////////////
static bool pool_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_push(event, callback);
}
static bool pool_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_remove(event, callback);
}
static bool pool_dispose(zend_async_event_t *event)
{
async_pool_t *pool = (async_pool_t *) event;
zend_async_pool_t *base = &pool->base;
/* Close if not already closed */
if (!ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
zend_async_pool_close(pool);
}
/* Free callbacks */
zend_async_callbacks_free(&base->event);
pool_queue_free(&pool->waiters);
/* Free circular buffer */
circular_buffer_dtor(&pool->idle);
/* Free fcall structures (only if not internal handlers) */
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_FACTORY_INTERNAL) && base->factory.fcall) {
zend_fcall_release(base->factory.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_DESTRUCTOR_INTERNAL) && base->destructor.fcall) {
zend_fcall_release(base->destructor.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL) && base->healthcheck.fcall) {
zend_fcall_release(base->healthcheck.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_BEFORE_ACQUIRE_INTERNAL) && base->before_acquire.fcall) {
zend_fcall_release(base->before_acquire.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_BEFORE_RELEASE_INTERNAL) && base->before_release.fcall) {
zend_fcall_release(base->before_release.fcall);
}
/* Release strategy object */
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_STRATEGY_INTERNAL) && base->strategy.object) {
OBJ_RELEASE(base->strategy.object);
base->strategy.object = NULL;
}
efree(pool);
return true;
}
static bool pool_start(zend_async_event_t *event)
{
return true;
}
static bool pool_stop(zend_async_event_t *event)
{
return true;
}
static zend_string *pool_info(zend_async_event_t *event)
{
const async_pool_t *pool = (async_pool_t *) event;
return zend_strpprintf(0,
"Pool(idle=%zu, active=%u, max=%u)",
circular_buffer_count(&pool->idle),
pool->active_count,
pool->base.max_size);
}
static void pool_event_init(async_pool_t *pool)
{
zend_async_event_t *event = &pool->base.event;
memset(event, 0, sizeof(zend_async_event_t));
event->ref_count = 1;
event->add_callback = pool_add_callback;
event->del_callback = pool_del_callback;
event->start = pool_start;
event->stop = pool_stop;
event->dispose = pool_dispose;
event->info = pool_info;
}
///////////////////////////////////////////////////////////////////////////////
// Healthcheck timer
///////////////////////////////////////////////////////////////////////////////
/* Call healthcheck, returns true if resource is healthy */
static bool pool_call_healthcheck(async_pool_t *pool, zval *resource)
{
zend_async_pool_t *base = &pool->base;
/* Internal C handler */
if (base->handler_flags & ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL) {
if (base->healthcheck.internal == NULL) {
return true;
}
return base->healthcheck.internal(base, resource);
}
/* PHP callable */
if (base->healthcheck.fcall == NULL) {
return true;
}
zval retval;
ZVAL_UNDEF(&retval);
zval args[1];
ZVAL_COPY(&args[0], resource);
/* Use local copy of fci to avoid corrupting the stored structure */
zend_fcall_info fci = base->healthcheck.fcall->fci;
fci.retval = &retval;
fci.param_count = 1;
fci.params = args;
zend_call_function(&fci, &base->healthcheck.fcall->fci_cache);
zval_ptr_dtor(&args[0]);
bool is_healthy = true;
if (EG(exception)) {
is_healthy = false;
zend_clear_exception();
} else if (Z_TYPE(retval) == IS_FALSE) {
is_healthy = false;
}
zval_ptr_dtor(&retval);
return is_healthy;
}
static void pool_healthcheck_callback_dispose(zend_async_event_callback_t *callback, zend_async_event_t *event)
{
/* No-op: callback is embedded in async_pool_t, not heap-allocated */
}
static void pool_healthcheck_timer_callback(zend_async_event_t *timer_event,
zend_async_event_callback_t *callback,
void *result,
zend_object *exception)
{
async_pool_t *pool = (async_pool_t *) ((char *) callback - offsetof(async_pool_t, healthcheck_callback));
zend_async_pool_t *base = &pool->base;
if (ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
return;
}
/* Check if healthcheck is set */
bool has_healthcheck = (base->handler_flags & ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL)
? (base->healthcheck.internal != NULL)
: (base->healthcheck.fcall != NULL);
if (!has_healthcheck) {
return;
}
/* Process all idle resources */
size_t count = circular_buffer_count(&pool->idle);
for (size_t i = 0; i < count; i++) {
zval resource;
if (zval_circular_buffer_pop(&pool->idle, &resource) != SUCCESS) {
break;
}
bool is_healthy = pool_call_healthcheck(pool, &resource);
if (is_healthy) {
/* Resource is healthy - return to buffer */
zval_circular_buffer_push(&pool->idle, &resource, true);
zval_ptr_dtor(&resource);
} else {
/* Resource is dead - destroy it */
pool_destroy_resource(pool, &resource);
if (UNEXPECTED(EG(exception))) {
break;
}
/* Create new one to maintain min_size */
uint32_t total = ASYNC_POOL_TOTAL(pool);
if (total < base->min_size) {
zval new_resource;
if (pool_create_resource(pool, &new_resource)) {
zval_circular_buffer_push(&pool->idle, &new_resource, true);
zval_ptr_dtor(&new_resource);
} else if (UNEXPECTED(EG(exception))) {
break;
}
}
}
}
}
static void pool_start_healthcheck_timer(async_pool_t *pool)
{
zend_async_pool_t *base = &pool->base;
if (pool->healthcheck_interval_ms == 0) {
return;
}
/* Check if healthcheck is set */
const bool has_healthcheck = (base->handler_flags & ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL)
? (base->healthcheck.internal != NULL)
: (base->healthcheck.fcall != NULL);
if (false == has_healthcheck) {
return;
}
pool->healthcheck_timer = ZEND_ASYNC_NEW_TIMER_EVENT(pool->healthcheck_interval_ms, true);
if (pool->healthcheck_timer == NULL) {
return;
}
/* Use inline callback embedded in pool structure */
pool->healthcheck_callback.ref_count = 1;
pool->healthcheck_callback.callback = pool_healthcheck_timer_callback;
pool->healthcheck_callback.dispose = pool_healthcheck_callback_dispose;
pool->healthcheck_timer->base.add_callback(&pool->healthcheck_timer->base, &pool->healthcheck_callback);
pool->healthcheck_timer->base.start(&pool->healthcheck_timer->base);
}
static void pool_stop_healthcheck_timer(async_pool_t *pool)
{
if (pool->healthcheck_timer != NULL) {
pool->healthcheck_timer->base.stop(&pool->healthcheck_timer->base);
ZEND_ASYNC_EVENT_RELEASE(&pool->healthcheck_timer->base);
pool->healthcheck_timer = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
// C API implementation
///////////////////////////////////////////////////////////////////////////////
/* Create pool with PHP callables (fcall) */
async_pool_t *zend_async_pool_create(zend_fcall_t *factory,
zend_fcall_t *destructor,
zend_fcall_t *healthcheck,
zend_fcall_t *before_acquire,
zend_fcall_t *before_release,
uint32_t min_size,
uint32_t max_size,
uint32_t healthcheck_interval_ms)
{
async_pool_t *pool = ecalloc(1, sizeof(async_pool_t));
zend_async_pool_t *base = &pool->base;
/* Event init */
pool_event_init(pool);
/* Callbacks - all fcall (PHP callables) */
base->handler_flags = 0;
base->factory.fcall = factory;
base->destructor.fcall = destructor;
base->healthcheck.fcall = healthcheck;
base->before_acquire.fcall = before_acquire;
base->before_release.fcall = before_release;
/* Config */
base->min_size = min_size;
base->max_size = max_size;
pool->healthcheck_interval_ms = healthcheck_interval_ms;
pool->active_count = 0;
/* Init idle buffer */
circular_buffer_ctor(&pool->idle, min_size > 0 ? min_size : 4, sizeof(zval), &zend_std_persistent_allocator);
/* Init waiters queue */
pool_queue_init(&pool->waiters);
/* Pre-warm: create min_size resources */
for (uint32_t i = 0; i < min_size; i++) {
zval resource;
if (pool_create_resource(pool, &resource)) {
zval_circular_buffer_push(&pool->idle, &resource, true);
zval_ptr_dtor(&resource);
} else if (UNEXPECTED(EG(exception))) {
/* Stop on first error */
break;
}
}
/* Start healthcheck timer */
pool_start_healthcheck_timer(pool);
return pool;
}
/* Create pool with internal C handlers */
async_pool_t *zend_async_pool_create_internal(zend_async_pool_factory_fn factory,
zend_async_pool_destructor_fn destructor,
zend_async_pool_healthcheck_fn healthcheck,
zend_async_pool_before_acquire_fn before_acquire,
zend_async_pool_before_release_fn before_release,
uint32_t min_size,
uint32_t max_size,
uint32_t healthcheck_interval_ms)
{
async_pool_t *pool = ecalloc(1, sizeof(async_pool_t));
zend_async_pool_t *base = &pool->base;
/* Event init */
pool_event_init(pool);
/* Callbacks - all internal C handlers */
base->handler_flags = ZEND_ASYNC_POOL_F_FACTORY_INTERNAL | ZEND_ASYNC_POOL_F_DESTRUCTOR_INTERNAL |
ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL | ZEND_ASYNC_POOL_F_BEFORE_ACQUIRE_INTERNAL |
ZEND_ASYNC_POOL_F_BEFORE_RELEASE_INTERNAL;
base->factory.internal = factory;
base->destructor.internal = destructor;
base->healthcheck.internal = healthcheck;
base->before_acquire.internal = before_acquire;
base->before_release.internal = before_release;
/* Config */
base->min_size = min_size;
base->max_size = max_size;
pool->healthcheck_interval_ms = healthcheck_interval_ms;
pool->active_count = 0;
/* Init idle buffer */
circular_buffer_ctor(&pool->idle, min_size > 0 ? min_size : 4, sizeof(zval), &zend_std_persistent_allocator);
/* Init waiters queue */
pool_queue_init(&pool->waiters);
/* Pre-warm: create min_size resources */
for (uint32_t i = 0; i < min_size; i++) {
zval resource;
if (pool_create_resource(pool, &resource)) {
zval_circular_buffer_push(&pool->idle, &resource, true);
zval_ptr_dtor(&resource);
} else if (EG(exception)) {
/* Stop on first error */
break;
}
}
/* Start healthcheck timer */
pool_start_healthcheck_timer(pool);
return pool;
}
bool zend_async_pool_acquire(async_pool_t *pool, zval *result, zend_long timeout_ms)
{
const zend_async_pool_t *base = &pool->base;
retry:
/* 1. Closed? */
if (ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
zend_throw_exception(async_ce_pool_exception, "Pool is closed", 0);
return false;
}
/* 2. Circuit breaker check */
if (base->circuit_state == ZEND_ASYNC_CIRCUIT_STATE_INACTIVE) {
async_throw_service_unavailable("Service is unavailable (circuit breaker is open)");
return false;
}
/* 2. Have idle resource? */
if (!circular_buffer_is_empty(&pool->idle)) {
zval resource;
zval_circular_buffer_pop(&pool->idle, &resource);
/* beforeAcquire check (if set) */
if (!pool_call_before_acquire(pool, &resource)) {
/* Check failed - destroy and try next */
pool_destroy_resource(pool, &resource);
if (UNEXPECTED(EG(exception))) {
return false;
}
goto retry;
}
ZVAL_COPY_VALUE(result, &resource);
pool->active_count++;
return true;
}
/* 3. Can create new? Reserve slot BEFORE factory call — factory may yield,
* allowing other coroutines to pass the same check concurrently. */
const uint32_t total = ASYNC_POOL_TOTAL(pool);
if (total < base->max_size) {
pool->active_count++;
if (pool_create_resource(pool, result)) {
return true;
}
pool->active_count--;
if (UNEXPECTED(EG(exception))) {
return false;
}
}
/* 4. Wait - like channel */
pool_wait_for_resource(pool, timeout_ms);
if (UNEXPECTED(EG(exception))) {
return false;
}
goto retry;
}
bool zend_async_pool_try_acquire(async_pool_t *pool, zval *result)
{
zend_async_pool_t *base = &pool->base;
retry:
if (ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
return false;
}
/* Circuit breaker check */
if (base->circuit_state == ZEND_ASYNC_CIRCUIT_STATE_INACTIVE) {
return false;
}
/* Have idle? */
if (!circular_buffer_is_empty(&pool->idle)) {
zval resource;
zval_circular_buffer_pop(&pool->idle, &resource);
/* beforeAcquire check */
if (!pool_call_before_acquire(pool, &resource)) {
pool_destroy_resource(pool, &resource);
goto retry;
}
ZVAL_COPY_VALUE(result, &resource);
pool->active_count++;
return true;
}
/* Can create new? Reserve slot before factory (may yield). */
const uint32_t total = ASYNC_POOL_TOTAL(pool);
if (total < base->max_size) {
pool->active_count++;
if (pool_create_resource(pool, result)) {
return true;
}
pool->active_count--;
}
/* No resource available */
return false;
}
void zend_async_pool_release(async_pool_t *pool, zval *resource)
{
pool->active_count--;
/* beforeRelease callback (if set) */
/* Returns false = resource is broken, destroy it */
if (!pool_call_before_release(pool, resource)) {
/* Report failure to strategy */
pool_strategy_report_failure(pool, NULL);
pool_destroy_resource(pool, resource);
return;
}
/* Report success to strategy */
pool_strategy_report_success(pool);
/* Closed? Destroy resource */
if (ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
pool_destroy_resource(pool, resource);
return;
}
/* Have waiters? Wake first one - it will take resource in retry */
if (pool_wake_waiter(pool)) {
/* Return resource to buffer, waiter will take it */
zval_circular_buffer_push(&pool->idle, resource, true);
return;
}
/* No waiters - just return to buffer */
zval_circular_buffer_push(&pool->idle, resource, true);
}
void zend_async_pool_close(async_pool_t *pool)
{
zend_async_pool_t *base = &pool->base;
if (ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
return;
}
ZEND_ASYNC_EVENT_SET_CLOSED(&base->event);
/* Stop healthcheck timer */
pool_stop_healthcheck_timer(pool);
/* Wake all waiters with exception */
zend_object *ex = async_new_exception(async_ce_pool_exception, "Pool is closed");
pool_wake_all_with_exception(pool, ex);
OBJ_RELEASE(ex);
/* Destroy all idle resources — must destroy all even if destructor throws */
zval resource;
zend_object *first_exception = NULL;
while (!circular_buffer_is_empty(&pool->idle)) {
if (zval_circular_buffer_pop(&pool->idle, &resource) == SUCCESS) {
pool_destroy_resource(pool, &resource);
if (UNEXPECTED(EG(exception))) {
zend_object *exception = EG(exception);
GC_ADDREF(exception);
zend_clear_exception();
if (first_exception != NULL) {
zend_exception_set_previous(exception, first_exception);
}
first_exception = exception;
}
}
}
if (first_exception != NULL) {
zval ex_zval;
ZVAL_OBJ(&ex_zval, first_exception);
zend_throw_exception_object(&ex_zval);
}
}
void zend_async_pool_destroy(async_pool_t *pool)
{
zend_async_pool_t *base = &pool->base;
/* Close if not already closed */
if (!ZEND_ASYNC_POOL_IS_CLOSED(pool)) {
zend_async_pool_close(pool);
}
/* Free callbacks */
zend_async_callbacks_free(&base->event);
pool_queue_free(&pool->waiters);
/* Free circular buffer */
circular_buffer_dtor(&pool->idle);
/* Free fcall structures (only if not internal handlers) */
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_FACTORY_INTERNAL) && base->factory.fcall) {
zend_fcall_release(base->factory.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_DESTRUCTOR_INTERNAL) && base->destructor.fcall) {
zend_fcall_release(base->destructor.fcall);
}
if (!(base->handler_flags & ZEND_ASYNC_POOL_F_HEALTHCHECK_INTERNAL) && base->healthcheck.fcall) {