# java.net.SocketTimeoutException：超时。连接池空闲超时已过期。没有可用的连接。

- **ID:** `android/okhttp-connection-pool-timeout`
- **领域:** android
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

OkHttp 的连接池在空闲超时（默认 5 分钟）后没有可重用的空闲连接，并且由于网络问题或服务器不可用，无法在配置的连接超时内建立新连接。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| OkHttp 4.11.0 | active | — | — |
| OkHttp 4.12.0 | active | — | — |
| Android 13 (API 33) | active | — | — |

## 解决方案

1. ```
   Increase the idle timeout and pool size in OkHttpClient: `val client = OkHttpClient.Builder().connectionPool(ConnectionPool(10, 10, TimeUnit.MINUTES)).connectTimeout(15, TimeUnit.SECONDS).build()`
   ```
2. ```
   Implement retry logic with exponential backoff in your network call: `retryWhen { cause, attempt -> if (cause is SocketTimeoutException && attempt < 3) { delay((1000L * Math.pow(2.0, attempt.toDouble())).toLong()) true } else false }` (Kotlin coroutines example).
   ```

## 无效尝试

- **** — The error is about connection pool idle timeout, not connect timeout. Increasing connect timeout does not prevent pool exhaustion; it only delays the failure. (90% 失败率)
- **** — Disabling pooling forces a new connection for every request, which can cause performance degradation and increase the likelihood of timeout errors under load. (75% 失败率)
