Skip to content

Commit

Permalink
docs: improve retry configuration documentation (#104)
Browse files Browse the repository at this point in the history
* Update README and JSDocs

* Fix default value UT

* Improve samples

* More sample fixes

* Whoops
  • Loading branch information
dmbrooke authored Sep 18, 2023
1 parent 172c936 commit 5c84117
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 10 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ import java.net.http.HttpResponse;

public class PushOneDocument {
public static void main(String[] args) {
Source source = new Source("my_api_key", "my_org_id");
Source pushSource = new PushSource("my_api_key", "my_org_id");
DocumentBuilder documentBuilder = new DocumentBuilder("https://my.document.uri", "My document title")
.withData("these words will be searchable");

try {
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
HttpResponse<String> response = pushSource.addOrUpdateDocument("my_source_id", documentBuilder);
System.out.println(String.format("Source creation status: %s", response.statusCode()));
System.out.println(String.format("Source creation response: %s", response.body()));
} catch (IOException e) {
Expand All @@ -90,6 +90,36 @@ public class PushOneDocument {

```

### Exponential backoff retry configuration

By default, the SDK leverages an exponential backoff retry mechanism. Exponential backoff allows for the SDK to make multiple attempts to resolve throttled requests, increasing the amount of time to wait for each subsequent attempt. Outgoing requests will retry when a `429` status code is returned from the platform.

The exponential backoff parameters are as follows:

- `retryAfter` - The amount of time, in milliseconds, to wait between throttled request attempts.

Optional, will default to 5,000.

- `maxRetries` - The maximum number of times to retry throttled requests.

Optional, will default to 10.

- `timeMultiple` - The multiple by which to increase the wait time between each throttled request attempt.

Optional, will default to 2.

You may configure the exponential backoff that will be applied to all outgoing requests. To do so, specify use the `BackoffOptionsBuilder` class to create a `BackoffOptions` object when creating either a `PushService`, `PushSource`, or `StreamService` object:

```java
PushSource pushSource = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withMaxRetries(5).withRetryAfter(10000).build());

PushService pushService = new PushService(myPushEnabledSource, new BackoffOptionsBuilder().withMaxRetries(10).build());

StreamService streamService = new StreamService(myStreamEnabledSource, new BackoffOptionsBuilder().withRetryAfter(2000).withTimeMultiple(3).build());
```

By default, requests will retry a maximum of 10 times, waiting 5 seconds after the first attempt, with a time multiple of 2 (which will equate to a maximum execution time of roughly 1.5 hours).

## Logging

If you want to push multiple documents to your Coveo organization and use a service for that (e.g. `PushService`, `StreamService`), you may find it useful to configure a **logger** to catch error and warning messages.
Expand Down
3 changes: 2 additions & 1 deletion samples/DeleteOneDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

public class DeleteOneDocument {
public static void main(String[] args) {
PushSource source = new PushSource("my_api_key", "my_org_id");
URL sourceUrl = new URL("https://api.cloud.coveo.com/push/v1/organizations/org_id/sources/source_id");
PushSource source = new PushSource("my_api_key", sourceUrl);
String documentId = "https://my.document.uri";
Boolean deleteChildren = true;

Expand Down
6 changes: 4 additions & 2 deletions samples/PushOneDocument.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import com.coveo.pushapiclient.BackoffOptionsBuilder;
import com.coveo.pushapiclient.DocumentBuilder;
import com.coveo.pushapiclient.PushSource;
import com.coveo.pushapiclient.PlatformUrlBuilder;

import java.io.IOException;
import java.net.http.HttpResponse;

public class PushOneDocument {
public static void main(String[] args) {
PushSource source = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withMaxRetries(5).withRetryAfter(10000).build());
PushSource source = PushSource.fromPlatformUrl("my_api_key", "my_org_id", "my_source_id");

DocumentBuilder documentBuilder = new DocumentBuilder("https://my.document.uri", "My document title")
.withData("these words will be searchable");

try {
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
System.out.println(String.format("Push document status: %s", response.statusCode()));
System.out.println(String.format("Push document response: %s", response.body()));
} catch (IOException e) {
Expand Down
5 changes: 3 additions & 2 deletions samples/PushOneDocumentWithMetadata.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import com.coveo.pushapiclient.BackoffOptions;
import com.coveo.pushapiclient.BackoffOptionsBuilder;
import com.coveo.pushapiclient.DocumentBuilder;
import com.coveo.pushapiclient.PlatformUrlBuilder;
import com.coveo.pushapiclient.PushSource;

import java.io.IOException;
Expand All @@ -9,7 +10,7 @@

public class PushOneDocumentWithMetadata {
public static void main(String[] args) {
PushSource source = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withTimeMultiple(1).build());
PushSource source = PushSource.fromPlatformUrl("my_api_key", "my_org_id", "my_source_id", new PlatformUrlBuilder().build(), new BackoffOptionsBuilder().withTimeMultiple(1).build());
DocumentBuilder documentBuilder = new DocumentBuilder("https://my.document.uri", "My document title")
.withData("these words will be searchable")
.withAuthor("bob")
Expand All @@ -22,7 +23,7 @@ public static void main(String[] args) {
}});

try {
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
System.out.println(String.format("Push document status: %s", response.statusCode()));
System.out.println(String.format("Push document response: %s", response.body()));
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion samples/PushOneDocumentWithPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) {
.withDeniedPermissions(deniedUsers);

try {
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
System.out.println(String.format("Push document status: %s", response.statusCode()));
System.out.println(String.format("Push document response: %s", response.body()));
} catch (IOException e) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/coveo/pushapiclient/BackoffOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ public class BackoffOptions {
private final int maxRetries;
private final int timeMultiple;

/**
* @param retryAfter The amount of time, in milliseconds, to wait between throttled request
* attempts.
* @param maxRetries The maximum number of times to retry throttled requests.
* @param timeMultiple The multiple by which to increase the wait time between each throttled
* request attempt.
*/
public BackoffOptions(int retryAfter, int maxRetries, int timeMultiple) {
this.retryAfter = retryAfter;
this.maxRetries = maxRetries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class BackoffOptionsBuilder {
public static final int DEFAULT_RETRY_AFTER = 5000;
public static final int DEFAULT_MAX_RETRIES = 50;
public static final int DEFAULT_MAX_RETRIES = 10;
public static final int DEFAULT_TIME_MULTIPLE = 2;

private int retryAfter = DEFAULT_RETRY_AFTER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void setup() {
public void testWithDefaultValues() {
BackoffOptions backoffOptions = backoffOptionsBuilder.build();
assertEquals("Should return default retry after time", 5000, backoffOptions.getRetryAfter());
assertEquals("Should return default max retries", 50, backoffOptions.getMaxRetries());
assertEquals("Should return default max retries", 10, backoffOptions.getMaxRetries());
assertEquals("Should return default time multiple", 2, backoffOptions.getTimeMultiple());
}

Expand Down

0 comments on commit 5c84117

Please sign in to comment.