Skip to content

Commit 376dbee

Browse files
committed
refactor: change resync commands creation
1 parent 5c17550 commit 376dbee

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

packages/cmd/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func startProxyServer(cmd *cobra.Command, args []string) {
489489
resyncCtx, resyncCancel := context.WithCancel(context.Background())
490490
defer resyncCancel()
491491

492-
go proxy.StartBackgroundLoops(resyncCtx, cache, domainURL, httpClient, evictionStrategy, accessTokenCheckInterval, staticSecretsRefreshInterval, useSSE)
492+
proxy.StartBackgroundLoops(resyncCtx, cache, domainURL, httpClient, evictionStrategy, accessTokenCheckInterval, staticSecretsRefreshInterval, useSSE)
493493

494494
// Handle graceful shutdown
495495
sigCh := make(chan os.Signal, 1)

packages/proxy/resync.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -289,38 +289,47 @@ func runStaticSecretsRefresh(cache *Cache, domainURL *url.URL, httpClient *http.
289289
Msg("Static secrets refresh completed")
290290
}
291291

292-
// StartBackgroundLoops starts the background loops for token validation and secrets refresh.
293-
// When sseEnabled is true, the static secrets refresh loop is disabled (SSE handles cache invalidation).
294-
func StartBackgroundLoops(ctx context.Context, cache *Cache, domainURL *url.URL, httpClient *http.Client, evictionStrategy string, accessTokenCheckInterval time.Duration, staticSecretsRefreshInterval time.Duration, sseEnabled bool) {
292+
func startAccessTokenValidation(ctx context.Context, cache *Cache, domainURL *url.URL, httpClient *http.Client, accessTokenCheckInterval time.Duration) {
295293
tokenTicker := time.NewTicker(accessTokenCheckInterval)
296294
defer tokenTicker.Stop()
297295

298-
var secretsTickerC <-chan time.Time
299-
if !sseEnabled {
300-
secretsTicker := time.NewTicker(staticSecretsRefreshInterval)
301-
defer secretsTicker.Stop()
302-
secretsTickerC = secretsTicker.C
303-
} else {
304-
secretsTickerC = make(chan time.Time)
305-
log.Info().Msg("Static secrets refresh disabled (SSE mode active)")
296+
for {
297+
select {
298+
case <-tokenTicker.C:
299+
runAccessTokenValidation(cache, domainURL, httpClient)
300+
case <-ctx.Done():
301+
return
302+
}
306303
}
304+
}
307305

308-
log.Info().
309-
Str("evictionStrategy", evictionStrategy).
310-
Str("accessTokenCheckInterval", accessTokenCheckInterval.String()).
311-
Str("staticSecretsRefreshInterval", staticSecretsRefreshInterval.String()).
312-
Bool("sseEnabled", sseEnabled).
313-
Msg("Background loops started")
306+
func startStaticSecretsRefresh(ctx context.Context, cache *Cache, domainURL *url.URL, httpClient *http.Client, staticSecretsRefreshInterval time.Duration) {
307+
secretsTicker := time.NewTicker(staticSecretsRefreshInterval)
308+
defer secretsTicker.Stop()
314309

315310
for {
316311
select {
317-
case <-tokenTicker.C:
318-
runAccessTokenValidation(cache, domainURL, httpClient)
319-
case <-secretsTickerC:
312+
case <-secretsTicker.C:
320313
runStaticSecretsRefresh(cache, domainURL, httpClient, staticSecretsRefreshInterval)
321314
case <-ctx.Done():
322-
log.Info().Msg("Background loops stopped")
323315
return
324316
}
325317
}
326318
}
319+
320+
// StartBackgroundLoops starts the background loops for token validation and secrets refresh.
321+
// When sseEnabled is true, the static secrets refresh loop is disabled (SSE handles cache invalidation).
322+
func StartBackgroundLoops(ctx context.Context, cache *Cache, domainURL *url.URL, httpClient *http.Client, evictionStrategy string, accessTokenCheckInterval time.Duration, staticSecretsRefreshInterval time.Duration, sseEnabled bool) {
323+
log.Info().
324+
Str("evictionStrategy", evictionStrategy).
325+
Str("accessTokenCheckInterval", accessTokenCheckInterval.String()).
326+
Str("staticSecretsRefreshInterval", staticSecretsRefreshInterval.String()).
327+
Bool("sseEnabled", sseEnabled).
328+
Msg("Background loops started")
329+
330+
if sseEnabled {
331+
go startAccessTokenValidation(ctx, cache, domainURL, httpClient, accessTokenCheckInterval)
332+
}
333+
go startStaticSecretsRefresh(ctx, cache, domainURL, httpClient, staticSecretsRefreshInterval)
334+
335+
}

packages/proxy/sse.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ func NewSSEAuthState(clientId, clientSecret string, domainURL *url.URL, httpClie
9696
}
9797

9898
// ensure we have a valid token
99-
authState.RefreshToken()
99+
_, err := authState.RefreshToken()
100+
if err != nil {
101+
log.Error().Err(err).Msg("Failed to refresh SSE auth token")
102+
}
100103

101104
return authState
102105
}
@@ -187,6 +190,7 @@ type SSEManager struct {
187190
}
188191

189192
func NewSSEManager(ctx context.Context, cache *Cache, domainURL *url.URL, httpClient *http.Client, resyncHttpClient *http.Client, authState *SSEAuthState) *SSEManager {
193+
190194
return &SSEManager{
191195
connections: make(map[string]*SSEConnection),
192196
cache: cache,

0 commit comments

Comments
 (0)