Skip to content

Commit a0c6649

Browse files
committed
les/flowcontrol: fixed locking scheme
1 parent ca73dea commit a0c6649

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

les/flowcontrol/control.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type ServerNode struct {
9999
params *ServerParams
100100
sumCost uint64 // sum of req costs sent to this server
101101
pending map[uint64]uint64 // value = sumCost after sending the given req
102-
lock sync.Mutex
102+
lock sync.RWMutex
103103
}
104104

105105
func NewServerNode(params *ServerParams) *ServerNode {
@@ -135,8 +135,8 @@ func (peer *ServerNode) canSend(maxCost uint64) uint64 {
135135
}
136136

137137
func (peer *ServerNode) CanSend(maxCost uint64) uint64 {
138-
peer.lock.Lock()
139-
defer peer.lock.Unlock()
138+
peer.lock.RLock()
139+
defer peer.lock.RUnlock()
140140

141141
return peer.canSend(maxCost)
142142
}
@@ -148,7 +148,10 @@ func (peer *ServerNode) SendRequest(reqID, maxCost uint64) {
148148

149149
peer.recalcBLE(getTime())
150150
for peer.bufEstimate < maxCost {
151-
time.Sleep(time.Duration(peer.canSend(maxCost)))
151+
wait := time.Duration(peer.canSend(maxCost))
152+
peer.lock.Unlock()
153+
time.Sleep(wait)
154+
peer.lock.Lock()
152155
peer.recalcBLE(getTime())
153156
}
154157
peer.bufEstimate -= maxCost

0 commit comments

Comments
 (0)