Skip to content

Commit

Permalink
Added a configurable timeout value for the StatsD Client
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmoussa committed Feb 24, 2015
1 parent 23425db commit 4d8f1e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Client
*/
protected $namespace = '';

/**
* Timeout for creating the socket connection
* @var null|float
*/
protected $timeout;


/**
* Singleton Reference
Expand All @@ -77,6 +83,10 @@ public static function instance($name = 'default')
public function __construct($instance_id = null)
{
$this->instance_id = $instance_id ?: uniqid();

if (empty($this->timeout)) {
$this->timeout = ini_get('default_socket_timeout');
}
}


Expand Down Expand Up @@ -111,6 +121,9 @@ public function configure(array $options = array())
if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];
}
if (isset($options['timeout'])) {
$this->timeout = $options['timeout'];
}
return $this;
}

Expand Down Expand Up @@ -266,7 +279,7 @@ public function set($metric, $value)
protected function send(array $data)
{

$socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr);
$socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout);
if (! $socket) {
throw new ConnectionException($this, '(' . $errno . ') ' . $errstr);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ public function testInvalidHost()
$this->client->increment('test');
}

public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided()
{
$this->client->configure(array(
'host' => 'localhost',
'timeout' => 123
));

$this->assertAttributeSame(123, 'timeout', $this->client);
}

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

0 comments on commit 4d8f1e3

Please sign in to comment.