From dcb6e42f43638861a1974623f3931c8b74bda9cf Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Thu, 19 Feb 2015 13:04:56 -0500 Subject: [PATCH 1/3] Added configuration option to disable throwing of connection exceptions --- src/Client.php | 22 +++++++++++++++++++++- tests/ConnectionTest.php | 27 +++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1c751c7..1263af1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -60,6 +60,11 @@ class Client */ protected $timeout; + /** + * Whether or not an exception should be thrown on failed connections + * @var bool + */ + protected $throwConnectionExceptions = true; /** * Singleton Reference @@ -118,12 +123,19 @@ public function configure(array $options = array()) } $this->port = $port; } + if (isset($options['namespace'])) { $this->namespace = $options['namespace']; } + if (isset($options['timeout'])) { $this->timeout = $options['timeout']; } + + if (isset($options['throwConnectionExceptions'])) { + $this->throwConnectionExceptions = $options['throwConnectionExceptions']; + } + return $this; } @@ -281,7 +293,15 @@ protected function send(array $data) $socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout); if (! $socket) { - throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); + if ($this->throwConnectionExceptions) { + throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); + } else { + trigger_error( + sprintf('StatsD server connection failed (udp://%s:%d)', $this->host, $this->port), + E_USER_WARNING + ); + return; + } } $this->messages = array(); $prefix = $this->namespace ? $this->namespace . '.' : ''; diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index adf9c74..8388247 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -4,7 +4,6 @@ class ConnectionTest extends TestCase { - /** * Non-integer ports are not acceptable * @expectedException League\StatsD\Exception\ConnectionException @@ -23,10 +22,34 @@ public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided() 'host' => 'localhost', 'timeout' => 123 )); - $this->assertAttributeSame(123, 'timeout', $this->client); } + public function testCanBeConfiguredNotToThrowConnectionExceptions() + { + $this->client->configure(array( + 'host' => 'hostdoesnotexiststalleverlol.stupidtld', + 'throwConnectionExceptions' => false + )); + $handlerInvoked = false; + + set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$handlerInvoked) { + $handlerInvoked = true; + + $this->assertSame(E_USER_WARNING, $errno); + $this->assertSame( + 'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)', + $errstr + ); + $this->assertSame(realpath(__DIR__ . '/../src/Client.php'), $errfile); + }, E_USER_WARNING); + + $this->client->increment('test'); + restore_error_handler(); + + $this->assertTrue($handlerInvoked); + } + public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout() { $this->assertAttributeSame(ini_get('default_socket_timeout'), 'timeout', $this->client); From 776c40e9613e122dba74b2e4eb23dd7dc2adf676 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Thu, 19 Feb 2015 13:08:48 -0500 Subject: [PATCH 2/3] Updated README with the throwConnectionExceptions option --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index c22b7f8..fcd1ccf 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,15 @@ $statsd2 = StatsD\Client::instance('server2')->configure(array(...)); The StatsD client wait for `ini_get('default_socket_timeout')` seconds when opening the socket by default. To reduce this timeout, add `'timeout' => ` to your config. +The StatsD client will throw a `ConnectionException` if it is unable to send data to the StatsD server. You may choose +to disable these exceptions and log a PHP warning instead if you wish. To do so, include the following in your config: + +``` + 'throwConnectionExceptions' => false +``` + +If omitted, this option defaults to `true`. + ### Counters ```php From 22a2605b2333453021ff49a1ed94b16432a342da Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Fri, 20 Feb 2015 10:23:54 -0500 Subject: [PATCH 3/3] PHP 5.3 support ... eww --- tests/ConnectionTest.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 8388247..a8d087d 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -33,16 +33,21 @@ public function testCanBeConfiguredNotToThrowConnectionExceptions() )); $handlerInvoked = false; - set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$handlerInvoked) { - $handlerInvoked = true; - - $this->assertSame(E_USER_WARNING, $errno); - $this->assertSame( - 'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)', - $errstr - ); - $this->assertSame(realpath(__DIR__ . '/../src/Client.php'), $errfile); - }, E_USER_WARNING); + $testCase = $this; + + set_error_handler( + function ($errno, $errstr, $errfile, $errline, $errcontext) use ($testCase, &$handlerInvoked) { + $handlerInvoked = true; + + $testCase->assertSame(E_USER_WARNING, $errno); + $testCase->assertSame( + 'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)', + $errstr + ); + $testCase->assertSame(realpath(__DIR__ . '/../src/Client.php'), $errfile); + }, + E_USER_WARNING + ); $this->client->increment('test'); restore_error_handler();