-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hellofresh/feature/cache-socket-connection
Added opened socket reuse for statsd
- Loading branch information
Showing
2 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace HelloFresh\Stats\StatsD; | ||
|
||
use League\StatsD\Client; | ||
use League\StatsD\Exception\ConnectionException; | ||
|
||
class CachingClient extends Client | ||
{ | ||
/** @var resource */ | ||
protected $socket; | ||
|
||
/** | ||
* @throws ConnectionException | ||
* @return resource | ||
*/ | ||
protected function getSocket() | ||
{ | ||
if (!$this->socket) { | ||
$this->socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout); | ||
if (!$this->socket) { | ||
throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); | ||
} | ||
} | ||
|
||
return $this->socket; | ||
} | ||
|
||
/** | ||
* Send Data to StatsD Server | ||
* @param array $data A list of messages to send to the server | ||
* @throws ConnectionException If there is a connection problem with the host | ||
* @return $this | ||
*/ | ||
protected function send(array $data) | ||
{ | ||
try { | ||
$socket = $this->getSocket(); | ||
$messages = []; | ||
$prefix = $this->namespace ? $this->namespace . '.' : ''; | ||
foreach ($data as $key => $value) { | ||
$messages[] = $prefix . $key . ':' . $value; | ||
} | ||
$this->message = implode("\n", $messages); | ||
@fwrite($socket, $this->message); | ||
fflush($socket); | ||
} catch (ConnectionException $e) { | ||
if ($this->throwConnectionExceptions) { | ||
throw $e; | ||
} else { | ||
trigger_error( | ||
sprintf('StatsD server connection failed (udp://%s:%d)', $this->host, $this->port), | ||
E_USER_WARNING | ||
); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->socket) { | ||
fclose($this->socket); | ||
} | ||
} | ||
} |