From c59ab4caa2660d2d436bdc0c50ae8cd456ede86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Leszko?= Date: Wed, 4 Sep 2024 16:30:29 +0200 Subject: [PATCH] Fix locking during session invalidation (#1377) * Invalidate sessions outside lock since it's an HTTP operation * Add timeout to Mist Client calls and run session invalidation in a separate goroutine * Add log for session invalidation --- clients/mist_client.go | 4 +++- handlers/accesscontrol/access-control.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/clients/mist_client.go b/clients/mist_client.go index 314be7f25..2b9d17a5a 100644 --- a/clients/mist_client.go +++ b/clients/mist_client.go @@ -238,7 +238,9 @@ type MistPushStats struct { Tracks []int `json:"tracks"` } -var mistRetryableClient = newRetryableClient(nil) +const MIST_CLIENT_TIMEOUT = 1 * time.Minute + +var mistRetryableClient = newRetryableClient(&http.Client{Timeout: MIST_CLIENT_TIMEOUT}) func (mc *MistClient) AddStream(streamName, sourceUrl string) error { c := commandAddStream(streamName, sourceUrl) diff --git a/handlers/accesscontrol/access-control.go b/handlers/accesscontrol/access-control.go index 18619e251..3fde6d58d 100644 --- a/handlers/accesscontrol/access-control.go +++ b/handlers/accesscontrol/access-control.go @@ -124,18 +124,26 @@ func (ac *AccessControlHandlersCollection) periodicRefreshIntervalCache(mapic mi time.Sleep(5 * time.Second) ac.mutex.Lock() refreshIntervalCache.mux.Lock() + var keysToInvalidate []string for key := range refreshIntervalCache.data { if time.Since(refreshIntervalCache.data[key].LastRefresh) > time.Duration(refreshIntervalCache.data[key].RefreshInterval)*time.Second { refreshIntervalCache.data[key].LastRefresh = time.Now() - mapic.InvalidateAllSessions(key) + keysToInvalidate = append(keysToInvalidate, key) for cachedAccessKey := range ac.cache[key] { delete(ac.cache[key], cachedAccessKey) } break } } - ac.mutex.Unlock() refreshIntervalCache.mux.Unlock() + ac.mutex.Unlock() + + go func() { + glog.Infof("Invalidating sessions, count=%d", len(keysToInvalidate)) + for _, key := range keysToInvalidate { + mapic.InvalidateAllSessions(key) + } + }() } }() }