Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout and error handling #73

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/Beberlei/Metrics/Collector/Graphite.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ class Graphite implements Collector
/** @var int */
private $port;

/** @var int fsocket connection timeout, in seconds. */
private $timeout;

/** @var array */
private $data = array();

/**
* @param string $host
* @param int $port
* @param int $port
* @param string $protocol
*/
public function __construct($host = 'localhost', $port = 2003, $protocol = 'tcp')
public function __construct($host = 'localhost', $port = 2003, $timeout = null, $protocol = 'tcp')
{
$this->host = $host;
$this->port = $port;
$this->protocol = $protocol;
$this->timeout = $timeout;
}

/**
Expand Down Expand Up @@ -85,21 +89,18 @@ public function flush()
return;
}

try {
$fp = fsockopen($this->protocol.'://'.$this->host, $this->port);

if (!$fp) {
return;
}
$fp = @fsockopen($this->protocol . '://' . $this->host, $this->port, $errno, $errmsg, $this->timeout);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using @ is a bad idea. Could you use a custom error handler?
see this example: https://github.com/redirectionio/proxy-sdk-php/blob/master/src/Client.php#L231-L251

thanks


foreach ($this->data as $line) {
fwrite($fp, $line);
}
if ($errno != 0 || $fp == false) {
throw new \RuntimeException("Couldn't connect to " . $this->host . ':' . $this->port . ' with message: ' . $errmsg);
}

fclose($fp);
} catch (Exception $e) {
foreach ($this->data as $line) {
fwrite($fp, $line);
}

fclose($fp);

$this->data = array();
}

Expand All @@ -113,3 +114,4 @@ public function push($stat, $value, $time = null)
);
}
}

5 changes: 4 additions & 1 deletion src/Beberlei/Metrics/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ public static function create($type, array $options = array())
if (!isset($options['host']) && isset($options['port'])) {
throw new MetricsException('You should specified a host if you specified a port.');
}
if (!isset($options['timeout'])) {
$options['timeout'] = null;
}

return new Collector\Graphite($options['host'], $options['port']);
return new Collector\Graphite($options['host'], $options['port'], $options['timeout']);

case 'zabbix':
if (!isset($options['hostname'])) {
Expand Down