Skip to content

Commit 5d911bd

Browse files
ganisbackyuvipanda
authored andcommitted
address comments
1 parent 53c5433 commit 5d911bd

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

jupyter_server_proxy/handlers.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,24 @@ async def proxy(self, host, port, proxied_path):
366366
body = b""
367367
else:
368368
body = None
369-
accept_type = self.request.headers.get('Accept')
370-
if accept_type == 'text/event-stream':
371-
return await self._proxy_progressive(host, port, proxied_path, body)
369+
if self.unix_socket is not None:
370+
# Port points to a Unix domain socket
371+
self.log.debug("Making client for Unix socket %r", self.unix_socket)
372+
assert host == "localhost", "Unix sockets only possible on localhost"
373+
client = SimpleAsyncHTTPClient(
374+
force_instance=True, resolver=UnixResolver(self.unix_socket)
375+
)
376+
else:
377+
client = httpclient.AsyncHTTPClient()
378+
# check if the request is stream request
379+
proxy_streaming = self.request.headers.get('Accept')
380+
if proxy_streaming == 'text/event-stream':
381+
return await self._proxy_progressive(host, port, proxied_path, body, client)
372382
else:
373-
return await self._proxy_normal(host, port, proxied_path, body)
383+
return await self._proxy_buffered(host, port, proxied_path, body, client)
374384

375-
async def _proxy_progressive(self, host, port, proxied_path, body):
385+
async def _proxy_progressive(self, host, port, proxied_path, body, client):
376386
# Proxy in progressive flush mode, whenever chunks are received. Potentially slower but get results quicker for voila
377-
378-
client = httpclient.AsyncHTTPClient()
379-
380387
# Set up handlers so we can progressively flush result
381388

382389
headers_raw = []
@@ -404,6 +411,8 @@ def header_callback(line):
404411
headers_raw.append(line)
405412

406413
def streaming_callback(chunk):
414+
# record activity at start and end of requests
415+
self._record_activity()
407416
# Do this here, not in header_callback so we can be sure headers are out of the way first
408417
dump_headers(headers_raw) # array will be empty if this was already called before
409418
self.write(chunk)
@@ -429,9 +438,6 @@ def streaming_callback(chunk):
429438
else:
430439
raise
431440

432-
# record activity at start and end of requests
433-
self._record_activity()
434-
435441
# For all non http errors...
436442
if response.error and type(response.error) is not httpclient.HTTPError:
437443
self.set_status(500)
@@ -444,16 +450,7 @@ def streaming_callback(chunk):
444450
if response.body: # Likewise, should already be chunked out and flushed
445451
self.write(response.body)
446452

447-
async def _proxy_normal(self, host, port, proxied_path, body):
448-
if self.unix_socket is not None:
449-
# Port points to a Unix domain socket
450-
self.log.debug("Making client for Unix socket %r", self.unix_socket)
451-
assert host == "localhost", "Unix sockets only possible on localhost"
452-
client = SimpleAsyncHTTPClient(
453-
force_instance=True, resolver=UnixResolver(self.unix_socket)
454-
)
455-
else:
456-
client = httpclient.AsyncHTTPClient()
453+
async def _proxy_buffered(self, host, port, proxied_path, body, client):
457454

458455
req = self._build_proxy_request(host, port, proxied_path, body)
459456

0 commit comments

Comments
 (0)