Skip to content

Commit dad4135

Browse files
committed
fix: custom fields cache concurrent writes
1 parent 85d81c7 commit dad4135

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

commercetools/custom_fields.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"fmt"
77
"strconv"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"github.com/labd/commercetools-go-sdk/platform"
1314
)
1415

1516
var cacheTypes map[string]*platform.Type
17+
var cacheTypesLock sync.Mutex
1618

1719
func CustomFieldSchema() *schema.Schema {
1820
return &schema.Schema{
@@ -257,6 +259,7 @@ func getTypeResourceFromResourceData(ctx context.Context, client *platform.ByPro
257259
// field. The type_id is cached to minimize API calls when multiple resource
258260
// use the same type
259261
func GetTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBuilder, typeId string) (*platform.Type, error) {
262+
cacheTypesLock.Lock()
260263
if cacheTypes == nil {
261264
cacheTypes = make(map[string]*platform.Type)
262265
}
@@ -266,9 +269,14 @@ func GetTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBu
266269
}
267270
return t, nil
268271
}
272+
cacheTypesLock.Unlock()
269273

270274
t, err := client.Types().WithId(typeId).Get().Execute(ctx)
275+
276+
cacheTypesLock.Lock()
271277
cacheTypes[typeId] = t
278+
cacheTypesLock.Unlock()
279+
272280
return t, err
273281
}
274282

0 commit comments

Comments
 (0)