Reuse HTTP connections to cut resource use and avoid request timeouts under high concurrency.
Connection reuse
The DashScope SDK supports connection reuse in both Java and Python:
- Java SDK: A built-in connection pool is enabled by default. Configure parameters like maximum connections and timeout durations.
- Python SDK: Enable connection reuse by passing a custom HTTP client. It supports both synchronous and asynchronous calls.
Before you begin
Before running the code examples:
Java SDK
The Java SDK includes a built-in connection pool enabled by default. Adjust maximum connections and timeout settings to optimize for your workload.
Parameters
| Parameter | Description | Default value | Unit | Notes |
|---|---|---|---|---|
connectTimeout | Connection establishment timeout. | 120 | seconds | In low-latency scenarios, set a shorter timeout to reduce wait time. |
readTimeout | Data read timeout. | 300 | seconds | |
writeTimeout | Data write timeout. | 60 | seconds | |
connectionIdleTimeout | Idle connection timeout in the pool. | 300 | seconds | In high-concurrency scenarios, extend the idle timeout to avoid frequent connection creation. |
connectionPoolSize | Maximum connections in the pool. | 32 | connections | In high-concurrency scenarios, too few connections cause blocking, timeouts, and frequent reconnections. Too many connections overload the server. Balance the count based on your workload. |
maximumAsyncRequests | Maximum concurrent requests across all hosts (global limit). Must be ≤ connectionPoolSize; otherwise, requests may block. | 32 | requests | |
maximumAsyncRequestsPerHost | Maximum concurrent requests per host. Must be ≤ maximumAsyncRequests. | 32 | requests |
Code example
The following example configures connection pool parameters (timeouts, maximum connections) and calls a model service. Adjust the parameters to optimize concurrency and resource usage.
Python SDK
The Python SDK supports connection reuse by passing a custom HTTP client (httpx.Client / httpx.AsyncClient). It supports two calling methods: asynchronous HTTP (coroutine-based) and synchronous HTTP.
Asynchronous HTTP
For asynchronous scenarios, use httpx.AsyncClient with httpx.Limits to enable connection reuse. httpx.Limits supports configuring parameters like connection limits:
| Parameter | Description | Default value | Notes |
|---|---|---|---|
max_connections | Total connection limit. | None (unlimited) | In high-concurrency scenarios, increase this value to improve concurrency. |
max_keepalive_connections | Maximum number of keep-alive connections. | None (unlimited) | Controls the number of active connections kept in the pool, preventing excessive load on a single server. |
keepalive_expiry | Keep-alive timeout for connections. | 5.0 (seconds) | Idle keep-alive connections are closed automatically after this duration. |
Code example
Synchronous HTTP
For synchronous scenarios, use httpx.Client to enable connection reuse. Multiple requests within the same HTTP client reuse the underlying TCP connection, avoiding the overhead of repeatedly establishing connections.
Code example: single call
Code example: multiple calls with a shared HTTP client
To reuse an HTTP client across multiple calls:
Best practices
- Java SDK: Set
connectionPoolSizeandmaximumAsyncRequestsbased on your workload. Balance the connection count: too few cause blocking, too many overload the server. - Python SDK: Use the
withstatement to automatically manage the HTTP client lifecycle and ensure correct resource cleanup. - Choose the right method: Use async calls for async applications (asyncio, FastAPI). Use sync calls for traditional sync applications.
Error codes
If a model call fails (the Python SDK throws a DashScopeError exception, and HTTP calls return an error message), see Error messages to resolve the issue.