(ctx context.Context, subj string, hdr, data []byte)
| 38 | } |
| 39 | |
| 40 | func (nc *Conn) requestWithContext(ctx context.Context, subj string, hdr, data []byte) (*Msg, error) { |
| 41 | if ctx == nil { |
| 42 | return nil, ErrInvalidContext |
| 43 | } |
| 44 | if nc == nil { |
| 45 | return nil, ErrInvalidConnection |
| 46 | } |
| 47 | // Check whether the context is done already before making |
| 48 | // the request. |
| 49 | if ctx.Err() != nil { |
| 50 | return nil, ctx.Err() |
| 51 | } |
| 52 | |
| 53 | var m *Msg |
| 54 | var err error |
| 55 | |
| 56 | // If user wants the old style. |
| 57 | if nc.useOldRequestStyle() { |
| 58 | m, err = nc.oldRequestWithContext(ctx, subj, hdr, data) |
| 59 | } else { |
| 60 | mch, token, err := nc.createNewRequestAndSend(subj, hdr, data) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | |
| 65 | var ok bool |
| 66 | |
| 67 | select { |
| 68 | case m, ok = <-mch: |
| 69 | if !ok { |
| 70 | return nil, ErrConnectionClosed |
| 71 | } |
| 72 | case <-ctx.Done(): |
| 73 | nc.mu.Lock() |
| 74 | delete(nc.respMap, token) |
| 75 | nc.mu.Unlock() |
| 76 | return nil, ctx.Err() |
| 77 | } |
| 78 | } |
| 79 | // Check for no responder status. |
| 80 | if err == nil && len(m.Data) == 0 && m.Header.Get(statusHdr) == noResponders { |
| 81 | m, err = nil, ErrNoResponders |
| 82 | } |
| 83 | return m, err |
| 84 | } |
| 85 | |
| 86 | // oldRequestWithContext utilizes inbox and subscription per request. |
| 87 | func (nc *Conn) oldRequestWithContext(ctx context.Context, subj string, hdr, data []byte) (*Msg, error) { |
no test coverage detected