Skip to content

Commit

Permalink
test service: replace plain Go map with sync.Map
Browse files Browse the repository at this point in the history
To reduce contention on the map, we'll use:

https://golang.org/pkg/sync/#Map
  • Loading branch information
panchoh committed Mar 4, 2019
1 parent 7735678 commit 5b26fe5
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions tests/e2e/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,19 @@ func (a *alertStore) GetAll() []string {
}

type snapStore struct {
sync.Mutex
d map[uint64]*protocol.SignedSnapshot
d *sync.Map
}

func (s *snapStore) Put(b *protocol.BatchSnapshots) {
s.Lock()
defer s.Unlock()

for _, snap := range b.Snapshots {
s.d[snap.Snapshot.Version] = snap
s.d.Store(snap.Snapshot.Version, snap)
}
}

func (s *snapStore) Get(version uint64) (v *protocol.SignedSnapshot, ok bool) {
s.Lock()
defer s.Unlock()
v, ok = s.d[version]
var tmpV interface{}
tmpV, ok = s.d.Load(version)
v = tmpV.(*protocol.SignedSnapshot)
return v, ok
}

Expand Down Expand Up @@ -276,7 +272,7 @@ func NewService() *Service {
var snaps snapStore
var alerts alertStore
var stats statStore
snaps.d = make(map[uint64]*protocol.SignedSnapshot)
snaps.d = &sync.Map{}
stats.batch = make(map[string][]int)
alerts.d = make([]string, 0)

Expand Down

0 comments on commit 5b26fe5

Please sign in to comment.