fix(monitoring): refresh cached public IP addresses (#6933)

The public IPv4/IPv6 lookup was cached for the life of the panel process, so
Overview kept showing an old address after the server's external IP changed,
and a failed first lookup pinned N/A until a restart. Addresses now refresh
every five minutes, a failed IPv4 lookup retries after a minute, failed IPv6
lookups back off 1/2/5/15 minutes, and a temporary failure keeps the last
known address. Status sampling still never waits on the lookup.
This commit is contained in:
VAIO73
2026-10-25 13:50:29 +01:00
committed by GitHub
parent 56ac8d0fc7
commit be25314157
4 changed files with 586 additions and 40 deletions
+73 -38
View File
@@ -154,8 +154,10 @@ type ServerService struct {
settingService SettingService
cachedIPv4 string
cachedIPv6 string
noIPv6 bool
resolvingIPs bool
nextIPv4Lookup time.Time
nextIPv6Lookup time.Time
ipv6RetryInterval time.Duration
publicIPLookupDone chan struct{}
mu sync.Mutex
lastCPUTimes cpu.TimesStat
hasLastCPUSample bool
@@ -453,35 +455,41 @@ var publicIPv6Services = []string{
"https://6.ident.me",
}
// resolvePublicIPs caches the public IPv4/IPv6 addresses on first use. The
// lookups run outside s.mu so a stalling service cannot block a status sample.
const (
publicIPRefreshInterval = 5 * time.Minute
publicIPRetryInterval = time.Minute
)
// resolvePublicIPs waits for the initial lookup so cold backups can use it.
// Warm callers use cached addresses while a refresh runs in the background.
func (s *ServerService) resolvePublicIPs() {
s.mu.Lock()
wantIPv4 := s.cachedIPv4 == ""
wantIPv6 := s.cachedIPv6 == "" && !s.noIPv6
s.mu.Unlock()
if !wantIPv4 && !wantIPv6 {
return
ipv4, ipv6 := s.publicIPs()
if done := s.resolvePublicIPsInBackground(); done != nil && ipv4 == "" && ipv6 == "" {
<-done
}
}
var ipv4, ipv6 string
if wantIPv4 {
ipv4 = firstPublicIP(publicIPv4Services)
// refreshedPublicIP retains a known address through temporary lookup failures.
func refreshedPublicIP(cached, fetched string, now time.Time) (string, time.Time) {
if fetched != "" && fetched != "N/A" {
return fetched, now.Add(publicIPRefreshInterval)
}
if wantIPv6 {
ipv6 = firstPublicIP(publicIPv6Services)
if cached == "" {
cached = "N/A"
}
return cached, now.Add(publicIPRetryInterval)
}
s.mu.Lock()
defer s.mu.Unlock()
if wantIPv4 && s.cachedIPv4 == "" {
s.cachedIPv4 = ipv4
}
if wantIPv6 && s.cachedIPv6 == "" {
s.cachedIPv6 = ipv6
}
if s.cachedIPv6 == "N/A" {
s.noIPv6 = true
func nextPublicIPv6RetryInterval(previous time.Duration) time.Duration {
switch {
case previous < time.Minute:
return time.Minute
case previous < 2*time.Minute:
return 2 * time.Minute
case previous < 5*time.Minute:
return 5 * time.Minute
default:
return 15 * time.Minute
}
}
@@ -498,26 +506,53 @@ func firstPublicIP(services []string) string {
return ip
}
// resolvePublicIPsInBackground keeps a status sample off the lookup path: a box
// with no IPv6 route spends 3s per service, and the sample is what nodes report.
func (s *ServerService) resolvePublicIPsInBackground() {
// resolvePublicIPsInBackground shares one lookup without blocking status samples.
// Each address family has its own deadline; cached values stay readable.
func (s *ServerService) resolvePublicIPsInBackground() <-chan struct{} {
s.mu.Lock()
settled := s.cachedIPv4 != "" && (s.cachedIPv6 != "" || s.noIPv6)
if s.resolvingIPs || settled {
if done := s.publicIPLookupDone; done != nil {
s.mu.Unlock()
return
return done
}
s.resolvingIPs = true
now := time.Now()
wantIPv4 := !now.Before(s.nextIPv4Lookup)
wantIPv6 := !now.Before(s.nextIPv6Lookup)
if !wantIPv4 && !wantIPv6 {
s.mu.Unlock()
return nil
}
done := make(chan struct{})
s.publicIPLookupDone = done
s.mu.Unlock()
go func() {
defer func() {
s.mu.Lock()
s.resolvingIPs = false
s.mu.Unlock()
}()
s.resolvePublicIPs()
var ipv4, ipv6 string
if wantIPv4 {
ipv4 = firstPublicIP(publicIPv4Services)
}
if wantIPv6 {
ipv6 = firstPublicIP(publicIPv6Services)
}
s.mu.Lock()
defer s.mu.Unlock()
now := time.Now()
if wantIPv4 {
s.cachedIPv4, s.nextIPv4Lookup = refreshedPublicIP(s.cachedIPv4, ipv4, now)
}
if wantIPv6 {
s.cachedIPv6, s.nextIPv6Lookup = refreshedPublicIP(s.cachedIPv6, ipv6, now)
if ipv6 != "" && ipv6 != "N/A" {
s.ipv6RetryInterval = 0
} else {
s.ipv6RetryInterval = nextPublicIPv6RetryInterval(s.ipv6RetryInterval)
s.nextIPv6Lookup = now.Add(s.ipv6RetryInterval)
}
}
s.publicIPLookupDone = nil
close(done)
}()
return done
}
func (s *ServerService) publicIPs() (ipv4 string, ipv6 string) {