Skip to content

Commit 1604389

Browse files
author
Andrew Carter
committed
Removed DaemonOptionsInterface and changed DaemonOptions object to make adding new features in later versions easier.
1 parent 79cc26a commit 1604389

File tree

12 files changed

+125
-154
lines changed

12 files changed

+125
-154
lines changed

src/Command/DaemonRunCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(KernelInterface $kernel, DriverContainerInterface $d
5555
}
5656

5757
/**
58-
* Retrieves the daemon configuration from the Symfony command input and
58+
* Creates a daemon configuration object from the Symfony command input and
5959
* output objects.
6060
*
6161
* @param InputInterface $input The Symfony command input
@@ -71,7 +71,12 @@ private function getDaemonOptions(InputInterface $input, OutputInterface $output
7171
$memoryLimit = $input->getOption('memory-limit') ?: DaemonOptions::NO_LIMIT;
7272
$timeLimit = $input->getOption('time-limit') ?: DaemonOptions::NO_LIMIT;
7373

74-
return new DaemonOptions($logger, $requestLimit, $memoryLimit, $timeLimit);
74+
return new DaemonOptions([
75+
DaemonOptions::LOGGER => $logger,
76+
DaemonOptions::REQUEST_LIMIT => $requestLimit,
77+
DaemonOptions::MEMORY_LIMIT => $memoryLimit,
78+
DaemonOptions::TIME_LIMIT => $timeLimit,
79+
]);
7580
}
7681

7782
/**

src/DaemonFactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface DaemonFactoryInterface
1717
*
1818
* @return DaemonInterface The FastCGI daemon
1919
*/
20-
public function createDaemon(KernelInterface $kernel, DaemonOptionsInterface $options);
20+
public function createDaemon(KernelInterface $kernel, DaemonOptions $options);
2121

2222
/**
2323
* Create a FastCGI daemon listening on a given address. The default host is
@@ -30,5 +30,5 @@ public function createDaemon(KernelInterface $kernel, DaemonOptionsInterface $op
3030
*
3131
* @return DaemonInterface The FastCGI daemon
3232
*/
33-
public function createTcpDaemon(KernelInterface $kernel, DaemonOptionsInterface $options, $host, $port);
33+
public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, $host, $port);
3434
}

src/DaemonOptions.php

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,77 @@
33
namespace PHPFastCGI\FastCGIDaemon;
44

55
use Psr\Log\LoggerInterface;
6+
use Psr\Log\NullLogger;
67

78
/**
8-
* The default implementation of the DaemonOptionsInterface.
9+
* The default configuration object.
910
*/
10-
class DaemonOptions implements DaemonOptionsInterface
11+
class DaemonOptions
1112
{
12-
/**
13-
* @var LoggerInterface
14-
*/
15-
private $logger;
13+
const NO_LIMIT = 0;
1614

17-
/**
18-
* @var int
19-
*/
20-
private $requestLimit;
15+
// Possible daemon options
16+
const LOGGER = 'logger';
17+
const REQUEST_LIMIT = 'request-limit';
18+
const MEMORY_LIMIT = 'memory-limit';
19+
const TIME_LIMIT = 'time-limit';
2120

2221
/**
23-
* @var int
22+
* @var array
2423
*/
25-
private $memoryLimit;
26-
27-
/**
28-
* @var int
29-
*/
30-
private $timeLimit;
24+
private $options;
3125

3226
/**
3327
* Constructor.
3428
*
35-
* For the $requestLimit parameter, DaemonOptionsInterface::NO_LIMIT can be
36-
* used to specify that no number of requests should cause the daemon to
29+
* The value of the LOGGER option must implement the PSR-3 LoggerInterface.
30+
*
31+
* For the REQUEST_LIMIT, MEMORY_LIMIT and TIME_LIMIT options, NO_LIMIT can
32+
* be used to specify that these metrics should not cause the daemon to
3733
* shutdown.
3834
*
39-
* For the $memoryLimit parameter, DaemonOptionsInterface::NO_LIMIT can be
40-
* used to specify that no detected memory usage should cause the daemon
41-
* to shutdown.
42-
*
43-
* @param LoggerInterface $logger A logger to use
44-
* @param int $requestLimit Number of requests to handle before shutting down
45-
* @param int $memoryLimit Upper bound on amount of memory in bytes allocated to script before shutting down
46-
* @param int $timeLimit Upper bound on the number of seconds to run the daemon for before shutting down
35+
* @param array $options The options to configure the daemon with
36+
*
37+
* @throws \InvalidArgumentException On unrecognised option
4738
*/
48-
public function __construct(LoggerInterface $logger, $requestLimit, $memoryLimit, $timeLimit)
39+
public function __construct(array $options = [])
4940
{
50-
$this->logger = $logger;
51-
$this->requestLimit = $requestLimit;
52-
$this->memoryLimit = $memoryLimit;
53-
$this->timeLimit = $timeLimit;
54-
}
41+
// Set defaults
42+
$this->options = [
43+
self::LOGGER => new NullLogger(),
44+
self::REQUEST_LIMIT => self::NO_LIMIT,
45+
self::MEMORY_LIMIT => self::NO_LIMIT,
46+
self::TIME_LIMIT => self::NO_LIMIT,
47+
];
5548

56-
/**
57-
* {@inheritdoc}
58-
*/
59-
public function getLogger()
60-
{
61-
return $this->logger;
62-
}
49+
foreach ($options as $option => $value) {
50+
if (!isset($this->options[$option])) {
51+
throw new \InvalidArgumentException('Unknown option: '.$option);
52+
}
6353

64-
/**
65-
* {@inheritdoc}
66-
*/
67-
public function getRequestLimit()
68-
{
69-
return $this->requestLimit;
70-
}
54+
$this->options[$option] = $value;
55+
}
7156

72-
/**
73-
* {@inheritdoc}
74-
*/
75-
public function getMemoryLimit()
76-
{
77-
return $this->memoryLimit;
57+
if (!$this->options[self::LOGGER] instanceof LoggerInterface) {
58+
throw new \InvalidArgumentException('Logger must implement LoggerInterface');
59+
}
7860
}
7961

8062
/**
81-
* {@inheritdoc}
63+
* Retrieve the value of one of the daemon options.
64+
*
65+
* @param string $option The option to return
66+
*
67+
* @return mixed The value of the option requested
68+
*
69+
* @throws \InvalidArgumentException On unrecognised option
8270
*/
83-
public function getTimeLimit()
71+
public function getOption($option)
8472
{
85-
return $this->timeLimit;
73+
if (!isset($this->options[$option])) {
74+
throw new \InvalidArgumentException('Unknown option: '.$option);
75+
}
76+
77+
return $this->options[$option];
8678
}
8779
}

src/DaemonOptionsInterface.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/DaemonTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ trait DaemonTrait
2828
* Loads to configuration from the daemon options and installs signal
2929
* handlers.
3030
*
31-
* @param DaemonOptionsInterface $daemonOptions
31+
* @param DaemonOptions $daemonOptions
3232
*/
33-
private function setupDaemon(DaemonOptionsInterface $daemonOptions)
33+
private function setupDaemon(DaemonOptions $daemonOptions)
3434
{
3535
$this->requestCount = 0;
36-
$this->requestLimit = $daemonOptions->getRequestLimit();
37-
$this->memoryLimit = $daemonOptions->getMemoryLimit();
36+
$this->requestLimit = $daemonOptions->getOption(DaemonOptions::REQUEST_LIMIT);
37+
$this->memoryLimit = $daemonOptions->getOption(DaemonOptions::MEMORY_LIMIT);
3838

39-
$timeLimit = $daemonOptions->getTimeLimit();
39+
$timeLimit = $daemonOptions->getOption(DaemonOptions::TIME_LIMIT);
4040

41-
if (DaemonOptionsInterface::NO_LIMIT !== $timeLimit) {
41+
if (DaemonOptions::NO_LIMIT !== $timeLimit) {
4242
pcntl_alarm($timeLimit);
4343
}
4444

@@ -82,13 +82,13 @@ private function installSignalHandlers()
8282
*/
8383
private function checkDaemonLimits()
8484
{
85-
if (DaemonOptionsInterface::NO_LIMIT !== $this->requestLimit) {
85+
if (DaemonOptions::NO_LIMIT !== $this->requestLimit) {
8686
if ($this->requestLimit <= $this->requestCount) {
8787
throw new RequestLimitException('Daemon request limit reached ('.$this->requestCount.' of '.$this->requestLimit.')');
8888
}
8989
}
9090

91-
if (DaemonOptionsInterface::NO_LIMIT !== $this->memoryLimit) {
91+
if (DaemonOptions::NO_LIMIT !== $this->memoryLimit) {
9292
$memoryUsage = memory_get_usage(true);
9393

9494
if ($this->memoryLimit <= $memoryUsage) {

src/Driver/Userland/UserlandDaemon.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PHPFastCGI\FastCGIDaemon\Driver\Userland;
44

55
use PHPFastCGI\FastCGIDaemon\DaemonInterface;
6-
use PHPFastCGI\FastCGIDaemon\DaemonOptionsInterface;
6+
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
77
use PHPFastCGI\FastCGIDaemon\DaemonTrait;
88
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionPoolInterface;
99
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface;
@@ -24,7 +24,7 @@ class UserlandDaemon implements DaemonInterface
2424
private $kernel;
2525

2626
/**
27-
* @var DaemonOptionsInterface
27+
* @var DaemonOptions
2828
*/
2929
private $daemonOptions;
3030

@@ -47,11 +47,11 @@ class UserlandDaemon implements DaemonInterface
4747
* Constructor.
4848
*
4949
* @param KernelInterface $kernel The kernel for the daemon to use
50-
* @param DaemonOptionsInterface $daemonOptions The daemon configuration
50+
* @param DaemonOptions $daemonOptions The daemon configuration
5151
* @param ConnectionPoolInterface $connectionPool The connection pool to accept connections from
5252
* @param ConnectionHandlerFactoryInterface $connectionHandlerFactory A factory class for producing connection handlers
5353
*/
54-
public function __construct(KernelInterface $kernel, DaemonOptionsInterface $daemonOptions, ConnectionPoolInterface $connectionPool, ConnectionHandlerFactoryInterface $connectionHandlerFactory)
54+
public function __construct(KernelInterface $kernel, DaemonOptions $daemonOptions, ConnectionPoolInterface $connectionPool, ConnectionHandlerFactoryInterface $connectionHandlerFactory)
5555
{
5656
$this->kernel = $kernel;
5757
$this->daemonOptions = $daemonOptions;
@@ -75,18 +75,23 @@ public function run()
7575
$this->checkDaemonLimits();
7676
}
7777
} catch (ShutdownException $exception) {
78-
$this->daemonOptions->getLogger()->notice($exception->getMessage());
78+
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->notice($exception->getMessage());
7979

8080
$this->shutdown();
8181
} catch (\Exception $exception) {
82-
$this->daemonOptions->getLogger()->emergency($exception->getMessage());
82+
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->emergency($exception->getMessage());
8383

8484
$this->connectionPool->close();
8585

8686
throw $exception;
8787
}
8888
}
8989

90+
/**
91+
* Wait for connections in the pool to become readable. Create connection
92+
* handlers for new connections and trigger the ready method when there is
93+
* data for the handlers to receive. Clean up closed connections.
94+
*/
9095
private function processConnectionPool()
9196
{
9297
$readableConnections = $this->connectionPool->getReadableConnections(5);
@@ -105,6 +110,9 @@ private function processConnectionPool()
105110
}
106111
}
107112

113+
/**
114+
* Gracefully shutdown the daemon.
115+
*/
108116
private function shutdown()
109117
{
110118
$this->connectionPool->shutdown();

src/Driver/Userland/UserlandDaemonFactory.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPFastCGI\FastCGIDaemon\DaemonInterface;
66
use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface;
7-
use PHPFastCGI\FastCGIDaemon\DaemonOptionsInterface;
7+
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
88
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\StreamSocketConnectionPool;
99
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactory;
1010
use PHPFastCGI\FastCGIDaemon\KernelInterface;
@@ -18,14 +18,14 @@ class UserlandDaemonFactory implements DaemonFactoryInterface
1818
* Create a FastCGI daemon listening on FCGI_LISTENSOCK_FILENO using the
1919
* userland FastCGI implementation.
2020
*
21-
* @param KernelInterface $kernel The kernel to use for the daemon
22-
* @param DaemonOptionsInterface $options The daemon configuration
21+
* @param KernelInterface $kernel The kernel to use for the daemon
22+
* @param DaemonOptions $options The daemon configuration
2323
*
2424
* @return UserlandDaemon
2525
*
2626
* @codeCoverageIgnore The FastCGI daemon
2727
*/
28-
public function createDaemon(KernelInterface $kernel, DaemonOptionsInterface $options)
28+
public function createDaemon(KernelInterface $kernel, DaemonOptions $options)
2929
{
3030
$socket = fopen('php://fd/'.DaemonInterface::FCGI_LISTENSOCK_FILENO, 'r');
3131

@@ -41,16 +41,16 @@ public function createDaemon(KernelInterface $kernel, DaemonOptionsInterface $op
4141
* using the userland FastCGI implementation. The default host is
4242
* 'localhost'.
4343
*
44-
* @param KernelInterface $kernel The kernel to use for the daemon
45-
* @param DaemonOptionsInterface $options The daemon configuration
46-
* @param string $host The host to bind to
47-
* @param int $port The port to bind to
44+
* @param KernelInterface $kernel The kernel to use for the daemon
45+
* @param DaemonOptions $options The daemon configuration
46+
* @param string $host The host to bind to
47+
* @param int $port The port to bind to
4848
*
4949
* @return UserlandDaemon The FastCGI daemon
5050
*
5151
* @codeCoverageIgnore
5252
*/
53-
public function createTcpDaemon(KernelInterface $kernel, DaemonOptionsInterface $options, $host, $port)
53+
public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, $host, $port)
5454
{
5555
$address = 'tcp://'.$host.':'.$port;
5656
$socket = stream_socket_server($address);
@@ -66,13 +66,13 @@ public function createTcpDaemon(KernelInterface $kernel, DaemonOptionsInterface
6666
* Create a FastCGI daemon from a stream socket which is configured for
6767
* accepting connections using the userland FastCGI implementation.
6868
*
69-
* @param KernelInterface $kernel The kernel to use for the daemon
70-
* @param DaemonOptionsInterface $options The daemon configuration
71-
* @param resource $socket The socket to accept connections from
69+
* @param KernelInterface $kernel The kernel to use for the daemon
70+
* @param DaemonOptions $options The daemon configuration
71+
* @param resource $socket The socket to accept connections from
7272
*
7373
* @return UserlandDaemon The FastCGI daemon
7474
*/
75-
public function createDaemonFromStreamSocket(KernelInterface $kernel, DaemonOptionsInterface $options, $socket)
75+
public function createDaemonFromStreamSocket(KernelInterface $kernel, DaemonOptions $options, $socket)
7676
{
7777
$connectionPool = new StreamSocketConnectionPool($socket);
7878
$connectionHandlerFactory = new ConnectionHandlerFactory();

0 commit comments

Comments
 (0)