Skip to content

Commit

Permalink
Update retrying client documentation
Browse files Browse the repository at this point in the history
Update retrying client documentation according to our new documentation policy

Closes #309
  • Loading branch information
iDneprov committed Feb 7, 2023
1 parent 5819ca9 commit cb018c6
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions docs/RetryingTarantoolClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,17 @@ By default, failed requests will be repeated only for some known network problem
Some retry policies are available in the `TarantoolRequestRetryPolicies` class, but you may use your own implementations.
If you want to use proxy calls or retry settings only for a number of requests, you may use configureClient(client)
in `TarantoolClientFactory` for making a new configured client instance. Note, that the new instance will share the same
connection pool and basic client settings, and only augment the behavior of the client.
See an example below:

```java

TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> setupClient() {
return TarantoolClientFactory.createClient()
.withCredentials("admin", "secret-cluster-cookie")
.withAddress(container.getRouterHost(), container.getRouterPort())
.withProxyMethodMapping()
.build();
}

TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> retrying(
TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client, int retries, long delay) {
return TarantoolClientFactory.configureClient(client)
.withRetryingByNumberOfAttempts(
retries,
// you can use default predicates from TarantoolRequestRetryPolicies for checking errors
TarantoolRequestRetryPolicies.retryNetworkErrors()
// also you can use your own predicates and combine them with each other or with defaults
.or(e -> e.getMessage().contains("Unsuccessful attempt"))
.or(TarantoolRequestRetryPolicies.retryTarantoolNoSuchProcedureErrors()),
policy -> policy.withDelay(delay))
.build();
}

...

TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client = setupClient();
String result = retrying(client, 4, 500).callForSingleResult("retrying_function", String.class).get();
assertEquals("Success", result);
result = retrying(client, 2, 1000).callForSingleResult("retrying_function", String.class).get();
assertEquals("Success", result);
```
connection pool and basic client settings, and only augment the behavior of the client.

In this example I use custom delete function.
https://github.com/tarantool/cartridge-java/blob/25cad2843d6ed9ed76cf4020766f92307300d993/src/test/resources/cartridge/app/roles/api_router.lua#L165-L176
You can set up any client. In this case I use CRUD client.
https://github.com/tarantool/cartridge-java/blob/25cad2843d6ed9ed76cf4020766f92307300d993/src/test/java/io/tarantool/driver/integration/ReconnectIT.java#L147-L163
And reuse it then I need retrying client.
https://github.com/tarantool/cartridge-java/blob/25cad2843d6ed9ed76cf4020766f92307300d993/src/test/java/io/tarantool/driver/integration/ReconnectIT.java#L194-L214
You don't have to set up basic client if you need retying client only.
All methods of client builder with prefix `withRetrying` can be used with `createClient`.

In this code I call `delete_with_error_if_not_found` (custom delete function) before the record was inserted to the
database. So client recalls delete and removes the record after it was inserted.
https://github.com/tarantool/cartridge-java/blob/25cad2843d6ed9ed76cf4020766f92307300d993/src/test/java/io/tarantool/driver/integration/ReconnectIT.java#L82-L109

0 comments on commit cb018c6

Please sign in to comment.