Without connection reuse, each API call opens a new TCP connection and performs a TLS handshake, adding latency. In high-concurrency scenarios, this overhead causes timeouts and resource waste. Connection reuse eliminates repeated setup, reducing latency and resource consumption.
Connection reuse
The DashScope SDK supports reducing resource consumption and improving request processing efficiency by reusing existing connections.
- Java SDK: Built-in connection pool mechanism that supports configuring parameters such as the number of connections and timeout durations, enabled by default.
- Python SDK: Supports connection reuse by passing a custom HTTP client, in both synchronous and asynchronous modes.
Java SDK
The DashScope Java SDK includes a built-in connection pool mechanism that is enabled by default. We recommend that you adjust the number of connections and the timeout durations of the connection pool based on your specific business needs to optimize connection reuse.
Configuration parameters
Parameter | Description | Default value | Unit | Notes |
|---|---|---|---|---|
connectTimeout | Timeout for establishing a connection. | 120 | seconds | In low-latency scenarios, a shorter connection timeout is typically required to reduce wait time and improve response speed. |
readTimeout | Timeout for reading data. | 300 | seconds | |
writeTimeout | Timeout for writing data. | 60 | seconds | |
connectionIdleTimeout | Timeout for idle connections in the connection pool. | 300 | seconds | In high-concurrency scenarios, appropriately extending the idle connection timeout helps avoid frequent connection creation and reduces resource consumption. |
connectionPoolSize | Maximum number of connections in the connection pool. | 32 | connections | In high-concurrency scenarios:
Adjust the configuration based on your business requirements. |
maximumAsyncRequests | Maximum number of concurrent requests. A global limit on the number of concurrent requests (including all hosts). It must be less than or equal to the maximum number of connections; otherwise, requests may block. | 32 | requests | |
maximumAsyncRequestsPerHost | Maximum number of concurrent requests for a single host. It must be less than or equal to the maximum number of concurrent requests. | 32 | requests |
Code example
Before running the code, configure the API key as an environment variable and install the latest SDK.
The following code example shows how to configure connection pool parameters (such as timeout durations and the maximum number of connections) and call a model service. You can adjust these parameters based on your actual needs to optimize concurrency performance and resource utilization.
Python SDK
The DashScope Python SDK supports connection reuse by passing a custom HTTP client (httpx.Client / httpx.AsyncClient). It provides two calling methods: asynchronous HTTP (coroutine-based) and synchronous HTTP.
Asynchronous HTTP calls
In asynchronous call scenarios, you can use httpx.AsyncClient with httpx.Limits to implement connection reuse. httpx.Limits supports configuring parameters such as connection limits:
Parameter | Description | Default value | Notes |
|---|---|---|---|
max_connections | Total connection limit | None (unlimited) | Controls the maximum number of connections. In high-concurrency scenarios, appropriately increasing this value can improve concurrency. |
max_keepalive_connections | Maximum number of keep-alive connections | None (unlimited) | Controls the number of connections kept active in the connection pool, preventing excessive pressure on a single server. |
keepalive_expiry | Keep-alive duration | 5.0 (seconds) | Timeout for idle keep-alive connections. Connections are closed automatically after this duration. |
Code example
Before running the code, configure the API key as an environment variable and install the latest SDK.
The following code example shows how to configure connection reuse in an asynchronous scenario and call a model service:
Synchronous HTTP calls
In synchronous call scenarios, you can use httpx.Client to implement connection reuse. Multiple requests within the same HTTP client reuse the underlying TCP connection, avoiding the overhead of repeatedly establishing connections.
Code example
Before running the code, configure the API key as an environment variable and install the latest SDK.
The following code example shows how to configure connection reuse in a synchronous scenario and call a model service:
Best practices
- Java SDK: Configure parameters such as
connectionPoolSizeandmaximumAsyncRequestsbased on your business concurrency to avoid an excessively high or low number of connections. - Python SDK: We recommend using the
withstatement to automatically manage the lifecycle of the HTTP client and ensure that resources are released correctly. - Choose the appropriate calling method: If your application uses an asynchronous architecture (such as asyncio or FastAPI), we recommend the asynchronous calling method; for a traditional synchronous architecture, use the synchronous calling method.
Error codes
If a model call fails (the Python SDK raises a DashScopeError exception, and HTTP calls return an error message), see Error codes to resolve the issue.