Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generators): allow per-spec timeouts [skip-bc] #4173

Merged
merged 29 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export function createTransporter({
responseTimeout: getTimeout(timeoutsCount, isRead ? timeout.read : timeout.write),
};

console.log(payload);
shortcuts marked this conversation as resolved.
Show resolved Hide resolved

/**
* The stackFrame is pushed to the stackTrace so we
* can have information about onRetry and onFailure
Expand Down
Loading