From b0b2f28d7c4df62e23879c241c4419135ef6f365 Mon Sep 17 00:00:00 2001 From: Duncan Dam <59436522+Duncan-Ultra@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:37:52 +0700 Subject: [PATCH] [BLOCK-2347] fix concurrent map writes (#48) --- dgraphql/resolvers/trxcache.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dgraphql/resolvers/trxcache.go b/dgraphql/resolvers/trxcache.go index 94a06a31..41790e5b 100644 --- a/dgraphql/resolvers/trxcache.go +++ b/dgraphql/resolvers/trxcache.go @@ -4,12 +4,14 @@ package resolvers import ( "container/list" + "sync" ) type TrxCache struct { capacity int data map[string]*list.Element order list.List + mu sync.RWMutex } func NewTrxCache(capacity int) *TrxCache { @@ -21,6 +23,9 @@ func NewTrxCache(capacity int) *TrxCache { } func (c *TrxCache) Put(key string) { + c.mu.Lock() + defer c.mu.Unlock() + if ele, exists := c.data[key]; exists { // If key already exists, move it to the front c.order.MoveToFront(ele) @@ -38,6 +43,9 @@ func (c *TrxCache) Put(key string) { } func (c *TrxCache) Exists(key string) bool { + c.mu.RLock() + defer c.mu.RUnlock() + _, exists := c.data[key] return exists }