Skip to content

Commit 3071c69

Browse files
Improve timeout documentation with dedicated section
Co-authored-by: alexeyzimarev <[email protected]>
1 parent 7968586 commit 3071c69

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

docs/docs/advanced/configuration.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ RestSharp allows configuring `RestClient` using client options, as mentioned at
172172
| `RemoteCertificateValidationCallback` | Custom function to validate the server certificate. Normally, it's used when the server uses a certificate that isn't trusted by default. |
173173
| `BaseHost` | Value for the `Host` header sent with each request. |
174174
| `CookieContainer` | Custom cookie container that will be shared among all calls made by the client. Normally not required as RestSharp handles cookies without using a client-level cookie container. |
175-
| `Timeout` | Client-level timeout as `TimeSpan`. Used when the request timeout is not specified using `RestRequest.Timeout`. If not set, the default timeout is 100 seconds. Set to `Timeout.InfiniteTimeSpan` (or `TimeSpan.FromMilliseconds(-1)`) for no timeout. Setting to `TimeSpan.Zero` will cancel the request immediately. Negative values other than -1 millisecond will throw an exception. |
175+
| `Timeout` | Client-level timeout as `TimeSpan`. Default is 100 seconds. See [Configuring Timeouts](#configuring-timeouts) for details on timeout behavior. |
176176
| `Encoding` | Default request encoding. Override it only if you don't use UTF-8. |
177177
| `ThrowOnDeserializationError` | Forces the client to throw if it fails to deserialize the response. Remember that not all deserialization issues forces the serializer to throw. Default is `false`, so the client will return a `RestResponse` with deserialization exception details. Only relevant for `Execute...` functions. |
178178
| `FailOnDeserializationError` | When set to `true`, if the client fails to deserialize the response, the response object will have status `Failed`, although the HTTP calls might have been successful. Default is `true`. |
@@ -213,7 +213,7 @@ Client options apply to all requests made by the client. Sometimes, you want to
213213
| `Authenticator` | Overrides the client-level authenticator. |
214214
| `Files` | Collection of file parameters, read-only. Use `AddFile` for adding files to the request. |
215215
| `Method` | Request HTTP method, default is `GET`. Only needed when using `Execute` or `ExecuteAsync` as other functions like `ExecutePostAsync` will override the request method. |
216-
| `Timeout` | Overrides the client-level timeout. If not set, uses the client timeout or the default of 100 seconds. Set to `Timeout.InfiniteTimeSpan` (or `TimeSpan.FromMilliseconds(-1)`) for no timeout. Setting to `TimeSpan.Zero` will cancel the request immediately. Negative values other than -1 millisecond will throw an exception. |
216+
| `Timeout` | Request-level timeout override. See [Configuring Timeouts](#configuring-timeouts) for details on timeout behavior. |
217217
| `Resource` | Resource part of the remote endpoint URL. For example, when using the client-level base URL `https://localhost:5000/api` and `Resource` set to `weather`, the request will be sent to `https://localhost:5000/api/weather`. It can container resource placeholders to be used in combination with `AddUrlSegment` |
218218
| `RequestFormat` | Identifies the request as JSON, XML, binary, or none. Rarely used because the client will set the request format based on the body type if functions like `AddJsonBody` or `AddXmlBody` are used. |
219219
| `RootElement` | Used by the default deserializers to determine where to start deserializing from. Only supported for XML responses. Does not apply to requests. |
@@ -228,3 +228,46 @@ Client options apply to all requests made by the client. Sometimes, you want to
228228
| `Interceptors` | Allows adding interceptors to the request. Both client-level and request-level interceptors will be called. |
229229

230230
The table below contains all configuration properties of `RestRequest`. To learn more about adding request parameters, check the [usage page](../usage/request.md) page about creating requests with parameters.
231+
232+
## Configuring Timeouts
233+
234+
RestSharp provides flexible timeout configuration at both the client and request levels. The timeout determines how long RestSharp will wait for a response before canceling the request.
235+
236+
### Timeout Resolution
237+
238+
When making a request, RestSharp uses the following priority order to determine the timeout:
239+
1. `RestRequest.Timeout` (if set)
240+
2. `RestClientOptions.Timeout` (if set)
241+
3. Default timeout of 100 seconds
242+
243+
### Timeout Values
244+
245+
The `Timeout` property accepts a `TimeSpan?` value and supports the following behaviors:
246+
247+
| Value | Behavior |
248+
|-------|----------|
249+
| `null` (not set) | Uses the next level timeout (client timeout, then default 100 seconds) |
250+
| Positive `TimeSpan` | Request times out after the specified duration (e.g., `TimeSpan.FromSeconds(30)`) |
251+
| `Timeout.InfiniteTimeSpan` or `TimeSpan.FromMilliseconds(-1)` | No timeout - request will wait indefinitely for a response |
252+
| `TimeSpan.Zero` | Request is canceled immediately (effectively no time allowed for the request) |
253+
| Other negative values | Throws `ArgumentOutOfRangeException` when the request is executed |
254+
255+
### Examples
256+
257+
```csharp
258+
// Client-level timeout of 30 seconds
259+
var options = new RestClientOptions("https://api.example.com") {
260+
Timeout = TimeSpan.FromSeconds(30)
261+
};
262+
var client = new RestClient(options);
263+
264+
// Request-level timeout override
265+
var request = new RestRequest("resource") {
266+
Timeout = TimeSpan.FromSeconds(60) // This request gets 60 seconds
267+
};
268+
269+
// Infinite timeout (no timeout)
270+
var longRunningRequest = new RestRequest("long-operation") {
271+
Timeout = Timeout.InfiniteTimeSpan
272+
};
273+
```

0 commit comments

Comments
 (0)