-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-supervisor: fix db locking, fix crossdb usage
- Loading branch information
1 parent
a8b2276
commit f6c00d4
Showing
7 changed files
with
210 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package locks | ||
|
||
import "sync" | ||
|
||
// RWMap is a simple wrapper around a map, with global Read-Write protection. | ||
// For many concurrent reads/writes a sync.Map may be more performant, | ||
// although it does not utilize Go generics. | ||
// The RWMap does not have to be initialized, | ||
// it is immediately ready for reads/writes. | ||
type RWMap[K comparable, V any] struct { | ||
inner map[K]V | ||
mu sync.RWMutex | ||
} | ||
|
||
func (m *RWMap[K, V]) Has(key K) (ok bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
_, ok = m.inner[key] | ||
return | ||
} | ||
|
||
func (m *RWMap[K, V]) Get(key K) (value V, ok bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
value, ok = m.inner[key] | ||
return | ||
} | ||
|
||
func (m *RWMap[K, V]) Set(key K, value V) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if m.inner == nil { | ||
m.inner = make(map[K]V) | ||
} | ||
m.inner[key] = value | ||
} | ||
|
||
// Range calls f sequentially for each key and value present in the map. | ||
// If f returns false, range stops the iteration. | ||
func (m *RWMap[K, V]) Range(f func(key K, value V) bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
for k, v := range m.inner { | ||
if !f(k, v) { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package locks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRWMap(t *testing.T) { | ||
m := &RWMap[uint64, int64]{} | ||
|
||
// get on new map | ||
v, ok := m.Get(123) | ||
require.False(t, ok) | ||
require.Equal(t, int64(0), v) | ||
|
||
// set a value | ||
m.Set(123, 42) | ||
v, ok = m.Get(123) | ||
require.True(t, ok) | ||
require.Equal(t, int64(42), v) | ||
|
||
// overwrite a value | ||
m.Set(123, -42) | ||
v, ok = m.Get(123) | ||
require.True(t, ok) | ||
require.Equal(t, int64(-42), v) | ||
|
||
// add a value | ||
m.Set(10, 100) | ||
|
||
// range over values | ||
got := make(map[uint64]int64) | ||
m.Range(func(key uint64, value int64) bool { | ||
if _, ok := got[key]; ok { | ||
panic("duplicate") | ||
} | ||
got[key] = value | ||
return true | ||
}) | ||
require.Len(t, got, 2) | ||
require.Equal(t, int64(100), got[uint64(10)]) | ||
require.Equal(t, int64(-42), got[uint64(123)]) | ||
|
||
// range and stop early | ||
clear(got) | ||
m.Range(func(key uint64, value int64) bool { | ||
got[key] = value | ||
return false | ||
}) | ||
require.Len(t, got, 1, "stop early") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package locks | ||
|
||
import "sync" | ||
|
||
// RWValue is a simple container struct, to deconflict reads/writes of the value, | ||
// without locking up a bigger structure in the caller. | ||
// It exposes the underlying RWLock and Value for direct access where needed. | ||
type RWValue[E any] struct { | ||
sync.RWMutex | ||
Value E | ||
} | ||
|
||
func (c *RWValue[E]) Get() (out E) { | ||
c.RLock() | ||
defer c.RUnlock() | ||
out = c.Value | ||
return | ||
} | ||
|
||
func (c *RWValue[E]) Set(v E) { | ||
c.Lock() | ||
defer c.Unlock() | ||
c.Value = v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package locks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRWValue(t *testing.T) { | ||
v := &RWValue[uint64]{} | ||
require.Equal(t, uint64(0), v.Get()) | ||
v.Set(123) | ||
require.Equal(t, uint64(123), v.Get()) | ||
v.Set(42) | ||
require.Equal(t, uint64(42), v.Get()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.