Skip to content

Commit

Permalink
kvstore: Local kvstore requires locking
Browse files Browse the repository at this point in the history
Fixes parallel map access panic

Signed-off-by: Thomas Graf <thomas@cilium.io>
  • Loading branch information
tgraf committed Mar 19, 2017
1 parent 7ab155b commit de88dda
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/kvstore/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/cilium/cilium/common"
Expand All @@ -29,6 +30,7 @@ import (

type LocalClient struct {
store map[string]string
lock sync.RWMutex
}

type LocalLocker struct {
Expand All @@ -47,6 +49,9 @@ func (l *LocalLocker) Unlock() error {
}

func (l *LocalClient) GetValue(k string) (json.RawMessage, error) {
l.lock.RLock()
defer l.lock.RUnlock()

if v, ok := l.store[k]; ok {
return json.RawMessage(v), nil
}
Expand All @@ -58,7 +63,10 @@ func (l *LocalClient) SetValue(k string, v interface{}) error {
if err != nil {
return err
}

l.lock.Lock()
l.store[k] = string(vByte)
l.lock.Unlock()

return nil
}
Expand Down

0 comments on commit de88dda

Please sign in to comment.