-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: loadbalance stream based on response #6122
Merged
JoanFM
merged 11 commits into
jina-ai:master
from
NarekA:fix-loadbalancer-forwarding-method
Dec 6, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
af64005
fix: loadbalance right method
NarekA 7932b0d
Merge branch 'master' into fix-loadbalancer-forwarding-method
NarekA 4b85741
docs: add comment for content type
NarekA 8292fcf
fix: use response content
NarekA f170efb
Merge branch 'master' into fix-loadbalancer-forwarding-method
JoanFM 42f444a
fix: use correct protocol
NarekA 7fbe42f
fix: user session.request
NarekA 676ca4c
fix: passing json
NarekA ac1febe
fix: always stream response
NarekA c94ea5f
fix: don't pass request headers
NarekA bc91179
fix: disable autodecompress
NarekA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,48 +157,37 @@ | |
try: | ||
async with aiohttp.ClientSession() as session: | ||
|
||
if request.method == 'GET': | ||
request_kwargs = {} | ||
try: | ||
payload = await request.json() | ||
if payload: | ||
request_kwargs['json'] = payload | ||
except Exception: | ||
self.logger.debug('No JSON payload found in request') | ||
|
||
async with session.get( | ||
url=target_url, **request_kwargs | ||
) as response: | ||
# Create a StreamResponse with the same headers and status as the target response | ||
stream_response = web.StreamResponse( | ||
status=response.status, | ||
headers=response.headers, | ||
) | ||
|
||
# Prepare the response to send headers | ||
await stream_response.prepare(request) | ||
|
||
# Stream the response from the target server to the client | ||
async for chunk in response.content.iter_any(): | ||
await stream_response.write(chunk) | ||
|
||
# Close the stream response once all chunks are sent | ||
await stream_response.write_eof() | ||
return stream_response | ||
|
||
elif request.method == 'POST': | ||
d = await request.read() | ||
import json | ||
|
||
async with session.post( | ||
url=target_url, json=json.loads(d.decode()) | ||
) as response: | ||
content = await response.read() | ||
return web.Response( | ||
body=content, | ||
status=response.status, | ||
content_type=response.content_type, | ||
) | ||
request_kwargs = {} | ||
try: | ||
payload = await request.json() | ||
if payload: | ||
request_kwargs['json'] = payload | ||
except Exception: | ||
self.logger.debug('No JSON payload found in request') | ||
|
||
async with session.request( | ||
request.method, | ||
url=target_url, | ||
auto_decompress=False, | ||
**request_kwargs, | ||
) as response: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NarekA why not directly use the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somehow I missed this, will fix. |
||
# Create a StreamResponse with the same headers and status as the target response | ||
stream_response = web.StreamResponse( | ||
status=response.status, | ||
headers=response.headers, | ||
) | ||
|
||
# Prepare the response to send headers | ||
await stream_response.prepare(request) | ||
|
||
# Stream the response from the target server to the client | ||
async for chunk in response.content.iter_any(): | ||
await stream_response.write(chunk) | ||
|
||
# Close the stream response once all chunks are sent | ||
await stream_response.write_eof() | ||
return stream_response | ||
|
||
except aiohttp.ClientError as e: | ||
return web.Response(text=f'Error: {str(e)}', status=500) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the original implementation, i have this idea:
it looks like the original logic was only to write a debug log which is not useful at all for production application. Can we just act as a pure proxy here for performance consideration? Something like:
@NarekA @JoanFM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, if I try to pass the content in any other way besides the json field, I get an error here. I've tried everything at this point, if you can get this to work, I am interested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what error do you see?