Skip to content

Commit 748b09f

Browse files
0xJackycursoragent
andauthored
Refactor: Avoid network requests when health check is disabled (#1447)
This change ensures that when a site's health check is disabled, the checker returns cached metadata without making any network requests, as per issue #1446. A new helper function `getExistingSiteSnapshot` is introduced to retrieve this cached information. A new test case `TestCheckSiteSkipsNetworkWhenDisabled` is added to verify this behavior. Co-authored-by: Cursor Agent <[email protected]>
1 parent 39fc181 commit 748b09f

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

internal/sitecheck/checker.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,25 @@ func (sc *SiteChecker) CheckSite(ctx context.Context, siteURL string) (*SiteInfo
293293
// Try enhanced health check first if config exists
294294
config, err := LoadSiteConfig(siteURL)
295295

296-
// If health check is disabled, return a SiteInfo without status
296+
// If health check is disabled, return cached metadata without issuing any network requests (#1446)
297297
if err == nil && config != nil && !config.HealthCheckEnabled {
298-
protocol := "http"
299-
if config.HealthCheckConfig != nil && config.HealthCheckConfig.Protocol != "" {
300-
protocol = config.HealthCheckConfig.Protocol
301-
}
302-
303298
siteInfo := &SiteInfo{
304299
SiteConfig: *config,
305300
Name: extractDomainName(siteURL),
306301
Title: config.DisplayURL,
307302
}
308303

309-
// Try to get favicon if enabled and not a gRPC check
310-
if sc.options.CheckFavicon && !isGRPCProtocol(protocol) {
311-
faviconURL, faviconData := sc.tryGetFavicon(ctx, siteURL)
312-
siteInfo.FaviconURL = faviconURL
313-
siteInfo.FaviconData = faviconData
304+
if existing := sc.getExistingSiteSnapshot(siteURL); existing != nil {
305+
siteInfo.FaviconURL = existing.FaviconURL
306+
siteInfo.FaviconData = existing.FaviconData
307+
siteInfo.Status = existing.Status
308+
siteInfo.StatusCode = existing.StatusCode
309+
siteInfo.ResponseTime = existing.ResponseTime
310+
siteInfo.LastChecked = existing.LastChecked
311+
siteInfo.Error = existing.Error
312+
if siteInfo.Title == "" {
313+
siteInfo.Title = existing.Title
314+
}
314315
}
315316

316317
return siteInfo, nil
@@ -522,6 +523,20 @@ func (sc *SiteChecker) GetSitesList() []*SiteInfo {
522523
return result
523524
}
524525

526+
// getExistingSiteSnapshot returns a copy of the last known site info, if present.
527+
func (sc *SiteChecker) getExistingSiteSnapshot(siteURL string) *SiteInfo {
528+
sc.mu.RLock()
529+
defer sc.mu.RUnlock()
530+
531+
existing, ok := sc.sites[siteURL]
532+
if !ok || existing == nil {
533+
return nil
534+
}
535+
536+
clone := *existing
537+
return &clone
538+
}
539+
525540
// extractDomainName extracts domain name from URL
526541
func extractDomainName(siteURL string) string {
527542
parsed, err := url.Parse(siteURL)

internal/sitecheck/checker_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sitecheck
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/0xJacky/Nginx-UI/model"
9+
)
10+
11+
type roundTripFunc func(*http.Request) (*http.Response, error)
12+
13+
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
14+
return f(req)
15+
}
16+
17+
func TestCheckSiteSkipsNetworkWhenDisabled(t *testing.T) {
18+
t.Cleanup(InvalidateSiteConfigCache)
19+
20+
options := DefaultCheckOptions()
21+
checker := NewSiteChecker(options)
22+
23+
// Any HTTP request made by the checker should fail this test.
24+
checker.client = &http.Client{
25+
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
26+
t.Fatalf("unexpected HTTP request to %s while health check is disabled", req.URL.String())
27+
return nil, nil
28+
}),
29+
}
30+
31+
const siteURL = "https://example.com"
32+
config := &model.SiteConfig{
33+
Model: model.Model{ID: 1},
34+
Host: "example.com:443",
35+
Scheme: "https",
36+
DisplayURL: siteURL,
37+
HealthCheckEnabled: false,
38+
HealthCheckConfig: &model.HealthCheckConfig{
39+
Protocol: "https",
40+
},
41+
}
42+
43+
setCachedSiteConfig(config.Host, config)
44+
45+
if _, err := checker.CheckSite(context.Background(), siteURL); err != nil {
46+
t.Fatalf("CheckSite returned error: %v", err)
47+
}
48+
}

0 commit comments

Comments
 (0)