fix: various race conditions

This commit is contained in:
Cristian Vidmar 2023-10-04 16:27:10 +02:00
parent 9b9fdad2cd
commit 57bbb70c79
8 changed files with 39 additions and 15 deletions

View File

@ -338,7 +338,10 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
if cc == nil || cc.Client == nil { if cc == nil || cc.Client == nil {
return "", errors.New("GetContentTypeOfID: No client available") 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 okVo := false
{{ range $index , $contentType := $contentTypes }} {{ range $index , $contentType := $contentTypes }}
cc.cacheMutex.{{ $contentType.Sys.ID }}GcLock.Lock() 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 { func (cc *ContentfulClient) SetSyncMode(mode bool) error {
cc.cacheMutex.sharedDataGcLock.RLock() cc.cacheMutex.sharedDataGcLock.Lock()
defer cc.cacheMutex.sharedDataGcLock.RUnlock() defer cc.cacheMutex.sharedDataGcLock.Unlock()
if cc.offline { if cc.offline {
return errors.New("SetSyncMode: client is set offline, can't enable sync") 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() { func (cc *ContentfulClient) ResetSync() {
cc.cacheMutex.sharedDataGcLock.Lock()
defer cc.cacheMutex.sharedDataGcLock.Unlock()
cc.syncToken = "" 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.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateCanceled)
} }
} }
cc.cacheMutex.sharedDataGcLock.Lock()
cc.cacheInit = true cc.cacheInit = true
cc.cacheMutex.sharedDataGcLock.Unlock()
if cc.logFn != nil && cc.logLevel <= LogInfo { if cc.logFn != nil && cc.logLevel <= LogInfo {
cc.logFn(map[string]interface{}{"task":"UpdateCache"}, LogInfo, InfoCacheUpdateDone) 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) cc.logFn(map[string]interface{}{"task": "syncCache"}, LogInfo, InfoCacheUpdateQueued)
} }
for { for {
cc.cacheMutex.sharedDataGcLock.RLock()
col := cc.Client.Entries.Sync( col := cc.Client.Entries.Sync(
cc.SpaceID, cc.SpaceID,
cc.syncToken == "", cc.syncToken == "",
cc.syncToken, cc.syncToken,
) )
cc.cacheMutex.sharedDataGcLock.RUnlock()
col.GetAll() col.GetAll()
cc.cacheMutex.sharedDataGcLock.Lock() cc.cacheMutex.sharedDataGcLock.Lock()
cc.syncToken = col.SyncToken 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) { func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
parents = []EntryReference{} parents = []EntryReference{}
if cc.cacheInit { cc.cacheMutex.sharedDataGcLock.RLock()
cacheInit := cc.cacheInit
cc.cacheMutex.sharedDataGcLock.RUnlock()
if cacheInit {
cc.cacheMutex.parentMapGcLock.RLock() cc.cacheMutex.parentMapGcLock.RLock()
defer cc.cacheMutex.parentMapGcLock.RUnlock() defer cc.cacheMutex.parentMapGcLock.RUnlock()
if len(contentType) != 0 { if len(contentType) != 0 {

View File

@ -14,7 +14,7 @@ import (
"github.com/foomo/gocontentful/erm" "github.com/foomo/gocontentful/erm"
) )
var VERSION = "v1.0.25" var VERSION = "v1.0.26"
type contentfulRc struct { type contentfulRc struct {
ManagementToken string `json:"managementToken"` ManagementToken string `json:"managementToken"`

View File

@ -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 package testapi
import ( import (

View File

@ -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 package testapi
import "github.com/foomo/contentful" import "github.com/foomo/contentful"

View File

@ -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 package testapi
import ( import (
@ -355,7 +355,10 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
if cc == nil || cc.Client == nil { if cc == nil || cc.Client == nil {
return "", errors.New("GetContentTypeOfID: No client available") 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 okVo := false
cc.cacheMutex.brandGcLock.Lock() cc.cacheMutex.brandGcLock.Lock()
@ -631,8 +634,8 @@ func (cc *ContentfulClient) SetOfflineFallback(filename string) error {
} }
func (cc *ContentfulClient) SetSyncMode(mode bool) error { func (cc *ContentfulClient) SetSyncMode(mode bool) error {
cc.cacheMutex.sharedDataGcLock.RLock() cc.cacheMutex.sharedDataGcLock.Lock()
defer cc.cacheMutex.sharedDataGcLock.RUnlock() defer cc.cacheMutex.sharedDataGcLock.Unlock()
if cc.offline { if cc.offline {
return errors.New("SetSyncMode: client is set offline, can't enable sync") 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() { func (cc *ContentfulClient) ResetSync() {
cc.cacheMutex.sharedDataGcLock.Lock()
defer cc.cacheMutex.sharedDataGcLock.Unlock()
cc.syncToken = "" 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.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateCanceled)
} }
} }
cc.cacheMutex.sharedDataGcLock.Lock()
cc.cacheInit = true cc.cacheInit = true
cc.cacheMutex.sharedDataGcLock.Unlock()
if cc.logFn != nil && cc.logLevel <= LogInfo { if cc.logFn != nil && cc.logLevel <= LogInfo {
cc.logFn(map[string]interface{}{"task": "UpdateCache"}, LogInfo, InfoCacheUpdateDone) 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) cc.logFn(map[string]interface{}{"task": "syncCache"}, LogInfo, InfoCacheUpdateQueued)
} }
for { for {
cc.cacheMutex.sharedDataGcLock.RLock()
col := cc.Client.Entries.Sync( col := cc.Client.Entries.Sync(
cc.SpaceID, cc.SpaceID,
cc.syncToken == "", cc.syncToken == "",
cc.syncToken, cc.syncToken,
) )
cc.cacheMutex.sharedDataGcLock.RUnlock()
col.GetAll() col.GetAll()
cc.cacheMutex.sharedDataGcLock.Lock() cc.cacheMutex.sharedDataGcLock.Lock()
cc.syncToken = col.SyncToken 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) { func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
parents = []EntryReference{} parents = []EntryReference{}
if cc.cacheInit { cc.cacheMutex.sharedDataGcLock.RLock()
cacheInit := cc.cacheInit
cc.cacheMutex.sharedDataGcLock.RUnlock()
if cacheInit {
cc.cacheMutex.parentMapGcLock.RLock() cc.cacheMutex.parentMapGcLock.RLock()
defer cc.cacheMutex.parentMapGcLock.RUnlock() defer cc.cacheMutex.parentMapGcLock.RUnlock()
if len(contentType) != 0 { if len(contentType) != 0 {

View File

@ -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 package testapi
import ( import (

View File

@ -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 package testapi
import ( import (

View File

@ -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 package testapi
import ( import (