Misconfigured timeouts are a leading cause of connection resets, request backlogs, and cascading failures in dedicated gateway deployments. This guide explains the two timeout types you need to configure — idle connection timeout and request timeout — and provides client configuration examples for Java, Go, and the Alibaba Cloud Elastic Algorithm Service (EAS) SDK, plus a troubleshooting reference for common timeout errors.
How timeouts work in a dedicated gateway
A dedicated gateway sits between your client and the backend model service, creating two distinct connection segments: client-to-gateway and gateway-to-backend. Each segment has its own timeout behavior.
The gateway enforces two fixed idle connection timeouts that you cannot modify:
| Segment | Fixed idle timeout | Behavior |
|---|---|---|
| Gateway as server (facing the client) | 600 seconds | Closes client-to-gateway connections idle for more than 600 seconds |
| Gateway as client (facing the model service) | 30 seconds | Closes gateway-to-backend connections idle for more than 30 seconds |
The request timeout controls how long the gateway waits for the backend to return a complete response. This timeout covers the full lifecycle of a request: from TCP connection establishment, through sending the request, to receiving the last byte of the response.
Configure the idle connection timeout
Set the client-side idle connection timeout to less than 600 seconds. This makes the client responsible for closing idle connections before the gateway does, preventing errors when a client tries to reuse a connection the gateway has already closed.
The following diagram illustrates the connection path:
Client configuration examples
These examples are for reference only. Do not copy them directly into a production environment. Adjust parameters based on your system's traffic, load, and client library version.
All examples use a 500-second idle timeout, which is below the gateway's 600-second limit.
Key parameters
| Parameter | Language | Recommended value | Description |
|---|---|---|---|
closeIdleConnections() interval | Java (Apache HttpClient 4.x) | 500 seconds | Closes connections idle longer than this duration |
IdleConnTimeout | Go | Less than 600 seconds | Controls the idle connection timeout in the Transport struct |
setIdleConnectionTimeout() | EAS SDK (Java) | 500 seconds | Sets the idle connection timeout; unit: seconds |
setConnectionCleanupInterval() | EAS SDK (Java) | 5,000 ms | Interval between cleanup runs; unit: milliseconds |
Java (Apache HttpClient 4.x)
Apache HttpClient 4.x manages idle connections by periodically calling closeIdleConnections() on the connection manager. Run this in a background thread.
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.client.config.RequestConfig;
import java.util.concurrent.TimeUnit;
public class HttpClientIdleTimeout {
public static void main(String[] args) throws InterruptedException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(20); // Example value: maximum connections per route
cm.setMaxTotal(100); // Example value: maximum total connections
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) // Example value: connection timeout, 5 seconds
.setSocketTimeout(10000) // Example value: read timeout, 10 seconds
.build();
try (CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(requestConfig)
.build()) {
// Start a background thread to clean up idle connections periodically
Thread cleanerThread = new Thread(() -> {
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(5000); // Example value: check every 5 seconds
// Close connections idle for more than 500 seconds (below the gateway's 600-second threshold)
cm.closeIdleConnections(500, TimeUnit.SECONDS);
// Close connections the server has already closed
cm.closeExpiredConnections();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Reset the interrupt flag
}
});
cleanerThread.setDaemon(true); // Exit when the main thread exits
cleanerThread.start();
// Execute HTTP requests...
// For example: httpClient.execute(new HttpGet("http://your-gateway-url"));
// Simulate the program running for a while
Thread.sleep(60000); // Example value: run for 1 minute
// Stop the cleaner thread gracefully when the application shuts down
cleanerThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}Go
The net/http package exposes fine-grained connection pool control through the Transport struct. Set IdleConnTimeout to less than 600 seconds.
package main
import (
"net/http"
"time"
)
func main() {
// Create a custom Transport
tr := &http.Transport{
MaxIdleConns: 100, // Maximum number of idle connections
IdleConnTimeout: 500 * time.Second, // Idle connection timeout: 500 seconds (less than 600 seconds)
DisableKeepAlives: false, // Keep-Alive enabled
}
_ = tr
}EAS SDK (Java)
For the Alibaba Cloud EAS SDK, enable idle connection cleanup and set the idle timeout in the HttpConfig object.
import com.aliyun.openservices.eas.predict.http.PredictClient;
import com.aliyun.openservices.eas.predict.http.HttpConfig;
public class EasSdkTimeoutJava {
public static void main(String[] args) {
// Global client configuration
HttpConfig httpConfig = new HttpConfig();
// Enable the idle connection cleanup interval; unit: milliseconds
httpConfig.setConnectionCleanupInterval(5000);
// Set the idle connection timeout to less than the gateway's 600-second threshold; unit: seconds
httpConfig.setIdleConnectionTimeout(500);
PredictClient client = new PredictClient(httpConfig);
client.setEndpoint("your-eas-service-endpoint");
client.setModelName("your-model-name");
// client.setToken("your-token"); // Uncomment if authentication is required
}
}Configure the request timeout
The dedicated gateway's default request timeout is 10 minutes (600 seconds). To customize it, set the metadata.rpc.keepalive field in the service configuration file. The gateway reads this value and applies it as the request timeout. For the full list of supported fields, see metadata parameter descriptions.
Application Load Balancer (ALB)-based dedicated gateways do not currently support custom request timeout settings.
Because the gateway's idle connection timeout is fixed at 600 seconds, setting a request timeout greater than 600 seconds has no effect on short-lived connections.
For tasks that run longer than 10 minutes, use one of the following alternatives instead of increasing the timeout:
Streaming — for large file downloads or AI content generation.
WebSocket — for real-time, bidirectional communication.
Set the client request timeout slightly longer than the gateway timeout to avoid false timeout errors caused by the client aborting a request the server is still processing:
Client timeout = Server timeout + buffer (1–5 seconds)For example, if the gateway timeout is 600 seconds, set the client to 610 seconds.
The following diagram illustrates the connection path:
Client configuration examples
These examples are for reference only. Do not copy them directly into a production environment. Adjust parameters based on your system's traffic, load, and client library version.
All examples configure a 610-second request timeout (600-second gateway default + 10-second buffer).
Key parameters
| Parameter | Language | Recommended value | Description |
|---|---|---|---|
setSocketTimeout() | Java (Apache HttpClient 4.x) | 610,000 ms | Data transmission (read) timeout; unit: milliseconds |
setConnectTimeout() | Java (Apache HttpClient 4.x) | 5,000 ms | TCP connection establishment timeout; unit: milliseconds |
Client.Timeout | Go | 610 seconds | Timeout for the entire request lifecycle |
setReadTimeout() | EAS SDK (Java) | 610 seconds | Read timeout; unit: seconds |
setConnectTimeout() | EAS SDK (Java) | 5 seconds | Connection timeout; unit: seconds |
Java (Apache HttpClient 4.x)
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ApacheHttpClientTimeout {
public static void main(String[] args) {
// Client timeout = gateway timeout (600 s) + buffer (10 s) = 610 s
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) // Connection timeout; unit: milliseconds
.setSocketTimeout(610000) // Read timeout: 610 seconds; unit: milliseconds
.build();
}
}Go
The net/http package provides two ways to set a request timeout: the Timeout field on http.Client (applies to all requests) and context.WithTimeout (applies to a single request).
package main
import (
"context"
"fmt"
"io"
"net/http"
"time"
)
func main() {
// Client timeout = gateway timeout (600 s) + buffer (10 s) = 610 s
client := &http.Client{
Timeout: 610 * time.Second, // Timeout for the entire request
}
req, err := http.NewRequest("GET", "http://your-gateway-url", nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Override the timeout for a single request using context
ctx, cancel := context.WithTimeout(req.Context(), 610*time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
// Check if it is a timeout error
if t, ok := err.(interface{ Timeout() bool }); ok && t.Timeout() {
fmt.Println("Request timed out!")
}
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
fmt.Printf("Response Status: %s\n", resp.Status)
fmt.Printf("Response Body: %s\n", body)
}EAS SDK (Java)
For the Alibaba Cloud EAS SDK, set the connection and read timeouts in the global HttpConfig object.
import com.aliyun.openservices.eas.predict.http.PredictClient;
import com.aliyun.openservices.eas.predict.http.HttpConfig;
public class EasSdkTimeoutJava {
public static void main(String[] args) {
// Global client configuration
HttpConfig httpConfig = new HttpConfig();
// Connection timeout; unit: seconds
httpConfig.setConnectTimeout(5);
// Read timeout = gateway timeout (600 s) + buffer (10 s) = 610 s; unit: seconds
httpConfig.setReadTimeout(610);
}
}FAQ
Why am I getting "Connection reset by peer" errors?
The client's idle connection timeout is longer than the gateway's 600-second threshold. The gateway closes the connection after 600 seconds of inactivity, but the client still considers the connection valid and tries to reuse it — causing the reset.
Set the client idle connection timeout to less than 600 seconds. See Configure the idle connection timeout for language-specific examples.
Common error messages for this scenario:
Connection reset by peerBroken pipejava.net.SocketException: Connection reset(Java)requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))(Python requests)read: connection reset by peer(Go)HTTP 503 Service Unavailable
Why is the dedicated gateway returning HTTP 503?
The gateway tried to reuse a connection to the backend model service, but the model service had already closed it. The gateway's client-side idle timeout is fixed at 30 seconds. If the model service closes connections in less than 30 seconds, the gateway may attempt to forward requests on a closed connection.
Check the model service's idle connection timeout and make sure it is at least 30 seconds. If the model service timeout is shorter and cannot be changed, contact the service owner to increase it.
Why am I getting frequent timeout errors on the client?
The client timeout is shorter than the gateway's request timeout. The client aborts the connection while the gateway is still waiting for the backend to respond.
Common error messages:
java.net.http.HttpTimeoutException(JavaHttpClient)java.net.SocketTimeoutException: Read timed out(Java Apache HttpClient)requests.exceptions.ReadTimeout(Python requests)context deadline exceeded(Go)
Set the client request timeout to server timeout + 1–5 seconds. For the default 600-second gateway timeout, use 610 seconds on the client.
If you intentionally use a short timeout to implement a fail-fast strategy, these errors are expected. The 610-second recommendation applies when you want the server to complete processing before the client gives up.
Why am I getting HTTP 504 errors?
HTTP 504 Gateway Timeout means the gateway timed out waiting for the backend model service to respond. The gateway's configured request timeout (default 600 seconds, or a custom value set via metadata.rpc.keepalive) expired before the model service returned a result. Depending on how the model service handles its internal timeout and reports errors to the gateway, the client might also receive an HTTP 502 Bad Gateway or HTTP 500 Internal Server Error.
To troubleshoot:
Check client logs — Review the exception type and stack trace.
Check gateway logs — Look for error codes and forwarding status related to the failed request.
Check model service logs — If the gateway returns a 5xx error, inspect the model service logs to confirm whether an internal error occurred or whether processing took too long.
If the model service consistently needs more than 600 seconds to process a request, switch to streaming or WebSocket instead of increasing the timeout. See Configure the request timeout for details.