From 57bbb70c794fc386de9053fc3e77202fa7f9965c Mon Sep 17 00:00:00 2001 From: Cristian Vidmar Date: Wed, 4 Oct 2023 16:27:10 +0200 Subject: [PATCH] fix: various race conditions --- erm/templates/contentful_vo_lib.gotmpl | 20 ++++++++++++++++---- main.go | 2 +- test/testapi/gocontentfulvo.go | 2 +- test/testapi/gocontentfulvobase.go | 2 +- test/testapi/gocontentfulvolib.go | 22 +++++++++++++++++----- test/testapi/gocontentfulvolibbrand.go | 2 +- test/testapi/gocontentfulvolibcategory.go | 2 +- test/testapi/gocontentfulvolibproduct.go | 2 +- 8 files changed, 39 insertions(+), 15 deletions(-) diff --git a/erm/templates/contentful_vo_lib.gotmpl b/erm/templates/contentful_vo_lib.gotmpl index 5b0e9bd..f2aaa4e 100644 --- a/erm/templates/contentful_vo_lib.gotmpl +++ b/erm/templates/contentful_vo_lib.gotmpl @@ -338,7 +338,10 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) { if cc == nil || cc.Client == nil { return "", errors.New("GetContentTypeOfID: No client available") } - if cc.cacheInit { + cc.cacheMutex.sharedDataGcLock.RLock() + cacheInit := cc.cacheInit + cc.cacheMutex.sharedDataGcLock.RUnlock() + if cacheInit { okVo := false {{ range $index , $contentType := $contentTypes }} cc.cacheMutex.{{ $contentType.Sys.ID }}GcLock.Lock() @@ -597,8 +600,8 @@ func (cc *ContentfulClient) SetOfflineFallback(filename string) error { } func (cc *ContentfulClient) SetSyncMode(mode bool) error { - cc.cacheMutex.sharedDataGcLock.RLock() - defer cc.cacheMutex.sharedDataGcLock.RUnlock() + cc.cacheMutex.sharedDataGcLock.Lock() + defer cc.cacheMutex.sharedDataGcLock.Unlock() if cc.offline { return errors.New("SetSyncMode: client is set offline, can't enable sync") } @@ -607,6 +610,8 @@ func (cc *ContentfulClient) SetSyncMode(mode bool) error { } func (cc *ContentfulClient) ResetSync() { + cc.cacheMutex.sharedDataGcLock.Lock() + defer cc.cacheMutex.sharedDataGcLock.Unlock() cc.syncToken = "" } @@ -654,7 +659,9 @@ func (cc *ContentfulClient) UpdateCache(ctx context.Context, contentTypes []stri cc.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateCanceled) } } + cc.cacheMutex.sharedDataGcLock.Lock() cc.cacheInit = true + cc.cacheMutex.sharedDataGcLock.Unlock() if cc.logFn != nil && cc.logLevel <= LogInfo { cc.logFn(map[string]interface{}{"task":"UpdateCache"}, LogInfo, InfoCacheUpdateDone) } @@ -675,11 +682,13 @@ func (cc *ContentfulClient) syncCache(ctx context.Context, contentTypes []string cc.logFn(map[string]interface{}{"task": "syncCache"}, LogInfo, InfoCacheUpdateQueued) } for { + cc.cacheMutex.sharedDataGcLock.RLock() col := cc.Client.Entries.Sync( cc.SpaceID, cc.syncToken == "", cc.syncToken, ) + cc.cacheMutex.sharedDataGcLock.RUnlock() col.GetAll() cc.cacheMutex.sharedDataGcLock.Lock() cc.syncToken = col.SyncToken @@ -1523,7 +1532,10 @@ func updateCacheForContentTypeAndEntity(ctx context.Context, cc *ContentfulClien func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) { parents = []EntryReference{} - if cc.cacheInit { + cc.cacheMutex.sharedDataGcLock.RLock() + cacheInit := cc.cacheInit + cc.cacheMutex.sharedDataGcLock.RUnlock() + if cacheInit { cc.cacheMutex.parentMapGcLock.RLock() defer cc.cacheMutex.parentMapGcLock.RUnlock() if len(contentType) != 0 { diff --git a/main.go b/main.go index dacc615..8b14d80 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "github.com/foomo/gocontentful/erm" ) -var VERSION = "v1.0.25" +var VERSION = "v1.0.26" type contentfulRc struct { ManagementToken string `json:"managementToken"` diff --git a/test/testapi/gocontentfulvo.go b/test/testapi/gocontentfulvo.go index 94dcb77..89ba373 100644 --- a/test/testapi/gocontentfulvo.go +++ b/test/testapi/gocontentfulvo.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import ( diff --git a/test/testapi/gocontentfulvobase.go b/test/testapi/gocontentfulvobase.go index c056f35..3cac386 100644 --- a/test/testapi/gocontentfulvobase.go +++ b/test/testapi/gocontentfulvobase.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import "github.com/foomo/contentful" diff --git a/test/testapi/gocontentfulvolib.go b/test/testapi/gocontentfulvolib.go index b451c18..9934150 100644 --- a/test/testapi/gocontentfulvolib.go +++ b/test/testapi/gocontentfulvolib.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import ( @@ -355,7 +355,10 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) { if cc == nil || cc.Client == nil { return "", errors.New("GetContentTypeOfID: No client available") } - if cc.cacheInit { + cc.cacheMutex.sharedDataGcLock.RLock() + cacheInit := cc.cacheInit + cc.cacheMutex.sharedDataGcLock.RUnlock() + if cacheInit { okVo := false cc.cacheMutex.brandGcLock.Lock() @@ -631,8 +634,8 @@ func (cc *ContentfulClient) SetOfflineFallback(filename string) error { } func (cc *ContentfulClient) SetSyncMode(mode bool) error { - cc.cacheMutex.sharedDataGcLock.RLock() - defer cc.cacheMutex.sharedDataGcLock.RUnlock() + cc.cacheMutex.sharedDataGcLock.Lock() + defer cc.cacheMutex.sharedDataGcLock.Unlock() if cc.offline { return errors.New("SetSyncMode: client is set offline, can't enable sync") } @@ -641,6 +644,8 @@ func (cc *ContentfulClient) SetSyncMode(mode bool) error { } func (cc *ContentfulClient) ResetSync() { + cc.cacheMutex.sharedDataGcLock.Lock() + defer cc.cacheMutex.sharedDataGcLock.Unlock() cc.syncToken = "" } @@ -688,7 +693,9 @@ func (cc *ContentfulClient) UpdateCache(ctx context.Context, contentTypes []stri cc.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateCanceled) } } + cc.cacheMutex.sharedDataGcLock.Lock() cc.cacheInit = true + cc.cacheMutex.sharedDataGcLock.Unlock() if cc.logFn != nil && cc.logLevel <= LogInfo { cc.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateDone) } @@ -709,11 +716,13 @@ func (cc *ContentfulClient) syncCache(ctx context.Context, contentTypes []string cc.logFn(map[string]interface{}{"task": "syncCache"}, LogInfo, InfoCacheUpdateQueued) } for { + cc.cacheMutex.sharedDataGcLock.RLock() col := cc.Client.Entries.Sync( cc.SpaceID, cc.syncToken == "", cc.syncToken, ) + cc.cacheMutex.sharedDataGcLock.RUnlock() col.GetAll() cc.cacheMutex.sharedDataGcLock.Lock() cc.syncToken = col.SyncToken @@ -1615,7 +1624,10 @@ func updateCacheForContentTypeAndEntity(ctx context.Context, cc *ContentfulClien func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) { parents = []EntryReference{} - if cc.cacheInit { + cc.cacheMutex.sharedDataGcLock.RLock() + cacheInit := cc.cacheInit + cc.cacheMutex.sharedDataGcLock.RUnlock() + if cacheInit { cc.cacheMutex.parentMapGcLock.RLock() defer cc.cacheMutex.parentMapGcLock.RUnlock() if len(contentType) != 0 { diff --git a/test/testapi/gocontentfulvolibbrand.go b/test/testapi/gocontentfulvolibbrand.go index d75c050..d5127f9 100644 --- a/test/testapi/gocontentfulvolibbrand.go +++ b/test/testapi/gocontentfulvolibbrand.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import ( diff --git a/test/testapi/gocontentfulvolibcategory.go b/test/testapi/gocontentfulvolibcategory.go index 1c41822..f9d3770 100644 --- a/test/testapi/gocontentfulvolibcategory.go +++ b/test/testapi/gocontentfulvolibcategory.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import ( diff --git a/test/testapi/gocontentfulvolibproduct.go b/test/testapi/gocontentfulvolibproduct.go index 2c06d92..d767562 100644 --- a/test/testapi/gocontentfulvolibproduct.go +++ b/test/testapi/gocontentfulvolibproduct.go @@ -1,4 +1,4 @@ -// Code generated by https://github.com/foomo/gocontentful v1.0.24 - DO NOT EDIT. +// Code generated by https://github.com/foomo/gocontentful v1.0.26 - DO NOT EDIT. package testapi import (