diff --git a/application/forms/Config/BackendForm.php b/application/forms/Config/BackendForm.php index 90e0af2f..fb3970b7 100644 --- a/application/forms/Config/BackendForm.php +++ b/application/forms/Config/BackendForm.php @@ -53,6 +53,16 @@ public function createElements(array $formData) 'label' => $this->translate('Connect insecurely'), 'description' => $this->translate('Check this to not verify the remote\'s TLS certificate') ] + ], + [ + 'number', + 'graphite_timeout', + [ + 'label' => $this->translate('Request timeout'), + 'description' => $this->translate('The timeout for HTTP requests to Graphite Web'), + 'min' => 0, + 'placeholder' => 10 + ] ] ]); } diff --git a/library/Graphite/Graphing/GraphingTrait.php b/library/Graphite/Graphing/GraphingTrait.php index a1c1cdc2..463c61ca 100644 --- a/library/Graphite/Graphing/GraphingTrait.php +++ b/library/Graphite/Graphing/GraphingTrait.php @@ -73,6 +73,7 @@ public static function getMetricsDataSource() ->setUser($graphite->user) ->setPassword($graphite->password) ->setInsecure((bool) $graphite->insecure) + ->setTimeout(isset($graphite->timeout) ? intval($graphite->timeout) : 10) ); } diff --git a/library/Graphite/Graphing/GraphiteWebClient.php b/library/Graphite/Graphing/GraphiteWebClient.php index 982a2afe..b8f3d599 100644 --- a/library/Graphite/Graphing/GraphiteWebClient.php +++ b/library/Graphite/Graphing/GraphiteWebClient.php @@ -40,6 +40,13 @@ class GraphiteWebClient */ protected $insecure = false; + /** + * Timeout for every Graphite Web HTTP request + * + * @var int + */ + protected $timeout = 10; + /** * HTTP client * @@ -79,9 +86,12 @@ public function request(Url $url, $method = 'GET', array $headers = [], $body = // TODO(ak): keep connections alive (TCP handshakes are a bit expensive and TLS handshakes are very expensive) return (string) $this->httpClient->send( new Request($method, $this->completeUrl($url)->getAbsoluteUrl(), $headers, $body), - ['curl' => [ - CURLOPT_SSL_VERIFYPEER => ! $this->insecure - ]] + [ + 'curl' => [ + CURLOPT_SSL_VERIFYPEER => ! $this->insecure + ], + 'timeout' => $this->timeout + ] )->getBody(); } @@ -195,4 +205,28 @@ public function setInsecure($insecure = true) return $this; } + + /** + * Get the HTTP request timeout + * + * @return int + */ + public function getTimeout(): int + { + return $this->timeout; + } + + /** + * Set the HTTP request timeout + * + * @param int $timeout + * + * @return $this + */ + public function setTimeout(int $timeout): self + { + $this->timeout = $timeout; + + return $this; + } }