1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Channel\Record; |
4
|
|
|
|
5
|
|
|
use Dazzle\Util\Support\TimeSupport; |
6
|
|
|
use Dazzle\Throwable\Exception\Runtime\TimeoutException; |
7
|
|
|
use Dazzle\Throwable\ThrowableProxy; |
8
|
|
|
use Error; |
9
|
|
|
use Exception; |
10
|
|
|
|
11
|
|
|
trait RequestRecordStorage |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RequestRecord[] |
15
|
|
|
*/ |
16
|
|
|
protected $reqs = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create RequestRecord. |
20
|
|
|
* |
21
|
|
|
* @param string $pid |
22
|
|
|
* @param callable|null $success |
23
|
|
|
* @param callable|null $failure |
24
|
|
|
* @param callable|null $cancel |
25
|
|
|
* @param float $timeout |
26
|
|
|
* @return RequestRecord |
27
|
|
|
*/ |
28
|
2 |
|
protected function createRequest($pid, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
29
|
|
|
{ |
30
|
2 |
|
if ($timeout > 0.0) |
31
|
|
|
{ |
32
|
1 |
|
$timeout = $timeout * 1000 + TimeSupport::now(); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
return new RequestRecord($pid, $success, $failure, $cancel, $timeout); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check if RequestRecord with given protocol ID exists. |
40
|
|
|
* |
41
|
|
|
* @param string $pid |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
3 |
|
protected function existsRequest($pid) |
45
|
|
|
{ |
46
|
3 |
|
return isset($this->reqs[$pid]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add new RequestRecord to storage. |
51
|
|
|
* |
52
|
|
|
* @param string $pid |
53
|
|
|
* @param RequestRecord $request |
54
|
|
|
*/ |
55
|
5 |
|
protected function addRequest($pid, RequestRecord $request) |
56
|
|
|
{ |
57
|
5 |
|
$this->reqs[$pid] = $request; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return RequestRecord if it exists or null if it does not exist. |
62
|
|
|
* |
63
|
|
|
* @param string $pid |
64
|
|
|
* @return RequestRecord|null |
65
|
|
|
*/ |
66
|
1 |
|
protected function getRequest($pid) |
67
|
|
|
{ |
68
|
1 |
|
return $this->reqs[$pid]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Resolve RequestRecord if it exists that has protocol ID equal to $pid. |
73
|
|
|
* |
74
|
|
|
* @param string $pid |
75
|
|
|
* @param string $message |
76
|
|
|
*/ |
77
|
1 |
|
protected function resolveRequest($pid, $message) |
78
|
|
|
{ |
79
|
1 |
|
$callback = $this->reqs[$pid]->onSuccess(); |
80
|
1 |
|
unset($this->reqs[$pid]); |
81
|
1 |
|
$callback($message); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Reject RequestRecord if it exists that has protocol ID equal to $pid. |
86
|
|
|
* |
87
|
|
|
* @param string $pid |
88
|
|
|
* @param Error|Exception|ThrowableProxy $ex |
89
|
|
|
*/ |
90
|
1 |
|
protected function rejectRequest($pid, $ex) |
91
|
|
|
{ |
92
|
1 |
|
$callback = $this->reqs[$pid]->onFailure(); |
93
|
1 |
|
unset($this->reqs[$pid]); |
94
|
1 |
|
$callback($ex); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Cancel RequestRecord if it exists that has protocol ID equal to $pid. |
99
|
|
|
* |
100
|
|
|
* @param string $pid |
101
|
|
|
* @param Error|Exception|ThrowableProxy $ex |
102
|
|
|
*/ |
103
|
1 |
|
protected function cancelRequest($pid, $ex) |
104
|
|
|
{ |
105
|
1 |
|
$callback = $this->reqs[$pid]->onCancel(); |
106
|
1 |
|
unset($this->reqs[$pid]); |
107
|
1 |
|
$callback($ex); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Cancel overdue Requests. |
112
|
|
|
*/ |
113
|
|
|
protected function expireRequests() |
114
|
|
|
{ |
115
|
|
|
$now = TimeSupport::now(); |
116
|
|
|
$expiredReqs = []; |
117
|
|
|
|
118
|
|
|
foreach ($this->reqs as $pid=>$request) |
119
|
|
|
{ |
120
|
|
|
if ($now >= $request->getTimeout()) |
121
|
|
|
{ |
122
|
|
|
$expiredReqs[] = $request; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($expiredReqs as $request) |
127
|
|
|
{ |
128
|
|
|
unset($this->reqs[$request->getPid()]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
foreach ($expiredReqs as $request) |
132
|
|
|
{ |
133
|
|
|
$request->cancel(new ThrowableProxy(new TimeoutException("RequestRecord has expired."))); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|