Skip to content

Commit

Permalink
fixed race conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Dubovikov <[email protected]>
  • Loading branch information
adnull committed Oct 9, 2024
1 parent 7fff56c commit f58da42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions mediaapi/routing/url_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand Down Expand Up @@ -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()
}
}()

Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions mediaapi/routing/url_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,16 @@ func Test_ActiveRequestWaiting(t *testing.T) {
}

successResults := 0
successResultsLock := &sync.Mutex{}

for i := 0; i < 3; i++ {
go func() {
if res, ok := checkActivePreviewResponse(activeRequests, "someurl"); ok {
if res.Code != 200 {
t.Errorf("Unsuccess result: %v", res)
}
successResultsLock.Lock()
defer successResultsLock.Unlock()
successResults++
return
}
Expand All @@ -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 {
Expand All @@ -217,9 +222,11 @@ func Test_ActiveRequestWaiting(t *testing.T) {
return
default:
}
successResultsLock.Lock()
if successResults == 3 {
break
}
successResultsLock.Unlock()
}
}

Expand Down
2 changes: 1 addition & 1 deletion mediaapi/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ActiveThumbnailGeneration struct {
}

type UrlPreviewCache struct {
Lock sync.Mutex
sync.Mutex
Records map[string]*UrlPreviewCacheRecord
}

Expand Down

0 comments on commit f58da42

Please sign in to comment.