| 62 | |
| 63 | |
| 64 | class NpipeHTTPAdapter(BaseHTTPAdapter): |
| 65 | |
| 66 | __attrs__ = requests.adapters.HTTPAdapter.__attrs__ + ['npipe_path', |
| 67 | 'pools', |
| 68 | 'timeout', |
| 69 | 'max_pool_size'] |
| 70 | |
| 71 | def __init__(self, base_url, timeout=60, |
| 72 | pool_connections=constants.DEFAULT_NUM_POOLS, |
| 73 | max_pool_size=constants.DEFAULT_MAX_POOL_SIZE): |
| 74 | self.npipe_path = base_url.replace('npipe://', '') |
| 75 | self.timeout = timeout |
| 76 | self.max_pool_size = max_pool_size |
| 77 | self.pools = RecentlyUsedContainer( |
| 78 | pool_connections, dispose_func=lambda p: p.close() |
| 79 | ) |
| 80 | super().__init__() |
| 81 | |
| 82 | def get_connection(self, url, proxies=None): |
| 83 | with self.pools.lock: |
| 84 | pool = self.pools.get(url) |
| 85 | if pool: |
| 86 | return pool |
| 87 | |
| 88 | pool = NpipeHTTPConnectionPool( |
| 89 | self.npipe_path, self.timeout, |
| 90 | maxsize=self.max_pool_size |
| 91 | ) |
| 92 | self.pools[url] = pool |
| 93 | |
| 94 | return pool |
| 95 | |
| 96 | def request_url(self, request, proxies): |
| 97 | # The select_proxy utility in requests errors out when the provided URL |
| 98 | # doesn't have a hostname, like is the case when using a UNIX socket. |
| 99 | # Since proxies are an irrelevant notion in the case of UNIX sockets |
| 100 | # anyway, we simply return the path URL directly. |
| 101 | # See also: https://github.com/docker/docker-sdk-python/issues/811 |
| 102 | return request.path_url |