# aiohttp.client_exceptions.ClientConnectionError：无法连接到主机 example.com:443 ssl:default [连接被拒绝]

- **ID:** `python/aiohttp-client-connection-pool-exhausted`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

连接池达到限制或服务器因并发请求过多而拒绝连接，通常是因为 ClientSession 的连接器限制过低或连接未释放。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   connector = aiohttp.TCPConnector(limit=100, limit_per_host=20)
async with aiohttp.ClientSession(connector=connector) as session:
    # use session for multiple requests
   ```
2. **** (90% 成功率)
   ```
   for attempt in range(3):
    try:
        async with session.get(url) as resp:
            return await resp.text()
    except aiohttp.ClientConnectionError:
        if attempt == 2:
            raise
        await asyncio.sleep(2 ** attempt)
   ```

## 无效尝试

- **** — Higher limit can lead to resource exhaustion and doesn't address server-side refusal. (50% 失败率)
- **** — Creates many sessions, each with its own pool, causing overhead and still hitting OS limits. (70% 失败率)
