-
Notifications
You must be signed in to change notification settings - Fork 16
/
UserlandDaemon.php
135 lines (111 loc) · 4.3 KB
/
UserlandDaemon.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
<?php
namespace PHPFastCGI\FastCGIDaemon\Driver\Userland;
use PHPFastCGI\FastCGIDaemon\DaemonInterface;
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
use PHPFastCGI\FastCGIDaemon\DaemonTrait;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionPoolInterface;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException;
use PHPFastCGI\FastCGIDaemon\Exception\ShutdownException;
use PHPFastCGI\FastCGIDaemon\KernelInterface;
/**
* The standard implementation of the DaemonInterface is constructed from a
* connection pool and a factory class to generate connection handlers.
*/
class UserlandDaemon implements DaemonInterface
{
use DaemonTrait;
/**
* @var KernelInterface
*/
private $kernel;
/**
* @var DaemonOptions
*/
private $daemonOptions;
/**
* @var ConnectionPoolInterface
*/
private $connectionPool;
/**
* @var ConnectionHandlerFactoryInterface
*/
private $connectionHandlerFactory;
/**
* @var ConnectionHandler[]
*/
private $connectionHandlers;
/**
* Constructor.
*
* @param KernelInterface $kernel The kernel for the daemon to use
* @param DaemonOptions $daemonOptions The daemon configuration
* @param ConnectionPoolInterface $connectionPool The connection pool to accept connections from
* @param ConnectionHandlerFactoryInterface $connectionHandlerFactory A factory class for producing connection handlers
*/
public function __construct(KernelInterface $kernel, DaemonOptions $daemonOptions, ConnectionPoolInterface $connectionPool, ConnectionHandlerFactoryInterface $connectionHandlerFactory)
{
$this->kernel = $kernel;
$this->daemonOptions = $daemonOptions;
$this->connectionPool = $connectionPool;
$this->connectionHandlerFactory = $connectionHandlerFactory;
$this->connectionHandlers = [];
}
/**
* {@inheritdoc}
*/
public function run()
{
$this->setupDaemon($this->daemonOptions);
try {
while (1) {
$this->processConnectionPool();
$this->checkDaemonLimits();
}
} catch (ShutdownException $exception) {
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->notice($exception->getMessage());
$this->shutdown();
} catch (\Exception $exception) {
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->emergency($exception->getMessage());
$this->connectionPool->close();
throw $exception;
}
}
/**
* Wait for connections in the pool to become readable. Create connection
* handlers for new connections and trigger the ready method when there is
* data for the handlers to receive. Clean up closed connections.
*/
private function processConnectionPool()
{
$readableConnections = $this->connectionPool->getReadableConnections(5);
foreach ($readableConnections as $id => $connection) {
if (!isset($this->connectionHandlers[$id])) {
$this->connectionHandlers[$id] = $this->connectionHandlerFactory->createConnectionHandler($this->kernel, $connection);
}
try {
$dispatchedRequests = $this->connectionHandlers[$id]->ready();
$this->incrementRequestCount($dispatchedRequests);
} catch (UserlandDaemonException $exception) {
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->error($exception->getMessage());
}
if ($this->connectionHandlers[$id]->isClosed()) {
unset($this->connectionHandlers[$id]);
}
}
}
/**
* Gracefully shutdown the daemon.
*/
private function shutdown()
{
$this->connectionPool->shutdown();
foreach ($this->connectionHandlers as $connectionHandler) {
$connectionHandler->shutdown();
}
while ($this->connectionPool->count() > 0) {
$this->processConnectionPool();
}
$this->connectionPool->close();
}
}