MCPcopy
hub / github.com/nats-io/nats.go / MsgsTimeout

Method MsgsTimeout

nats_iter.go:55–72  ·  view source on GitHub ↗

MsgsTimeout returns an iter.Seq2[*Msg, error] that can be used to iterate over messages. It can only be used with a subscription that has been created with SubscribeSync or QueueSubscribeSync, otherwise it will return an error on the first iteration. The iterator will block until a message is avail

(timeout time.Duration)

Source from the content-addressed store, hash-verified

53// reached. If the timeout is reached, the iterator will return nats.ErrTimeout
54// but it will not be closed.
55func (sub *Subscription) MsgsTimeout(timeout time.Duration) iter.Seq2[*Msg, error] {
56 return func(yield func(*Msg, error) bool) {
57 for {
58 msg, err := sub.NextMsg(timeout)
59 if err != nil {
60 if !yield(nil, err) {
61 return
62 }
63 if !errors.Is(err, ErrTimeout) {
64 return
65 }
66 }
67 if !yield(msg, nil) {
68 return
69 }
70 }
71 }
72}

Callers 2

TestSubscribeIteratorFunction · 0.80

Calls 2

NextMsgMethod · 0.95
IsMethod · 0.45

Tested by 2

TestSubscribeIteratorFunction · 0.64