diff options
| -rw-r--r-- | app/db/in_memory_repository.go | 6 |
1 files changed, 5 insertions, 1 deletions
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() |
