Skip to content

Commit

Permalink
feat(generators): allow per-spec timeouts [skip-bc] (#4173)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 5, 2024
1 parent f780851 commit 106d643
Show file tree
Hide file tree
Showing 45 changed files with 1,257 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.Closeable;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
Expand All @@ -30,7 +31,16 @@ public abstract class ApiClient implements Closeable {
private AuthInterceptor authInterceptor;

/** Constructs a new instance of the {@link ApiClient}. */
protected ApiClient(String appId, String apiKey, String clientName, @Nullable ClientOptions options, List<Host> defaultHosts) {
protected ApiClient(
String appId,
String apiKey,
String clientName,
@Nullable ClientOptions options,
List<Host> defaultHosts,
Duration connectTimeout,
Duration readTimeout,
Duration writeTimeout
) {
if (appId == null || appId.isEmpty()) {
throw new AlgoliaRuntimeException("`appId` is missing.");
}
Expand All @@ -41,11 +51,20 @@ protected ApiClient(String appId, String apiKey, String clientName, @Nullable Cl
this.executor = clientOptions.getExecutor();
this.requester = clientOptions.getCustomRequester() != null
? clientOptions.getCustomRequester()
: defaultRequester(appId, apiKey, clientName, clientOptions, defaultHosts);
: defaultRequester(appId, apiKey, clientName, clientOptions, defaultHosts, connectTimeout, readTimeout, writeTimeout);
}

/** Creates a default {@link Requester} for executing API requests. */
private Requester defaultRequester(String appId, String apiKey, String clientName, ClientOptions options, List<Host> defaultHosts) {
private Requester defaultRequester(
String appId,
String apiKey,
String clientName,
ClientOptions options,
List<Host> defaultHosts,
Duration connectTimeout,
Duration readTimeout,
Duration writeTimeout
) {
AlgoliaAgent algoliaAgent = new AlgoliaAgent(BuildConfig.VERSION)
.addSegment(new AlgoliaAgent.Segment(clientName, BuildConfig.VERSION))
.addSegments(options.getAlgoliaAgentSegments());
Expand All @@ -58,7 +77,10 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam
HttpRequester.Builder builder = new HttpRequester.Builder(serializer)
.addInterceptor(authInterceptor)
.addInterceptor(new UserAgentInterceptor(algoliaAgent))
.addInterceptor(new RetryStrategy(statefulHosts));
.addInterceptor(new RetryStrategy(statefulHosts))
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.setWriteTimeout(writeTimeout);
if (options.getRequesterConfig() != null) {
options.getRequesterConfig().accept(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public static class Builder {
private final List<AlgoliaAgent.Segment> algoliaAgentSegments = new ArrayList<>();
private final Map<String, String> defaultHeaders = new HashMap<>();
private LogLevel logLevel = LogLevel.NONE;
private Duration connectTimeout = Duration.ofSeconds(2);
private Duration writeTimeout = Duration.ofSeconds(30);
private Duration readTimeout = Duration.ofSeconds(5);
private Duration connectTimeout = Duration.ZERO;
private Duration writeTimeout = Duration.ZERO;
private Duration readTimeout = Duration.ZERO;
private CompressionType compressionType = CompressionType.NONE;

public Builder setRequester(Requester requester) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -34,9 +35,9 @@ public final class HttpRequester implements Requester {
/** Private constructor initialized using the builder pattern. */
private HttpRequester(Builder builder, ClientConfig config) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(config.getConnectTimeout())
.readTimeout(config.getReadTimeout())
.writeTimeout(config.getWriteTimeout())
.connectTimeout(config.getConnectTimeout() == Duration.ZERO ? builder.connectTimeout : config.getConnectTimeout())
.readTimeout(config.getReadTimeout() == Duration.ZERO ? builder.readTimeout : config.getReadTimeout())
.writeTimeout(config.getWriteTimeout() == Duration.ZERO ? builder.writeTimeout : config.getWriteTimeout())
.addInterceptor(new HeaderInterceptor(config.getDefaultHeaders()))
.addNetworkInterceptor(new LogInterceptor(config.getLogger(), config.getLogLevel()));
builder.interceptors.forEach(clientBuilder::addInterceptor);
Expand Down Expand Up @@ -192,11 +193,11 @@ public void close() throws IOException {
public static class Builder {

private final List<Interceptor> interceptors = new ArrayList<>();

private final List<Interceptor> networkInterceptors = new ArrayList<>();

private Duration connectTimeout = Duration.ofSeconds(2);
private Duration writeTimeout = Duration.ofSeconds(30);
private Duration readTimeout = Duration.ofSeconds(5);
private Consumer<OkHttpClient.Builder> clientConfig;

private final JsonSerializer serializer;

public Builder(JsonSerializer serializer) {
Expand Down Expand Up @@ -229,6 +230,21 @@ public Builder setHttpClientConfig(Consumer<OkHttpClient.Builder> config) {
return this;
}

public Builder setConnectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

public Builder setWriteTimeout(Duration writeTimeout) {
this.writeTimeout = writeTimeout;
return this;
}

public Builder setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}

/**
* Builds and returns a HttpRequester instance.
*
Expand Down
Loading

0 comments on commit 106d643

Please sign in to comment.