From f58da42718f888c7aa8472d3b66dc1d7115b6701 Mon Sep 17 00:00:00 2001 From: Aleksandr Dubovikov Date: Wed, 9 Oct 2024 20:59:59 +0200 Subject: [PATCH] fixed race conditions Signed-off-by: Aleksandr Dubovikov --- mediaapi/routing/url_preview.go | 14 +++++++------- mediaapi/routing/url_preview_test.go | 7 +++++++ mediaapi/types/types.go | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/mediaapi/routing/url_preview.go b/mediaapi/routing/url_preview.go index 4723746bf..af011820f 100644 --- a/mediaapi/routing/url_preview.go +++ b/mediaapi/routing/url_preview.go @@ -56,14 +56,14 @@ func makeUrlPreviewHandler( go func() { for { t := time.Now().Unix() + urlPreviewCache.Lock() for k, record := range urlPreviewCache.Records { if record.Created < (t - int64(cfg.UrlPreviewCacheTime)) { - urlPreviewCache.Lock.Lock() delete(urlPreviewCache.Records, k) - urlPreviewCache.Lock.Unlock() } } - time.Sleep(time.Duration(16) * time.Second) + urlPreviewCache.Unlock() + time.Sleep(time.Duration(60) * time.Second) } }() @@ -115,9 +115,9 @@ func makeUrlPreviewHandler( Created: time.Now().Unix(), Preview: urlPreviewCached, } - urlPreviewCache.Lock.Lock() + urlPreviewCache.Lock() urlPreviewCache.Records[pUrl] = urlPreviewCacheItem - defer urlPreviewCache.Lock.Unlock() + defer urlPreviewCache.Unlock() } }() @@ -155,9 +155,9 @@ func makeUrlPreviewHandler( } } - urlPreviewCache.Lock.Lock() + urlPreviewCache.Lock() urlPreviewCache.Records[pUrl] = urlPreviewCacheItem - defer urlPreviewCache.Lock.Unlock() + defer urlPreviewCache.Unlock() activeUrlPreviewRequests.Lock() activeUrlPreviewRequests.Url[pUrl].Cond.Broadcast() diff --git a/mediaapi/routing/url_preview_test.go b/mediaapi/routing/url_preview_test.go index 6c531e6e8..6a46e0b6f 100644 --- a/mediaapi/routing/url_preview_test.go +++ b/mediaapi/routing/url_preview_test.go @@ -190,6 +190,7 @@ func Test_ActiveRequestWaiting(t *testing.T) { } successResults := 0 + successResultsLock := &sync.Mutex{} for i := 0; i < 3; i++ { go func() { @@ -197,6 +198,8 @@ func Test_ActiveRequestWaiting(t *testing.T) { if res.Code != 200 { t.Errorf("Unsuccess result: %v", res) } + successResultsLock.Lock() + defer successResultsLock.Unlock() successResults++ return } @@ -205,9 +208,11 @@ func Test_ActiveRequestWaiting(t *testing.T) { } time.Sleep(time.Duration(1) * time.Second) + successResultsLock.Lock() if successResults != 0 { t.Error("Subroutines didn't wait") } + successResultsLock.Unlock() activeRequests.Url["someurl"].Cond.Broadcast() to := time.After(1 * time.Second) for { @@ -217,9 +222,11 @@ func Test_ActiveRequestWaiting(t *testing.T) { return default: } + successResultsLock.Lock() if successResults == 3 { break } + successResultsLock.Unlock() } } diff --git a/mediaapi/types/types.go b/mediaapi/types/types.go index 681454177..d60d3c46c 100644 --- a/mediaapi/types/types.go +++ b/mediaapi/types/types.go @@ -101,7 +101,7 @@ type ActiveThumbnailGeneration struct { } type UrlPreviewCache struct { - Lock sync.Mutex + sync.Mutex Records map[string]*UrlPreviewCacheRecord }