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 {
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 {

View File

@ -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"`

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
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
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
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 {

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
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
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
import (