Connection pool exhausted error in Ti.Network.HTTPClient
0 reputation · 04 Apr 2023, 10:54 UTC
0 reputation · 04 Apr 2023, 10:54 UTC
Determine whether developers should explicitly close Ti.Network.HTTPClient instances or rely on garbage‑collection‑driven cleanup to keep the internal keep‑alive connection pool from being exhausted.
The pool defaults to five connections per host, and the framework does not document when a connection is returned to the pool after a request finishes. Observations show that omitting client.close() leads to exhaustion after a few rapid requests, while the timing of GC‑based release differs between iOS and Android runtimes.
32775 reputation · 04 Apr 2023, 19:10 UTC
The framework maintains a keep-alive connection pool that defaults to five connections per host. When a client is instantiated but not closed, the underlying socket remains active or tied to the pool until the runtime reclaims the object. Because rapid requests can instantiate new clients faster than the GC can trigger, the pool quickly reaches its limit, causing subsequent requests to hang or fail with exhaustion errors.
The only supported approach is to manage the lifecycle of the client explicitly. This ensures the socket is returned to the pool or terminated immediately regardless of whether the request succeeds or fails.
finally block or ensure client.close() is called in both onload and onerror handlers.HTTPClient instance rather than creating a new one per request.var client = new Ti.Network.HTTPClient();
client.get('https://api.example.com/data', function(response) {
// Handle response
client.close();
}, function(error) {
// Handle error
client.close();
});
While you could disable keep-alive via headers (Connection: close) as a workaround, this introduces significant overhead due to repeated TCP/TLS handshakes. To verify the fix, use a network proxy like Charles or mitmproxy to observe that connections are closed immediately after the callback execution.
Note: To provide more specific tuning parameters, please specify the SDK version you are using, as internal pool behavior can vary between versions.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.