forked from clue/reactphp-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyDatabase.php
201 lines (175 loc) · 5.66 KB
/
LazyDatabase.php
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
<?php
namespace Clue\React\SQLite\Io;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
/**
* @internal
*/
class LazyDatabase extends EventEmitter implements DatabaseInterface
{
private $filename;
private $flags;
/** @var Factory */
private $factory;
private $loop;
private $closed = false;
/**@var ?DatabaseInterface */
private $disconnecting;
/** @var ?\React\Promise\PromiseInterface */
private $promise;
private $idlePeriod = 60.0;
/** @var ?\React\EventLoop\TimerInterface */
private $idleTimer;
private $pending = 0;
public function __construct($target, $flags, array $options, Factory $factory, LoopInterface $loop)
{
$this->filename = $target;
$this->flags = $flags;
$this->factory = $factory;
$this->loop = $loop;
if (isset($options['idle'])) {
$this->idlePeriod = (float)$options['idle'];
}
}
/**
* @return \React\Promise\PromiseInterface
*/
private function db()
{
if ($this->promise !== null) {
return $this->promise;
}
if ($this->closed) {
return \React\Promise\reject(new \RuntimeException('Connection closed'));
}
// force-close connection if still waiting for previous disconnection
if ($this->disconnecting !== null) {
$this->disconnecting->close();
$this->disconnecting = null;
}
$this->promise = $promise = $this->factory->open($this->filename, $this->flags);
$promise->then(function (DatabaseInterface $db) {
// connection completed => remember only until closed
$db->on('close', function () {
$this->promise = null;
if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
});
}, function () {
// connection failed => discard connection attempt
$this->promise = null;
});
return $promise;
}
public function exec($sql)
{
return $this->db()->then(function (DatabaseInterface $db) use ($sql) {
$this->awake();
return $db->exec($sql)->then(
function ($result) {
$this->idle();
return $result;
},
function ($error) {
$this->idle();
throw $error;
}
);
});
}
public function query($sql, array $params = [])
{
return $this->db()->then(function (DatabaseInterface $db) use ($sql, $params) {
$this->awake();
return $db->query($sql, $params)->then(
function ($result) {
$this->idle();
return $result;
},
function ($error) {
$this->idle();
throw $error;
}
);
});
}
public function quit()
{
if ($this->promise === null && !$this->closed) {
$this->close();
return \React\Promise\resolve(null);
}
return $this->db()->then(function (DatabaseInterface $db) {
$db->on('close', function () {
$this->close();
});
return $db->quit();
});
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
// force-close connection if still waiting for previous disconnection
if ($this->disconnecting !== null) {
$this->disconnecting->close();
$this->disconnecting = null;
}
// either close active connection or cancel pending connection attempt
if ($this->promise !== null) {
$this->promise->then(function (DatabaseInterface $db) {
$db->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
$this->promise->cancel();
$this->promise = null;
}
}
if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
$this->emit('close');
$this->removeAllListeners();
}
private function awake()
{
++$this->pending;
if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
}
private function idle()
{
--$this->pending;
if ($this->pending < 1 && $this->idlePeriod >= 0) {
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () {
$this->promise->then(function (DatabaseInterface $db) {
$this->disconnecting = $db;
$db->quit()->then(
function () {
// successfully disconnected => remove reference
$this->disconnecting = null;
},
function () use ($db) {
// soft-close failed => force-close connection
$db->close();
$this->disconnecting = null;
}
);
});
$this->promise = null;
$this->idleTimer = null;
});
}
}
}