(req *pullRequest)
| 897 | } |
| 898 | |
| 899 | func (p *pullConsumer) fetch(req *pullRequest) (MessageBatch, error) { |
| 900 | res := &fetchResult{ |
| 901 | msgs: make(chan Msg, req.Batch), |
| 902 | } |
| 903 | msgs := make(chan *nats.Msg, 2*req.Batch) |
| 904 | subject := p.js.apiSubject(fmt.Sprintf(apiRequestNextT, p.stream, p.name)) |
| 905 | |
| 906 | sub := &pullSubscription{ |
| 907 | consumer: p, |
| 908 | done: make(chan struct{}, 1), |
| 909 | msgs: msgs, |
| 910 | errs: make(chan error, 10), |
| 911 | } |
| 912 | inbox := p.js.conn.NewInbox() |
| 913 | var err error |
| 914 | sub.subscription, err = p.js.conn.ChanSubscribe(inbox, sub.msgs) |
| 915 | if err != nil { |
| 916 | return nil, err |
| 917 | } |
| 918 | req.PinID = p.getPinID() |
| 919 | if err := sub.pull(req, subject); err != nil { |
| 920 | return nil, err |
| 921 | } |
| 922 | |
| 923 | var receivedMsgs, receivedBytes int |
| 924 | hbTimer := sub.scheduleHeartbeatCheck(req.Heartbeat) |
| 925 | |
| 926 | // Use context if provided |
| 927 | var ctxDone <-chan struct{} |
| 928 | if req.ctx != nil { |
| 929 | ctxDone = req.ctx.Done() |
| 930 | } |
| 931 | |
| 932 | go func(res *fetchResult) { |
| 933 | defer sub.subscription.Unsubscribe() |
| 934 | defer close(res.msgs) |
| 935 | for { |
| 936 | select { |
| 937 | case msg := <-msgs: |
| 938 | res.Lock() |
| 939 | if hbTimer != nil { |
| 940 | hbTimer.Reset(2 * req.Heartbeat) |
| 941 | } |
| 942 | userMsg, err := checkMsg(msg) |
| 943 | if err != nil { |
| 944 | errNotTimeoutOrNoMsgs := !errors.Is(err, nats.ErrTimeout) && !errors.Is(err, ErrNoMessages) |
| 945 | if errNotTimeoutOrNoMsgs && !errors.Is(err, ErrMaxBytesExceeded) { |
| 946 | res.err = err |
| 947 | } |
| 948 | if errors.Is(err, ErrPinIDMismatch) { |
| 949 | p.setPinID("") |
| 950 | } |
| 951 | res.done = true |
| 952 | res.Unlock() |
| 953 | return |
| 954 | } |
| 955 | if !userMsg { |
| 956 | res.Unlock() |
no test coverage detected