Skip to content

Commit

Permalink
fix: custom fields cache concurrent writes
Browse files Browse the repository at this point in the history
  • Loading branch information
Multiply committed Feb 6, 2025
1 parent 85d81c7 commit d8faf93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20250206-113217.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Custom fields cache concurrent writes
time: 2025-02-06T11:32:17.345811+01:00
8 changes: 8 additions & 0 deletions commercetools/custom_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"strconv"
"strings"
"sync"
"time"

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

var cacheTypes map[string]*platform.Type
var cacheTypesLock sync.Mutex

func CustomFieldSchema() *schema.Schema {
return &schema.Schema{
Expand Down Expand Up @@ -257,6 +259,7 @@ func getTypeResourceFromResourceData(ctx context.Context, client *platform.ByPro
// field. The type_id is cached to minimize API calls when multiple resource
// use the same type
func GetTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBuilder, typeId string) (*platform.Type, error) {
cacheTypesLock.Lock()
if cacheTypes == nil {
cacheTypes = make(map[string]*platform.Type)
}
Expand All @@ -266,9 +269,14 @@ func GetTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBu
}
return t, nil
}
cacheTypesLock.Unlock()

t, err := client.Types().WithId(typeId).Get().Execute(ctx)

cacheTypesLock.Lock()
cacheTypes[typeId] = t
cacheTypesLock.Unlock()

return t, err
}

Expand Down

0 comments on commit d8faf93

Please sign in to comment.