| 118 | |
| 119 | |
| 120 | class SSHConnectionPool(urllib3.connectionpool.HTTPConnectionPool): |
| 121 | scheme = 'ssh' |
| 122 | |
| 123 | def __init__(self, ssh_client=None, timeout=60, maxsize=10, host=None): |
| 124 | super().__init__( |
| 125 | 'localhost', timeout=timeout, maxsize=maxsize |
| 126 | ) |
| 127 | self.ssh_transport = None |
| 128 | self.timeout = timeout |
| 129 | if ssh_client: |
| 130 | self.ssh_transport = ssh_client.get_transport() |
| 131 | self.ssh_host = host |
| 132 | |
| 133 | def _new_conn(self): |
| 134 | return SSHConnection(self.ssh_transport, self.timeout, self.ssh_host) |
| 135 | |
| 136 | # When re-using connections, urllib3 calls fileno() on our |
| 137 | # SSH channel instance, quickly overloading our fd limit. To avoid this, |
| 138 | # we override _get_conn |
| 139 | def _get_conn(self, timeout): |
| 140 | conn = None |
| 141 | try: |
| 142 | conn = self.pool.get(block=self.block, timeout=timeout) |
| 143 | |
| 144 | except AttributeError as ae: # self.pool is None |
| 145 | raise urllib3.exceptions.ClosedPoolError(self, "Pool is closed.") from ae |
| 146 | |
| 147 | except queue.Empty: |
| 148 | if self.block: |
| 149 | raise urllib3.exceptions.EmptyPoolError( |
| 150 | self, |
| 151 | "Pool reached maximum size and no more " |
| 152 | "connections are allowed." |
| 153 | ) from None |
| 154 | # Oh well, we'll create a new connection then |
| 155 | |
| 156 | return conn or self._new_conn() |
| 157 | |
| 158 | |
| 159 | class SSHHTTPAdapter(BaseHTTPAdapter): |