1
|
|
|
<?php |
2
|
|
|
namespace ResourcePool; |
3
|
|
|
|
4
|
|
|
use React\Promise\PromiseInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Josh Di Fabio <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @api |
10
|
|
|
*/ |
11
|
|
|
class Pool |
12
|
|
|
{ |
13
|
|
|
private $internal; |
14
|
|
|
|
15
|
|
|
public function __construct($size = null) |
16
|
|
|
{ |
17
|
|
|
$this->internal = new PoolInternal($size); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Allocates a single resource when one becomes available |
22
|
|
|
* |
23
|
|
|
* @return PartialAllocationPromise |
24
|
|
|
*/ |
25
|
|
|
public function allocateOne() |
26
|
|
|
{ |
27
|
|
|
return $this->internal->allocate(1); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Allocates the specified number of resources when they become available |
32
|
|
|
* |
33
|
|
|
* @param int $count |
34
|
|
|
* @return PartialAllocationPromise |
35
|
|
|
*/ |
36
|
|
|
public function allocate($count) |
37
|
|
|
{ |
38
|
|
|
return $this->internal->allocate($count); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Allocates all of the pool's resources when they become available |
43
|
|
|
* |
44
|
|
|
* @return AllocationPromise |
45
|
|
|
*/ |
46
|
|
|
public function allocateAll() |
47
|
|
|
{ |
48
|
|
|
return $this->internal->allocateAll(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the number of resources in the pool |
53
|
|
|
* |
54
|
|
|
* @param int $size |
55
|
|
|
*/ |
56
|
|
|
public function setSize($size) |
57
|
|
|
{ |
58
|
|
|
$this->internal->setSize($size); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the number of resources which are not currently allocated |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getAvailability() |
67
|
|
|
{ |
68
|
|
|
return $this->internal->getAvailability(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the number of resources which are currently allocated |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getUsage() |
77
|
|
|
{ |
78
|
|
|
return $this->internal->getUsage(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param callable|null $fulfilledHandler |
83
|
|
|
* @return PromiseInterface |
84
|
|
|
*/ |
85
|
|
|
public function whenNextIdle($fulfilledHandler = null) |
86
|
|
|
{ |
87
|
|
|
return $this->internal->whenNextIdle($fulfilledHandler); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|