-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from FgForrest/607-support-for-disabling-keep…
…-alive-header-in-configuration fix(#607): support for disabling keep alive header in configuration
- Loading branch information
Showing
11 changed files
with
185 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,20 +42,44 @@ | |
/** | ||
* This DTO record encapsulates common settings shared among all the API endpoints. | ||
* | ||
* @param exposedOn the name of the host the APIs will be exposed on when evitaDB is running inside a container | ||
* @param ioThreads defines the number of IO thread will be used by Undertow for accept and send HTTP payload | ||
* @param accessLog defines whether the access logs will be enabled or not | ||
* @param endpoints contains specific configuration for all the API endpoints | ||
* @param certificate defines the certificate settings that will be used to secure connections to the web servers providing APIs | ||
* @param exposedOn the name of the host the APIs will be exposed on when evitaDB is running inside a container | ||
* @param ioThreads defines the number of IO thread will be used by Undertow for accept and send HTTP payload | ||
* @param idleTimeoutInMillis The amount of time a connection can be idle for before it is timed out. An idle connection is a | ||
* connection that has had no data transfer in the idle timeout period. Note that this is a fairly coarse | ||
* grained approach, and small values will cause problems for requests with a long processing time. | ||
* @param parseTimeoutInMillis How long a request can spend in the parsing phase before it is timed out. This timer is started when | ||
* the first bytes of a request are read, and finishes once all the headers have been parsed. | ||
* @param requestTimeoutInMillis The amount of time a connection can sit idle without processing a request, before it is closed by | ||
* the server. | ||
* @param keepAlive If this is true then a Connection: keep-alive header will be added to responses, even when it is not strictly required by | ||
* the specification. | ||
* @param maxEntitySizeInBytes The default maximum size of a request entity. If entity body is larger than this limit then a | ||
* java.io.IOException will be thrown at some point when reading the request (on the first read for fixed | ||
* length requests, when too much data has been read for chunked requests). | ||
* @param accessLog defines whether the access logs will be enabled or not | ||
* @param endpoints contains specific configuration for all the API endpoints | ||
* @param certificate defines the certificate settings that will be used to secure connections to the web servers providing APIs | ||
* @author Jan Novotný ([email protected]), FG Forrest a.s. (c) 2022 | ||
*/ | ||
public record ApiOptions( | ||
@Nonnull String exposedOn, | ||
@Nullable Integer ioThreads, | ||
int ioThreads, | ||
int idleTimeoutInMillis, | ||
int requestTimeoutInMillis, | ||
int parseTimeoutInMillis, | ||
boolean keepAlive, | ||
long maxEntitySizeInBytes, | ||
boolean accessLog, | ||
@Nonnull CertificateSettings certificate, | ||
@Nonnull Map<String, AbstractApiConfiguration> endpoints | ||
) { | ||
// double the value of available processors (recommended by Undertow configuration) | ||
public static final int DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors() << 1; | ||
public static final int DEFAULT_IDLE_TIMEOUT = 20 * 1000; | ||
public static final int DEFAULT_PARSE_TIMEOUT = 1000; | ||
public static final int DEFAULT_REQUEST_TIMEOUT = 1000; | ||
public static final boolean DEFAULT_KEEP_ALIVE = true; | ||
public static final long DEFAULT_MAX_ENTITY_SIZE = 2_097_152L; | ||
|
||
/** | ||
* Builder for the api options. Recommended to use to avoid binary compatibility problems in the future. | ||
|
@@ -64,8 +88,31 @@ public static ApiOptions.Builder builder() { | |
return new ApiOptions.Builder(); | ||
} | ||
|
||
public ApiOptions( | ||
@Nonnull String exposedOn, | ||
int ioThreads, int idleTimeoutInMillis, int requestTimeoutInMillis, int parseTimeoutInMillis, | ||
boolean keepAlive, long maxEntitySizeInBytes, boolean accessLog, | ||
@Nonnull CertificateSettings certificate, | ||
@Nonnull Map<String, AbstractApiConfiguration> endpoints | ||
) { | ||
this.exposedOn = exposedOn; | ||
this.ioThreads = ioThreads <= 0 ? DEFAULT_IO_THREADS : ioThreads; | ||
this.idleTimeoutInMillis = idleTimeoutInMillis <= 0 ? DEFAULT_IDLE_TIMEOUT : idleTimeoutInMillis; | ||
this.requestTimeoutInMillis = requestTimeoutInMillis <= 0 ? DEFAULT_REQUEST_TIMEOUT : requestTimeoutInMillis; | ||
this.parseTimeoutInMillis = parseTimeoutInMillis <= 0 ? DEFAULT_PARSE_TIMEOUT : parseTimeoutInMillis; | ||
this.keepAlive = keepAlive; | ||
this.maxEntitySizeInBytes = maxEntitySizeInBytes <= 0 ? DEFAULT_MAX_ENTITY_SIZE : maxEntitySizeInBytes; | ||
this.accessLog = accessLog; | ||
this.certificate = certificate; | ||
this.endpoints = endpoints; | ||
} | ||
|
||
public ApiOptions() { | ||
this(null, null, false, new CertificateSettings(), new HashMap<>(8)); | ||
this( | ||
null, DEFAULT_IO_THREADS, DEFAULT_IDLE_TIMEOUT, DEFAULT_REQUEST_TIMEOUT, DEFAULT_PARSE_TIMEOUT, | ||
DEFAULT_KEEP_ALIVE, DEFAULT_MAX_ENTITY_SIZE, false, | ||
new CertificateSettings(), new HashMap<>(8) | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -77,25 +124,21 @@ public <T extends AbstractApiConfiguration> T getEndpointConfiguration(@Nonnull | |
return (T) endpoints.get(endpointCode); | ||
} | ||
|
||
/** | ||
* Returns set {@link #ioThreads} or returns a default value. | ||
*/ | ||
public int ioThreadsAsInt() { | ||
return ofNullable(ioThreads) | ||
// double the value of available processors (recommended by Undertow configuration) | ||
.orElseGet(() -> Runtime.getRuntime().availableProcessors() << 1); | ||
} | ||
|
||
/** | ||
* Standard builder pattern implementation. | ||
*/ | ||
@ToString | ||
public static class Builder { | ||
private final Map<String, Class<?>> apiProviders; | ||
private final Map<String, AbstractApiConfiguration> enabledProviders; | ||
private int ioThreads = DEFAULT_IO_THREADS; | ||
private int idleTimeoutInMillis = DEFAULT_IDLE_TIMEOUT; | ||
private int requestTimeoutInMillis = DEFAULT_REQUEST_TIMEOUT; | ||
private int parseTimeoutInMillis = DEFAULT_PARSE_TIMEOUT; | ||
private boolean keepAlive = DEFAULT_KEEP_ALIVE; | ||
private long maxEntitySizeInBytes = DEFAULT_MAX_ENTITY_SIZE; | ||
private CertificateSettings certificate; | ||
@Nullable private String exposedOn; | ||
@Nullable private Integer ioThreads; | ||
private boolean accessLog; | ||
|
||
Builder() { | ||
|
@@ -119,8 +162,38 @@ public ApiOptions.Builder exposedOn(@Nonnull String exposedOn) { | |
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder ioThreads(int ioThreads) { | ||
this.ioThreads = ioThreads; | ||
public ApiOptions.Builder ioThreads(@Nullable Integer ioThreads) { | ||
this.ioThreads = ofNullable(ioThreads).orElse(DEFAULT_IO_THREADS); | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder idleTimeoutInMillis(int idleTimeoutInMillis) { | ||
this.idleTimeoutInMillis = idleTimeoutInMillis; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder requestTimeoutInMillis(int requestTimeoutInMillis) { | ||
this.requestTimeoutInMillis = requestTimeoutInMillis; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder parseTimeoutInMillis(int parseTimeoutInMillis) { | ||
this.parseTimeoutInMillis = parseTimeoutInMillis; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder keepAlive(boolean keepAlive) { | ||
this.keepAlive = keepAlive; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
public ApiOptions.Builder maxEntitySizeInBytes(long maxEntitySizeInBytes) { | ||
this.maxEntitySizeInBytes = maxEntitySizeInBytes; | ||
return this; | ||
} | ||
|
||
|
@@ -170,7 +243,8 @@ public <T extends AbstractApiConfiguration> ApiOptions.Builder enable(@Nonnull S | |
@Nonnull | ||
public ApiOptions build() { | ||
return new ApiOptions( | ||
exposedOn, ioThreads, accessLog, certificate, enabledProviders | ||
exposedOn, ioThreads, idleTimeoutInMillis, requestTimeoutInMillis, parseTimeoutInMillis, | ||
keepAlive, maxEntitySizeInBytes, accessLog, certificate, enabledProviders | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.