@@ -67,7 +67,7 @@ type Table interface {
67
67
type InMemTable struct {
68
68
self id.Signatory
69
69
70
- sortedMu * sync.Mutex
70
+ sortedMu * sync.RWMutex
71
71
sorted []id.Signatory
72
72
73
73
addrsBySignatoryMu * sync.Mutex
@@ -86,7 +86,7 @@ func NewInMemTable(self id.Signatory) *InMemTable {
86
86
return & InMemTable {
87
87
self : self ,
88
88
89
- sortedMu : new (sync.Mutex ),
89
+ sortedMu : new (sync.RWMutex ),
90
90
sorted : []id.Signatory {},
91
91
92
92
addrsBySignatoryMu : new (sync.Mutex ),
@@ -166,8 +166,8 @@ func (table *InMemTable) PeerAddress(peerID id.Signatory) (wire.Address, bool) {
166
166
167
167
// Peers returns the n closest peer IDs.
168
168
func (table * InMemTable ) Peers (n int ) []id.Signatory {
169
- table .sortedMu .Lock ()
170
- defer table .sortedMu .Unlock ()
169
+ table .sortedMu .RLock ()
170
+ defer table .sortedMu .RUnlock ()
171
171
172
172
if n <= 0 {
173
173
// For values of n that are less than, or equal to, zero, return an
@@ -183,9 +183,9 @@ func (table *InMemTable) Peers(n int) []id.Signatory {
183
183
184
184
// RandomPeers returns n random peer IDs
185
185
func (table * InMemTable ) RandomPeers (n int ) []id.Signatory {
186
- table .sortedMu .Lock ()
186
+ table .sortedMu .RLock ()
187
+ defer table .sortedMu .RUnlock ()
187
188
m := len (table .sorted )
188
- table .sortedMu .Unlock ()
189
189
190
190
if n <= 0 {
191
191
// For values of n that are less than, or equal to, zero, return an
@@ -195,8 +195,6 @@ func (table *InMemTable) RandomPeers(n int) []id.Signatory {
195
195
}
196
196
if n >= m {
197
197
sigs := make ([]id.Signatory , m )
198
- table .sortedMu .Lock ()
199
- defer table .sortedMu .Unlock ()
200
198
copy (sigs , table .sorted )
201
199
return sigs
202
200
}
@@ -208,8 +206,6 @@ func (table *InMemTable) RandomPeers(n int) []id.Signatory {
208
206
if m <= 10000 || n >= m / 50.0 {
209
207
shuffled := make ([]id.Signatory , n )
210
208
indexPerm := rand .Perm (m )
211
- table .sortedMu .Lock ()
212
- defer table .sortedMu .Unlock ()
213
209
for i := 0 ; i < n ; i ++ {
214
210
shuffled [i ] = table.sorted [indexPerm [i ]]
215
211
}
@@ -219,8 +215,6 @@ func (table *InMemTable) RandomPeers(n int) []id.Signatory {
219
215
// Otherwise, use Floyd's sampling algorithm to select n random elements
220
216
set := make (map [int ]struct {}, n )
221
217
randomSelection := make ([]id.Signatory , 0 , n )
222
- table .sortedMu .Lock ()
223
- defer table .sortedMu .Unlock ()
224
218
for i := m - n ; i < m ; i ++ {
225
219
index := table .randObj .Intn (i )
226
220
if _ , ok := set [index ]; ! ok {
0 commit comments