From fb1771de02c3ffffcb69c577696d0190863a9a6f Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 13 Jun 2025 14:18:09 -0600 Subject: fix: take a lock before updating the list of items --- app/db/in_memory_repository.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/db/in_memory_repository.go b/app/db/in_memory_repository.go index ba9ebad..340181f 100644 --- a/app/db/in_memory_repository.go +++ b/app/db/in_memory_repository.go @@ -3,6 +3,7 @@ package db import ( "context" "sort" + "sync" "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" @@ -10,12 +11,14 @@ import ( ) type inMemoryRepository[T domain.Entity] struct { + mu sync.RWMutex items []T } func NewRepository[T domain.Entity]() domain.Repository[T] { return &inMemoryRepository[T]{ items: []T{}, + mu: sync.RWMutex{}, } } @@ -37,7 +40,8 @@ func (r *inMemoryRepository[T]) Save(ctx context.Context, item T) error { if item.GetID() == "" { item.SetID(domain.ID(pls.GenerateULID())) } - + r.mu.Lock() + defer r.mu.Unlock() r.items = append(r.items, item) sort.Slice(r.items, func(i, j int) bool { return r.items[i].GetID() > r.items[j].GetID() -- cgit v1.2.3