Skip to content

Commit 5e0d57c

Browse files
atjnilmerg
authored andcommitted
Add Graphite Web HTTP request timeout option
If the Graphite Web server is unreachable, all requests for frontend pages containing graphs hang until the backend HTTP request times out, resulting in a very poor UX. The Guzzle documentation states that the default behaviour is to wait indefinitely, however in our testing the cURL handler has an internal default of 30 seconds: https://docs.guzzlephp.org/en/stable/request-options.html#timeout This commit makes the HTTP request timeout configurable and sets a reasonable default of 10 seconds.
1 parent f4cdeee commit 5e0d57c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

application/forms/Config/BackendForm.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public function createElements(array $formData)
5353
'label' => $this->translate('Connect insecurely'),
5454
'description' => $this->translate('Check this to not verify the remote\'s TLS certificate')
5555
]
56+
],
57+
[
58+
'number',
59+
'graphite_timeout',
60+
[
61+
'label' => $this->translate('Request timeout'),
62+
'description' => $this->translate('The timeout for HTTP requests to Graphite Web'),
63+
'min' => 0,
64+
'placeholder' => 10
65+
]
5666
]
5767
]);
5868
}

library/Graphite/Graphing/GraphingTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static function getMetricsDataSource()
7373
->setUser($graphite->user)
7474
->setPassword($graphite->password)
7575
->setInsecure((bool) $graphite->insecure)
76+
->setTimeout(isset($graphite->timeout) ? intval($graphite->timeout) : 10)
7677
);
7778
}
7879

library/Graphite/Graphing/GraphiteWebClient.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class GraphiteWebClient
4040
*/
4141
protected $insecure = false;
4242

43+
/**
44+
* Timeout for every Graphite Web HTTP request
45+
*
46+
* @var ?int
47+
*/
48+
protected $timeout;
49+
4350
/**
4451
* HTTP client
4552
*
@@ -79,9 +86,12 @@ public function request(Url $url, $method = 'GET', array $headers = [], $body =
7986
// TODO(ak): keep connections alive (TCP handshakes are a bit expensive and TLS handshakes are very expensive)
8087
return (string) $this->httpClient->send(
8188
new Request($method, $this->completeUrl($url)->getAbsoluteUrl(), $headers, $body),
82-
['curl' => [
83-
CURLOPT_SSL_VERIFYPEER => ! $this->insecure
84-
]]
89+
[
90+
'curl' => [
91+
CURLOPT_SSL_VERIFYPEER => ! $this->insecure
92+
],
93+
'timeout' => $this->timeout ?? 10
94+
]
8595
)->getBody();
8696
}
8797

@@ -195,4 +205,28 @@ public function setInsecure($insecure = true)
195205

196206
return $this;
197207
}
208+
209+
/**
210+
* Get the HTTP request timeout
211+
*
212+
* @return ?int
213+
*/
214+
public function getTimeout(): ?int
215+
{
216+
return $this->timeout;
217+
}
218+
219+
/**
220+
* Set the HTTP request timeout
221+
*
222+
* @param ?int $timeout
223+
*
224+
* @return $this
225+
*/
226+
public function setTimeout(?int $timeout): self
227+
{
228+
$this->timeout = $timeout;
229+
230+
return $this;
231+
}
198232
}

0 commit comments

Comments
 (0)