mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-07 15:23:08 +00:00
Prevent multiple invocations of relay cancel function
This commit is contained in:
parent
9a0d2f5948
commit
9a0bd7ad56
12
pubsub.go
12
pubsub.go
@ -676,12 +676,22 @@ func (p *PubSub) handleAddRelay(req *addRelayReq) {
|
|||||||
p.rt.Join(topic)
|
p.rt.Join(topic)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.resp <- func() {
|
// flag used to prevent calling cancel function multiple times
|
||||||
|
isCancelled := false
|
||||||
|
|
||||||
|
relayCancelFunc := func() {
|
||||||
|
if isCancelled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case p.rmRelay <- topic:
|
case p.rmRelay <- topic:
|
||||||
|
isCancelled = true
|
||||||
case <-p.ctx.Done():
|
case <-p.ctx.Done():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.resp <- relayCancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleRemoveRelay removes one relay reference from bookkeeping.
|
// handleRemoveRelay removes one relay reference from bookkeeping.
|
||||||
|
|||||||
@ -629,7 +629,20 @@ func TestTopicRelayReuse(t *testing.T) {
|
|||||||
t.Fatal("incorrect number of relays")
|
t.Fatal("incorrect number of relays")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only the first invocation should take effect
|
||||||
relay1Cancel()
|
relay1Cancel()
|
||||||
|
relay1Cancel()
|
||||||
|
relay1Cancel()
|
||||||
|
|
||||||
|
pubsubs[0].eval <- func() {
|
||||||
|
res <- pubsubs[0].myRelays[topic] == 2
|
||||||
|
}
|
||||||
|
|
||||||
|
isCorrectNumber = <-res
|
||||||
|
if !isCorrectNumber {
|
||||||
|
t.Fatal("incorrect number of relays")
|
||||||
|
}
|
||||||
|
|
||||||
relay2Cancel()
|
relay2Cancel()
|
||||||
relay3Cancel()
|
relay3Cancel()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user