Skip to content

Commit d35a49e

Browse files
chfastaxic
authored andcommitted
jsonprcproxy: Wait for backend connection on startup
1 parent 9bfee44 commit d35a49e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

scripts/jsonrpcproxy.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
from http.server import HTTPServer, BaseHTTPRequestHandler
2222
from os import path
2323
from urllib.parse import urlparse
24+
2425
import errno
2526
import socket
2627
import sys
28+
import time
2729
import threading
2830

2931
if sys.platform == 'win32':
@@ -85,6 +87,22 @@ def close(self):
8587
def is_connected(self):
8688
return self._socket is not None
8789

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+
88106
def recv(self, max_length):
89107
return self.socket().recv(max_length)
90108

@@ -114,6 +132,9 @@ def __init__(self, ipc_path):
114132
def is_connected(self):
115133
return True
116134

135+
def check_connection(self, timeout):
136+
pass
137+
117138
def recv(self, max_length):
118139
(err, data) = win32file.ReadFile(self.handle, max_length)
119140
if err:
@@ -222,6 +243,8 @@ def process(self, request):
222243

223244
def run(self):
224245
self.conn = get_ipc_connector(self.backend_address)
246+
self.conn.check_connection(timeout=10.0)
247+
225248
print("JSON-RPC HTTP Proxy: {} -> {}".format(
226249
self.backend_address, self.proxy_url), file=sys.stderr, flush=True)
227250
self.serve_forever()

0 commit comments

Comments
 (0)