Skip to content

Commit

Permalink
Adds ktor retry plugin documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoengenheiro committed Apr 7, 2024
1 parent e01566d commit 15f31d0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
- 🛠️ [Kotlin Multiplatform](kmp/README.md)
- 🌐 [Kotlin-Js Interop](kotlin-js-interop/README.md)
- ⚙️ [Ktor Framework](ktor/README.md)
- 🔄 [Ktor Retry Plugin](ktor-retry-plugin/README.md)
- 🛡️ [Resilience4j](resilience4j/README.md)
78 changes: 78 additions & 0 deletions ktor-retry-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Ktor Retry Plugin

### Install

The `HttpRequestRetry` plugin can be installed using the `install` function
and configured using its **last parameter function**
(_trailing lambda_), as with other Ktor plugins.

The plugin is part of the `ktor-client-core` module.

### Configuration

| Property | Description |
|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `maxRetries` | The maximum number of retries | |
| `retryIf` | A lambda that returns `true` if the request should be retried on specific request details and or responses. |
| `retryOnExceptionIf` | A lambda that returns `true` if the request should be retried on specific request details and or exceptions that occurred. |
| `delayMillis` | A lambda that returns the delay in milliseconds before the next retry. Some methods are provided to create more complex delays, such as `exponentialDelay()` or `constantDelay()`. |

> [!NOTE]
> The plugin also provides more specific methods to retry for
> (e.g., on server errors, for example, `retryOnServerErrors()` retries on server 5xx errors).
Example:

```kotlin
val client = HttpClient(CIO) {
install(HttpRequestRetry) {
maxRetries = 5
retryIf { request, response ->
!response.status.isSuccess()
}
retryOnExceptionIf { request, cause ->
cause is NetworkError
}
delayMillis { retry ->
retry * 3000L
} // retries in 3, 6, 9, etc. seconds
}
// other configurations
}
```

Default configuration:

```kotlin
install(HttpRequestRetry) {
retryOnExceptionOrServerErrors(3)
exponentialDelay()
}
```

### Changing a Request Before Retry

It is possible to modify the request
before retrying it by using the `modifyRequest` method inside the configuration block of the plugin.
This method receives a lambda that takes the request and returns the modified request.
One usage example is to add a header with the current retry count:

```kotlin
val client = HttpClient(CIO) {
install(HttpRequestRetry) {
modifyRequest { request ->
request.headers.append("x-retry-count", retryCount.toString())
}
}
// other configurations
}
```

> [!IMPORTANT]
> To preserve configuration context between retry attempts, the plugin uses request attibutes to store data.
> If those are altered, the plugin may not work as expected.
>
> If an attribute is not present in the request, the plugin will use the default configuration associated with that attribute.
> Such behaviour can be seen in the source code:
> - [after applying configuration](https://github.com/ktorio/ktor/blob/7c76fa7c0f2b7dcc6e0445da8612d75bb5d11609/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/HttpRequestRetry.kt#L366-L373)
> - [before each retry attempt](https://github.com/ktorio/ktor/blob/7c76fa7c0f2b7dcc6e0445da8612d75bb5d11609/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/HttpRequestRetry.kt#L267-L274)
1 change: 0 additions & 1 deletion ktor-retry-plugin/client-retry/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Client Retry

A sample Ktor project showing how to use the [HttpRequestRetry](https://ktor.io/docs/client-retry.html) plugin.
> This sample is a part of the [codeSnippets](../../README.md) Gradle project.

## Running

Expand Down
1 change: 0 additions & 1 deletion ktor-retry-plugin/simulate-slow-server/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Slow Server

An application simulating a slow server by creating a custom interceptor for Ktor.
> This sample is a part of the [codeSnippets](../../README.md) Gradle project.

## Running

Expand Down

0 comments on commit 15f31d0

Please sign in to comment.