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

Method FlushWithContext

context.go:174–215  ·  view source on GitHub ↗

FlushWithContext will allow a context to control the duration of a Flush() call. This context should be non-nil and should have a deadline set. We will return an error if none is present.

(ctx context.Context)

Source from the content-addressed store, hash-verified

172// of a Flush() call. This context should be non-nil and should
173// have a deadline set. We will return an error if none is present.
174func (nc *Conn) FlushWithContext(ctx context.Context) error {
175 if nc == nil {
176 return ErrInvalidConnection
177 }
178 if ctx == nil {
179 return ErrInvalidContext
180 }
181 _, ok := ctx.Deadline()
182 if !ok {
183 return ErrNoDeadlineContext
184 }
185
186 nc.mu.Lock()
187 if nc.isClosed() {
188 nc.mu.Unlock()
189 return ErrConnectionClosed
190 }
191 // Create a buffered channel to prevent chan send to block
192 // in processPong()
193 ch := make(chan struct{}, 1)
194 nc.sendPing(ch)
195 nc.mu.Unlock()
196
197 var err error
198
199 select {
200 case _, ok := <-ch:
201 if !ok {
202 err = ErrConnectionClosed
203 } else {
204 close(ch)
205 }
206 case <-ctx.Done():
207 err = ctx.Err()
208 }
209
210 if err != nil {
211 nc.removeFlushEntry(ch)
212 }
213
214 return err
215}
216
217// RequestWithContext will create an Inbox and perform a Request
218// using the provided cancellation context with the Inbox reply

Callers 1

TestFlushWithContextFunction · 0.80

Calls 5

isClosedMethod · 0.95
sendPingMethod · 0.95
removeFlushEntryMethod · 0.95
DoneMethod · 0.65
ErrMethod · 0.65

Tested by 1

TestFlushWithContextFunction · 0.64