mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-02 12:53:09 +00:00
fix: Select ctx.Done() when preprocessing to avoid blocking on cancel (#635)
Close #636 The PR updates the send logic to use a select with ctx.Done() and t.p.ctx.Done(), ensuring the operation terminates gracefully. A test case reproducing the issue is included in the PR for verification.
This commit is contained in:
parent
ee9c8434f9
commit
ab876fc71c
8
topic.go
8
topic.go
@ -349,8 +349,14 @@ func (t *Topic) validate(ctx context.Context, data []byte, opts ...PubOpt) (*Mes
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local}
|
msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local}
|
||||||
t.p.eval <- func() {
|
select {
|
||||||
|
case t.p.eval <- func() {
|
||||||
t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg})
|
t.p.rt.Preprocess(t.p.host.ID(), []*Message{msg})
|
||||||
|
}:
|
||||||
|
case <-t.p.ctx.Done():
|
||||||
|
return nil, t.p.ctx.Err()
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
}
|
}
|
||||||
err := t.p.val.ValidateLocal(msg)
|
err := t.p.val.ValidateLocal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -951,6 +951,23 @@ func TestTopicPublishWithKeyInvalidParameters(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTopicPublishWithContextCanceled(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
const topic = "foobar"
|
||||||
|
const numHosts = 5
|
||||||
|
|
||||||
|
hosts := getDefaultHosts(t, numHosts)
|
||||||
|
topics := getTopics(getPubsubs(ctx, hosts), topic)
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
err := topics[0].Publish(ctx, []byte("buff"))
|
||||||
|
if err != context.Canceled {
|
||||||
|
t.Fatal("error should have been of type context.Canceled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTopicRelayPublishWithKey(t *testing.T) {
|
func TestTopicRelayPublishWithKey(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user