Skip to content

Commit

Permalink
feat(java): retry strategy APIC-261 (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Mar 28, 2022
1 parent 23f75b5 commit e4a064f
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gradle*

# Selective source file
algoliasearch-core/com/algolia/auth/**
algoliasearch-core/com/algolia/ApiException.java
algoliasearch-core/com/algolia/Configuration.java
algoliasearch-core/com/algolia/Server*.java
algoliasearch-core/com/algolia/StringUtil.java
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.algolia.exceptions;

/** Exception thrown in case of API failure such as 4XX, 5XX error. */
public class AlgoliaApiException extends AlgoliaRuntimeException {

public int getHttpErrorCode() {
return httpErrorCode;
}

private final int httpErrorCode;

public AlgoliaApiException(
String message,
Throwable cause,
int httpErrorCode
) {
super(message, cause);
this.httpErrorCode = httpErrorCode;
}

public AlgoliaApiException(String message, int httpErrorCode) {
super(message);
this.httpErrorCode = httpErrorCode;
}

public AlgoliaApiException(Throwable cause, int httpErrorCode) {
super(cause);
this.httpErrorCode = httpErrorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.algolia.exceptions;

/**
* Exception thrown when an error occurs during the retry strategy. For example: All hosts are
* unreachable.
*/
public class AlgoliaRetryException extends AlgoliaRuntimeException {

public AlgoliaRetryException(String message, Throwable cause) {
super(message, cause);
}

public AlgoliaRetryException(String message) {
super(message);
}

public AlgoliaRetryException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.algolia.exceptions;

/** Exception thrown when an error occurs during the Serialization/Deserialization process */
public class AlgoliaRuntimeException extends RuntimeException {

public AlgoliaRuntimeException(String message, Throwable cause) {
super(message, cause);
}

public AlgoliaRuntimeException(String message) {
super(message);
}

public AlgoliaRuntimeException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.algolia.utils;

import com.algolia.ApiCallback;
import com.algolia.ApiException;
import com.algolia.ProgressResponseBody;
import com.algolia.utils.retry.RetryStrategy;
import com.algolia.utils.retry.StatefulHost;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Interceptor;
Expand All @@ -15,18 +17,23 @@

public class HttpRequester implements Requester {

private RetryStrategy retryStrategy;
private OkHttpClient httpClient;
private HttpLoggingInterceptor loggingInterceptor;
private boolean debugging;

public HttpRequester() {
public HttpRequester(List<StatefulHost> hosts) {
this.retryStrategy = new RetryStrategy(hosts);

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(retryStrategy.getRetryInterceptor());
builder.addNetworkInterceptor(getProgressInterceptor());
builder.retryOnConnectionFailure(false);

httpClient = builder.build();
}

public Call newCall(Request request) throws ApiException {
public Call newCall(Request request) {
return httpClient.newCall(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.algolia.utils;

import com.algolia.ApiException;
import okhttp3.Call;
import okhttp3.Request;

public interface Requester {
public Call newCall(Request request) throws ApiException;
public Call newCall(Request request);

/**
* Enable/disable debugging for this API client.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.algolia.utils;

import java.time.Clock;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;

public class Utils {

private static final ZoneRules ZONE_RULES_UTC = ZoneOffset.UTC.getRules();

/**
* Memory optimization for getZoneRules with the same ZoneOffset (UTC). ZoneRules is immutable and
* threadsafe, but getRules method consumes a lot of memory during load testing.
*/
public static OffsetDateTime nowUTC() {
final Instant now = Clock.system(ZoneOffset.UTC).instant();
return OffsetDateTime.ofInstant(now, ZONE_RULES_UTC.getOffset(now));
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
package com.algolia.utils.echo;

import com.algolia.ApiException;
import com.algolia.utils.Requester;
import okhttp3.Request;

public class EchoRequester implements Requester {

public CallEcho newCall(Request request) throws ApiException {
private int connectionTimeout, readTimeout, writeTimeout;

public EchoRequester() {
this.connectionTimeout = 100;
this.readTimeout = 100;
this.writeTimeout = 100;
}

public CallEcho newCall(Request request) {
return new CallEcho(request);
}

// NO-OP for now

public void setDebugging(boolean debugging) {}

public int getConnectTimeout() {
return 100;
return this.connectionTimeout;
}

public void setConnectTimeout(int connectionTimeout) {}
public void setConnectTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

public int getReadTimeout() {
return 100;
return this.readTimeout;
}

public void setReadTimeout(int readTimeout) {}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}

public int getWriteTimeout() {
return 100;
return this.writeTimeout;
}

public void setWriteTimeout(int writeTimeout) {}
public void setWriteTimeout(int writeTimeout) {
this.writeTimeout = writeTimeout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.algolia.utils.retry;

public enum CallType {
READ,
WRITE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algolia.utils.retry;

public enum RetryOutcome {
SUCCESS,
RETRY,
FAILURE,
}
Loading

0 comments on commit e4a064f

Please sign in to comment.