Skip to content

Commit 5c84117

Browse files
authored
docs: improve retry configuration documentation (#104)
* Update README and JSDocs * Fix default value UT * Improve samples * More sample fixes * Whoops
1 parent 172c936 commit 5c84117

File tree

8 files changed

+51
-10
lines changed

8 files changed

+51
-10
lines changed

README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ import java.net.http.HttpResponse;
7272

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

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

9191
```
9292

93+
### Exponential backoff retry configuration
94+
95+
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.
96+
97+
The exponential backoff parameters are as follows:
98+
99+
- `retryAfter` - The amount of time, in milliseconds, to wait between throttled request attempts.
100+
101+
Optional, will default to 5,000.
102+
103+
- `maxRetries` - The maximum number of times to retry throttled requests.
104+
105+
Optional, will default to 10.
106+
107+
- `timeMultiple` - The multiple by which to increase the wait time between each throttled request attempt.
108+
109+
Optional, will default to 2.
110+
111+
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:
112+
113+
```java
114+
PushSource pushSource = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withMaxRetries(5).withRetryAfter(10000).build());
115+
116+
PushService pushService = new PushService(myPushEnabledSource, new BackoffOptionsBuilder().withMaxRetries(10).build());
117+
118+
StreamService streamService = new StreamService(myStreamEnabledSource, new BackoffOptionsBuilder().withRetryAfter(2000).withTimeMultiple(3).build());
119+
```
120+
121+
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).
122+
93123
## Logging
94124

95125
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.

samples/DeleteOneDocument.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

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

samples/PushOneDocument.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import com.coveo.pushapiclient.BackoffOptionsBuilder;
22
import com.coveo.pushapiclient.DocumentBuilder;
33
import com.coveo.pushapiclient.PushSource;
4+
import com.coveo.pushapiclient.PlatformUrlBuilder;
45

56
import java.io.IOException;
67
import java.net.http.HttpResponse;
78

89
public class PushOneDocument {
910
public static void main(String[] args) {
10-
PushSource source = new PushSource("my_api_key", "my_org_id", new BackoffOptionsBuilder().withMaxRetries(5).withRetryAfter(10000).build());
11+
PushSource source = PushSource.fromPlatformUrl("my_api_key", "my_org_id", "my_source_id");
12+
1113
DocumentBuilder documentBuilder = new DocumentBuilder("https://my.document.uri", "My document title")
1214
.withData("these words will be searchable");
1315

1416
try {
15-
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
17+
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
1618
System.out.println(String.format("Push document status: %s", response.statusCode()));
1719
System.out.println(String.format("Push document response: %s", response.body()));
1820
} catch (IOException e) {

samples/PushOneDocumentWithMetadata.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import com.coveo.pushapiclient.BackoffOptions;
22
import com.coveo.pushapiclient.BackoffOptionsBuilder;
33
import com.coveo.pushapiclient.DocumentBuilder;
4+
import com.coveo.pushapiclient.PlatformUrlBuilder;
45
import com.coveo.pushapiclient.PushSource;
56

67
import java.io.IOException;
@@ -9,7 +10,7 @@
910

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

2425
try {
25-
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
26+
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
2627
System.out.println(String.format("Push document status: %s", response.statusCode()));
2728
System.out.println(String.format("Push document response: %s", response.body()));
2829
} catch (IOException e) {

samples/PushOneDocumentWithPermissions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void main(String[] args) {
1717
.withDeniedPermissions(deniedUsers);
1818

1919
try {
20-
HttpResponse<String> response = source.addOrUpdateDocument("my_source_id", documentBuilder);
20+
HttpResponse<String> response = source.addOrUpdateDocument(documentBuilder);
2121
System.out.println(String.format("Push document status: %s", response.statusCode()));
2222
System.out.println(String.format("Push document response: %s", response.body()));
2323
} catch (IOException e) {

src/main/java/com/coveo/pushapiclient/BackoffOptions.java

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ public class BackoffOptions {
55
private final int maxRetries;
66
private final int timeMultiple;
77

8+
/**
9+
* @param retryAfter The amount of time, in milliseconds, to wait between throttled request
10+
* attempts.
11+
* @param maxRetries The maximum number of times to retry throttled requests.
12+
* @param timeMultiple The multiple by which to increase the wait time between each throttled
13+
* request attempt.
14+
*/
815
public BackoffOptions(int retryAfter, int maxRetries, int timeMultiple) {
916
this.retryAfter = retryAfter;
1017
this.maxRetries = maxRetries;

src/main/java/com/coveo/pushapiclient/BackoffOptionsBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class BackoffOptionsBuilder {
44
public static final int DEFAULT_RETRY_AFTER = 5000;
5-
public static final int DEFAULT_MAX_RETRIES = 50;
5+
public static final int DEFAULT_MAX_RETRIES = 10;
66
public static final int DEFAULT_TIME_MULTIPLE = 2;
77

88
private int retryAfter = DEFAULT_RETRY_AFTER;

src/test/java/com/coveo/pushapiclient/BackoffOptionsBuilderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void setup() {
1818
public void testWithDefaultValues() {
1919
BackoffOptions backoffOptions = backoffOptionsBuilder.build();
2020
assertEquals("Should return default retry after time", 5000, backoffOptions.getRetryAfter());
21-
assertEquals("Should return default max retries", 50, backoffOptions.getMaxRetries());
21+
assertEquals("Should return default max retries", 10, backoffOptions.getMaxRetries());
2222
assertEquals("Should return default time multiple", 2, backoffOptions.getTimeMultiple());
2323
}
2424

0 commit comments

Comments
 (0)