Skip to content

Commit

Permalink
Merge pull request #16 from michaelmoussa/disable-exceptions
Browse files Browse the repository at this point in the history
Allow optional disabling of connection exceptions
  • Loading branch information
marcqualie committed Jun 11, 2015
2 parents dd55c72 + 22a2605 commit ee55378
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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' => <float>` 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
Expand Down
22 changes: 21 additions & 1 deletion src/Client.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 . '.' : '';
Expand Down
32 changes: 30 additions & 2 deletions tests/ConnectionTest.php
Expand Up @@ -4,7 +4,6 @@

class ConnectionTest extends TestCase
{

/**
* Non-integer ports are not acceptable
* @expectedException League\StatsD\Exception\ConnectionException
Expand All @@ -23,10 +22,39 @@ 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;

$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();

$this->assertTrue($handlerInvoked);
}

public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout()
{
$this->assertAttributeSame(ini_get('default_socket_timeout'), 'timeout', $this->client);
Expand Down

0 comments on commit ee55378

Please sign in to comment.