fixed race conditions

Signed-off-by: Aleksandr Dubovikov <d.lexand@gmail.com>
This commit is contained in:
Aleksandr Dubovikov 2024-10-09 20:59:59 +02:00
parent 7fff56c758
commit f58da42718
3 changed files with 15 additions and 8 deletions

View file

@ -56,14 +56,14 @@ func makeUrlPreviewHandler(
go func() { go func() {
for { for {
t := time.Now().Unix() t := time.Now().Unix()
urlPreviewCache.Lock()
for k, record := range urlPreviewCache.Records { for k, record := range urlPreviewCache.Records {
if record.Created < (t - int64(cfg.UrlPreviewCacheTime)) { if record.Created < (t - int64(cfg.UrlPreviewCacheTime)) {
urlPreviewCache.Lock.Lock()
delete(urlPreviewCache.Records, k) 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(), Created: time.Now().Unix(),
Preview: urlPreviewCached, Preview: urlPreviewCached,
} }
urlPreviewCache.Lock.Lock() urlPreviewCache.Lock()
urlPreviewCache.Records[pUrl] = urlPreviewCacheItem 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 urlPreviewCache.Records[pUrl] = urlPreviewCacheItem
defer urlPreviewCache.Lock.Unlock() defer urlPreviewCache.Unlock()
activeUrlPreviewRequests.Lock() activeUrlPreviewRequests.Lock()
activeUrlPreviewRequests.Url[pUrl].Cond.Broadcast() activeUrlPreviewRequests.Url[pUrl].Cond.Broadcast()

View file

@ -190,6 +190,7 @@ func Test_ActiveRequestWaiting(t *testing.T) {
} }
successResults := 0 successResults := 0
successResultsLock := &sync.Mutex{}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
go func() { go func() {
@ -197,6 +198,8 @@ func Test_ActiveRequestWaiting(t *testing.T) {
if res.Code != 200 { if res.Code != 200 {
t.Errorf("Unsuccess result: %v", res) t.Errorf("Unsuccess result: %v", res)
} }
successResultsLock.Lock()
defer successResultsLock.Unlock()
successResults++ successResults++
return return
} }
@ -205,9 +208,11 @@ func Test_ActiveRequestWaiting(t *testing.T) {
} }
time.Sleep(time.Duration(1) * time.Second) time.Sleep(time.Duration(1) * time.Second)
successResultsLock.Lock()
if successResults != 0 { if successResults != 0 {
t.Error("Subroutines didn't wait") t.Error("Subroutines didn't wait")
} }
successResultsLock.Unlock()
activeRequests.Url["someurl"].Cond.Broadcast() activeRequests.Url["someurl"].Cond.Broadcast()
to := time.After(1 * time.Second) to := time.After(1 * time.Second)
for { for {
@ -217,9 +222,11 @@ func Test_ActiveRequestWaiting(t *testing.T) {
return return
default: default:
} }
successResultsLock.Lock()
if successResults == 3 { if successResults == 3 {
break break
} }
successResultsLock.Unlock()
} }
} }

View file

@ -101,7 +101,7 @@ type ActiveThumbnailGeneration struct {
} }
type UrlPreviewCache struct { type UrlPreviewCache struct {
Lock sync.Mutex sync.Mutex
Records map[string]*UrlPreviewCacheRecord Records map[string]*UrlPreviewCacheRecord
} }