|
21 | 21 | from http.server import HTTPServer, BaseHTTPRequestHandler |
22 | 22 | from os import path |
23 | 23 | from urllib.parse import urlparse |
| 24 | + |
24 | 25 | import errno |
25 | 26 | import socket |
26 | 27 | import sys |
| 28 | +import time |
27 | 29 | import threading |
28 | 30 |
|
29 | 31 | if sys.platform == 'win32': |
@@ -85,6 +87,22 @@ def close(self): |
85 | 87 | def is_connected(self): |
86 | 88 | return self._socket is not None |
87 | 89 |
|
| 90 | + def check_connection(self, timeout): |
| 91 | + SLEEPTIME = 0.1 |
| 92 | + wait_time = 0.0 |
| 93 | + last_exception = None |
| 94 | + while True: |
| 95 | + try: |
| 96 | + if self.socket(): |
| 97 | + break |
| 98 | + except BackendError as ex: |
| 99 | + last_exception = ex # Ignore backed errors for some time. |
| 100 | + |
| 101 | + time.sleep(SLEEPTIME) |
| 102 | + wait_time += SLEEPTIME |
| 103 | + if wait_time > timeout: |
| 104 | + raise last_exception if last_exception else TimeoutError |
| 105 | + |
88 | 106 | def recv(self, max_length): |
89 | 107 | return self.socket().recv(max_length) |
90 | 108 |
|
@@ -114,6 +132,9 @@ def __init__(self, ipc_path): |
114 | 132 | def is_connected(self): |
115 | 133 | return True |
116 | 134 |
|
| 135 | + def check_connection(self, timeout): |
| 136 | + pass |
| 137 | + |
117 | 138 | def recv(self, max_length): |
118 | 139 | (err, data) = win32file.ReadFile(self.handle, max_length) |
119 | 140 | if err: |
@@ -222,6 +243,8 @@ def process(self, request): |
222 | 243 |
|
223 | 244 | def run(self): |
224 | 245 | self.conn = get_ipc_connector(self.backend_address) |
| 246 | + self.conn.check_connection(timeout=10.0) |
| 247 | + |
225 | 248 | print("JSON-RPC HTTP Proxy: {} -> {}".format( |
226 | 249 | self.backend_address, self.proxy_url), file=sys.stderr, flush=True) |
227 | 250 | self.serve_forever() |
|
0 commit comments