Skip to content

Commit 9107c50

Browse files
committed
Update README about Timeout and Context
1 parent 1551ff6 commit 9107c50

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,7 @@ request := NewRequest().
183183
WithResponse(true)
184184
```
185185

186-
By default a `context.Background()` will be added and `WithResponse()` will be
187-
set to `true`.
188-
189-
`WithTimeout` will also set the `Expiration` on the publishing since there is no
190-
point of handling the message after the timeout has expired. Setting
191-
`WithResponse(false)` will ensure that no `Expiration` is set.
186+
By default the request will have `Reply` set to true to expect a reply to the message.
192187

193188
The `Request` also implements the `io.Writer` interface which makes it possible
194189
to use directly like that.
@@ -202,6 +197,37 @@ if err != nil {
202197
}
203198
```
204199

200+
#### Timeout
201+
202+
You can set a timeout on requests with `Client.WithTimeout(duration)`,
203+
`Request.WithTimeout(duration)` or having a context with a deadline set. Timeouts set on
204+
the request will take precedence.
205+
206+
When a request expects a reply (`Request.Reply`), the outgoing message
207+
is also assigned an `Expiration` corresponding to the request’s deadline. This
208+
ensures the message won’t remain in the queue after the client has stopped
209+
waiting. If you set a deadline on the context, and that deadline is shorter
210+
than the timeout, the `Expiration` will be set to that deadline.
211+
212+
#### Context
213+
214+
By default a `context.Background()` will be set on the request. You should set
215+
your own context so that any cancellation is propagated. Cancelling the context
216+
will cancel the request and `Client.Send` will unblock and return with the
217+
error from the `Cause` set by the cancellation. It will not wait for
218+
confirmations or responses.
219+
220+
```go
221+
ctx, cancel := context.WithCancelCause(r.Context, errors.New("my error"))
222+
cancel()
223+
224+
request := NewRequest().
225+
WithContext(ctx)
226+
227+
_, err := client.Send(NewRequest().WithContext(ctx))
228+
fmt.Println(err) // my error
229+
```
230+
205231
### Sender
206232

207233
The client invokes a default `SendFunc` while calling `Send()` where all the

0 commit comments

Comments
 (0)