From 044c7bf8a0918d30af719c4ac6134c8facd484ec Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 26 Jan 2026 14:17:41 +0800 Subject: [PATCH] feat(proxmox): add session refresh loop to maintain Proxmox API session Introduced a new session refresh mechanism in the Proxmox configuration to ensure the API session remains active. This includes: - Added `SessionRefreshInterval` constant for configurable session refresh timing. - Implemented `refreshSessionLoop` method to periodically refresh the session and handle errors with exponential backoff. This enhancement improves the reliability of interactions with the Proxmox API by preventing session expiry. --- internal/proxmox/config.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/proxmox/config.go b/internal/proxmox/config.go index 82195b0f..35d54445 100644 --- a/internal/proxmox/config.go +++ b/internal/proxmox/config.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "errors" + "math" "net/http" "strings" "time" @@ -31,6 +32,7 @@ type Config struct { } const ResourcePollInterval = 3 * time.Second +const SessionRefreshInterval = 1 * time.Minute // NodeStatsPollInterval controls how often node stats are streamed when streaming is enabled. const NodeStatsPollInterval = time.Second @@ -106,6 +108,7 @@ func (c *Config) Init(ctx context.Context) gperr.Error { } go c.updateResourcesLoop(ctx) + go c.refreshSessionLoop(ctx) return nil } @@ -130,3 +133,33 @@ func (c *Config) updateResourcesLoop(ctx context.Context) { } } } + +func (c *Config) refreshSessionLoop(ctx context.Context) { + ticker := time.NewTicker(SessionRefreshInterval) + defer ticker.Stop() + + log.Trace().Str("cluster", c.client.Cluster.Name).Msg("[proxmox] starting session refresh loop") + + numRetries := 0 + + for { + select { + case <-ctx.Done(): + log.Trace().Str("cluster", c.client.Cluster.Name).Msg("[proxmox] stopping session refresh loop") + return + case <-ticker.C: + reqCtx, reqCtxCancel := context.WithTimeout(ctx, SessionRefreshInterval) + err := c.client.RefreshSession(reqCtx) + reqCtxCancel() + if err != nil { + log.Error().Err(err).Str("cluster", c.client.Cluster.Name).Msg("[proxmox] failed to refresh session") + // exponential backoff + numRetries++ + backoff := time.Duration(min(math.Pow(2, float64(numRetries)), 10)) * time.Second + ticker.Reset(backoff) + } else { + ticker.Reset(SessionRefreshInterval) + } + } + } +} \ No newline at end of file