Repository: jina-ai/jina
File: jina/serve/runtimes/gateway/request_handling.py (Python)

Code before:
                no_debug_endpoints=no_debug_endpoints,
                no_crud_endpoints=no_crud_endpoints,
                expose_endpoints=expose_endpoints,
                expose_graphql_endpoint=expose_graphql_endpoint,
                cors=cors,
                logger=self.logger,
                tracing=tracing,
                tracer_provider=tracer_provider,
            )
        )

    async def _load_balance(self, request):
        import aiohttp
        from aiohttp import web

        target_server = next(self.load_balancer_servers)
        target_url = f'{target_server}{request.path_qs}'

        try:
            async with aiohttp.ClientSession() as session:

                request_kwargs = {
                    'headers': request.headers,
                    'params': request.query,
                }
                try:
                    payload = await request.read()
                    if payload:
                        request_kwargs['json'] = json.loads(payload.decode())
                except Exception:
                    self.logger.debug('No JSON payload found in request')

                async with session.request(
                    request.method, target_url, **request_kwargs
                ) as response:
                    # Looking for application/octet-stream, text/event-stream, text/stream
                    if response.content_type.endswith('stream'):

                        # 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)


Review comment (bug):
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:
```
async with session.request(request.method, data=request.iter_any(), **request_kwargs) as response:
    ....
```

@NarekA @JoanFM 

Code after:
                expose_graphql_endpoint=expose_graphql_endpoint,
                cors=cors,
                logger=self.logger,
                tracing=tracing,
                tracer_provider=tracer_provider,
            )
        )

    async def _load_balance(self, request):
        import aiohttp
        from aiohttp import web

        target_server = next(self.load_balancer_servers)
        target_url = f'{target_server}{request.path_qs}'

        try:
            async with aiohttp.ClientSession() as session:

                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:
                    # 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)