Skip to content

Commit

Permalink
datastore: Using RWMutex (also for reading/writing responses)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 6, 2024
1 parent 55ed892 commit 55e201b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/lib/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type Datastore struct {
storage map[string]any // custom storage
responseJson []string // store the responses
logDatastore bool
lock *sync.Mutex
lock *sync.RWMutex
}

func NewStore(logDatastore bool) *Datastore {
ds := Datastore{}
ds.storage = make(map[string]any, 0)
ds.responseJson = make([]string, 0)
ds.logDatastore = logDatastore
ds.lock = &sync.Mutex{}
ds.lock = &sync.RWMutex{}
return &ds
}

Expand Down Expand Up @@ -84,11 +84,17 @@ func (ds *Datastore) Delete(k string) {

// We store the response
func (ds *Datastore) UpdateLastResponse(s string) {
ds.lock.Lock()
defer ds.lock.Unlock()

ds.responseJson[len(ds.responseJson)-1] = s
}

// We store the response
func (ds *Datastore) AppendResponse(s string) {
ds.lock.Lock()
defer ds.lock.Unlock()

ds.responseJson = append(ds.responseJson, s)
}

Expand Down Expand Up @@ -167,6 +173,9 @@ func (ds *Datastore) Set(index string, value any) error {
}

func (ds Datastore) Get(index string) (res any, err error) {
ds.lock.RLock()
defer ds.lock.RUnlock()

// strings are evalulated as int, so
// that we can support "-<int>" notations

Expand Down

0 comments on commit 55e201b

Please sign in to comment.