Skip to content

Commit ee55378

Browse files
committed
Merge pull request #16 from michaelmoussa/disable-exceptions
Allow optional disabling of connection exceptions
2 parents dd55c72 + 22a2605 commit ee55378

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ $statsd2 = StatsD\Client::instance('server2')->configure(array(...));
6464
The StatsD client wait for `ini_get('default_socket_timeout')` seconds when opening the socket by default. To reduce
6565
this timeout, add `'timeout' => <float>` to your config.
6666

67+
The StatsD client will throw a `ConnectionException` if it is unable to send data to the StatsD server. You may choose
68+
to disable these exceptions and log a PHP warning instead if you wish. To do so, include the following in your config:
69+
70+
```
71+
'throwConnectionExceptions' => false
72+
```
73+
74+
If omitted, this option defaults to `true`.
75+
6776
### Counters
6877

6978
```php

src/Client.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class Client
6060
*/
6161
protected $timeout;
6262

63+
/**
64+
* Whether or not an exception should be thrown on failed connections
65+
* @var bool
66+
*/
67+
protected $throwConnectionExceptions = true;
6368

6469
/**
6570
* Singleton Reference
@@ -118,12 +123,19 @@ public function configure(array $options = array())
118123
}
119124
$this->port = $port;
120125
}
126+
121127
if (isset($options['namespace'])) {
122128
$this->namespace = $options['namespace'];
123129
}
130+
124131
if (isset($options['timeout'])) {
125132
$this->timeout = $options['timeout'];
126133
}
134+
135+
if (isset($options['throwConnectionExceptions'])) {
136+
$this->throwConnectionExceptions = $options['throwConnectionExceptions'];
137+
}
138+
127139
return $this;
128140
}
129141

@@ -281,7 +293,15 @@ protected function send(array $data)
281293

282294
$socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout);
283295
if (! $socket) {
284-
throw new ConnectionException($this, '(' . $errno . ') ' . $errstr);
296+
if ($this->throwConnectionExceptions) {
297+
throw new ConnectionException($this, '(' . $errno . ') ' . $errstr);
298+
} else {
299+
trigger_error(
300+
sprintf('StatsD server connection failed (udp://%s:%d)', $this->host, $this->port),
301+
E_USER_WARNING
302+
);
303+
return;
304+
}
285305
}
286306
$this->messages = array();
287307
$prefix = $this->namespace ? $this->namespace . '.' : '';

tests/ConnectionTest.php

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

55
class ConnectionTest extends TestCase
66
{
7-
87
/**
98
* Non-integer ports are not acceptable
109
* @expectedException League\StatsD\Exception\ConnectionException
@@ -23,10 +22,39 @@ public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided()
2322
'host' => 'localhost',
2423
'timeout' => 123
2524
));
26-
2725
$this->assertAttributeSame(123, 'timeout', $this->client);
2826
}
2927

28+
public function testCanBeConfiguredNotToThrowConnectionExceptions()
29+
{
30+
$this->client->configure(array(
31+
'host' => 'hostdoesnotexiststalleverlol.stupidtld',
32+
'throwConnectionExceptions' => false
33+
));
34+
$handlerInvoked = false;
35+
36+
$testCase = $this;
37+
38+
set_error_handler(
39+
function ($errno, $errstr, $errfile, $errline, $errcontext) use ($testCase, &$handlerInvoked) {
40+
$handlerInvoked = true;
41+
42+
$testCase->assertSame(E_USER_WARNING, $errno);
43+
$testCase->assertSame(
44+
'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)',
45+
$errstr
46+
);
47+
$testCase->assertSame(realpath(__DIR__ . '/../src/Client.php'), $errfile);
48+
},
49+
E_USER_WARNING
50+
);
51+
52+
$this->client->increment('test');
53+
restore_error_handler();
54+
55+
$this->assertTrue($handlerInvoked);
56+
}
57+
3058
public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout()
3159
{
3260
$this->assertAttributeSame(ini_get('default_socket_timeout'), 'timeout', $this->client);

0 commit comments

Comments
 (0)