Skip to content

Commit

Permalink
fix: various race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Vidmar committed Oct 4, 2023
1 parent 9b9fdad commit 57bbb70
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
20 changes: 16 additions & 4 deletions erm/templates/contentful_vo_lib.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 = ""
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvobase.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions test/testapi/gocontentfulvolib.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvolibbrand.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvolibcategory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testapi/gocontentfulvolibproduct.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57bbb70

Please sign in to comment.