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)
| 53 | // reached. If the timeout is reached, the iterator will return nats.ErrTimeout |
| 54 | // but it will not be closed. |
| 55 | func (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 | } |