From 1661f14adb25b9ec546c56a36968839bcefb8ca2 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Tue, 29 Apr 2025 09:04:31 +0100 Subject: [PATCH] Add timeout property --- docs/configuration.md | 6 +++++- internal/glance/widget-monitor.go | 17 ++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0ede32d..cc911c3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1613,7 +1613,6 @@ Example: - title: Vaultwarden url: https://vault.yourdomain.com icon: /assets/vaultwarden-logo.png - ``` Preview: @@ -1651,6 +1650,7 @@ Properties for each site: | check-url | string | no | | | error-url | string | no | | | icon | string | no | | +| timeout | string | no | 3s | | allow-insecure | boolean | no | false | | same-tab | boolean | no | false | | alt-status-codes | array | no | | @@ -1686,6 +1686,10 @@ icon: si:adguard > > Simple Icons are loaded externally and are hosted on `cdn.jsdelivr.net`, if you do not wish to depend on a 3rd party you are free to download the icons individually and host them locally. +`timeout` + +How long to wait for a response from the server before considering it unreachable. The value is a string and must be a number followed by one of s, m, h, d. Example: `5s` for 5 seconds, `1m` for 1 minute, etc. + `allow-insecure` Whether to ignore invalid/self-signed certificates. diff --git a/internal/glance/widget-monitor.go b/internal/glance/widget-monitor.go index e42a710..cdce0d6 100644 --- a/internal/glance/widget-monitor.go +++ b/internal/glance/widget-monitor.go @@ -115,9 +115,10 @@ func statusCodeToStyle(status int, altStatusCodes []int) string { } type SiteStatusRequest struct { - DefaultURL string `yaml:"url"` - CheckURL string `yaml:"check-url"` - AllowInsecure bool `yaml:"allow-insecure"` + DefaultURL string `yaml:"url"` + CheckURL string `yaml:"check-url"` + AllowInsecure bool `yaml:"allow-insecure"` + Timeout durationField `yaml:"timeout"` BasicAuth struct { Username string `yaml:"username"` Password string `yaml:"password"` @@ -138,7 +139,12 @@ func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) { } else { url = statusRequest.DefaultURL } - request, err := http.NewRequest(http.MethodGet, url, nil) + + timeout := ternary(statusRequest.Timeout > 0, time.Duration(statusRequest.Timeout), 3*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return siteStatus{ Error: err, @@ -149,9 +155,6 @@ func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) { request.SetBasicAuth(statusRequest.BasicAuth.Username, statusRequest.BasicAuth.Password) } - ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) - defer cancel() - request = request.WithContext(ctx) requestSentAt := time.Now() var response *http.Response