def test_header(self): h = SofaHeader(keya='key1', keyabcxcs='key2') b = h.to_bytes() self.assertEqual(len(h), len(b)) print(b) h_recover = SofaHeader.from_bytes(b) print(h_recover) self.assertEqual(h, h_recover)
def handle(self): try: fixed_header_bs = self._readexactly(BoltRequest.bolt_header_size()) header = BoltRequest.bolt_header_from_stream(fixed_header_bs) call_type = header['ptype'] cmdcode = header['cmdcode'] class_name = self._readexactly(header['class_len']) bs = self._readexactly(header['header_len']) sofa_header = SofaHeader.from_bytes(bs) body = self._readexactly(header['content_len']) if self.server.ready: self.server.ready.set() request_id = header['request_id'] if cmdcode == CMDCODE.HEARTBEAT: self.wfile.write(HeartbeatResponse.response_to(request_id).to_stream()) self.wfile.flush() return if cmdcode == CMDCODE.RESPONSE: raise ClientError("wrong cmdcode:[{}]".format(cmdcode)) if class_name != "com.alipay.sofa.rpc.core.request.SofaRequest".encode(): raise ClientError("wrong class_name:[{}]".format(class_name)) service = sofa_header.get('sofa_head_target_service') or sofa_header.get('service') if not service: self.wfile.write(FailResponse.response_to(request_id, RESPSTATUS.CLIENT_SEND_ERROR).to_stream()) self.wfile.flush() logger.error("Missing service name in sofa header [{}]".format(sofa_header)) return method = sofa_header.get('sofa_head_method_name') if not method: self.wfile.write(FailResponse.response_to(request_id, RESPSTATUS.CLIENT_SEND_ERROR).to_stream()) self.wfile.flush() logger.error("Missing method name in sofa header [{}]".format(sofa_header)) return spanctx = tracer.extract(opentracing.Format.TEXT_MAP, sofa_header) ret = self.handle_request(spanctx, service, method, body) self.wfile.write(ret) self.wfile.flush() except OSError as e: if e.errno != ECONNRESET: raise except Exception as e: logger.error(traceback.format_exc())
async def _handler_connection(self, reader, writer): """ Full duplex model Only recv request here, run a new coro to process and send back response in same connection. """ logger.info("connection created.") first_req = True while True: try: try: fixed_header_bs = await reader.readexactly( BoltRequest.bolt_header_size()) except asyncio.IncompleteReadError: if first_req: # just connected, do nothing. most likely L4 health checks from mosn/upstream await self._close_writer(writer) break # break the loop raise first_req = False logger.debug( "received bolt header({})".format(fixed_header_bs)) header = BoltRequest.bolt_header_from_stream(fixed_header_bs) call_type = header['ptype'] cmdcode = header['cmdcode'] class_name = await reader.readexactly(header['class_len']) logger.debug("received classname({})".format(class_name)) bs = await reader.readexactly(header['header_len']) logger.debug("received sofa header({})".format(bs)) sofa_header = SofaHeader.from_bytes(bs) body = await reader.readexactly(header['content_len']) logger.debug("received sofa body({})".format(body)) if cmdcode == CMDCODE.HEARTBEAT: logger.info("received heartbeat, request_id={}".format( header['request_id'])) asyncio.ensure_future( self._write_msg( writer, HeartbeatResponse.response_to( header['request_id']).to_stream())) continue if cmdcode == CMDCODE.RESPONSE: raise ClientError("wrong cmdcode:[{}]".format(cmdcode)) if class_name != "com.alipay.sofa.rpc.core.request.SofaRequest".encode( ): raise ClientError( "wrong class_name:[{}]".format(class_name)) logger.debug("dispatching request[{}]".format( header['request_id'])) asyncio.ensure_future( self._dispatch(call_type, header['request_id'], sofa_header, body, timeout_ms=header['timeout'], codec=header['codec'], writer=writer)) except ClientError as e: logger.error(str(e) + " returning CLIENT_SEND_ERROR") await self._write_msg( writer, FailResponse.response_to( header['request_id'], RESPSTATUS.CLIENT_SEND_ERROR).to_stream()) continue except ProtocolError as e: logger.error(str(e) + " returning CODEC_EXCEPTION") await self._write_msg( writer, FailResponse.response_to( header['request_id'], RESPSTATUS.CODEC_EXCEPTION).to_stream()) continue except EOFError as e: logger.warning("Connection closed by remote: {}".format(e)) try: await writer.drain( ) # clean the buffer, avoid backpressure writer.write( FailResponse.response_to( header['request_id'], RESPSTATUS.CONNECTION_CLOSED).to_stream()) except: pass await self._close_writer(writer) break except Exception: logger.error("Unknow exception, close connection") logger.error(traceback.format_exc()) try: await writer.drain( ) # clean the buffer, avoid backpressure writer.write( FailResponse.response_to( header['request_id'], RESPSTATUS.UNKNOWN).to_stream()) except: pass await self._close_writer(writer) break
def _handler_connection(self, reader, writer): """ Full duplex model Only recv request here, run a new coro to process and send back response in same connection. """ first_req = True while True: try: try: fixed_header_bs = yield from reader.readexactly( BoltRequest.bolt_header_size()) except asyncio.streams.IncompleteReadError: if first_req: raise first_req = False header = BoltRequest.bolt_header_from_stream(fixed_header_bs) call_type = header['ptype'] cmdcode = header['cmdcode'] class_name = yield from reader.readexactly(header['class_len']) bs = yield from reader.readexactly(header['header_len']) sofa_header = SofaHeader.from_bytes(bs) body = yield from reader.readexactly(header['content_len']) if cmdcode == CMDCODE.HEARTBEAT: asyncio.ensure_future( self._write_msg( writer, HeartbeatResponse.response_to( header['request_id']).to_stream())) continue if cmdcode == CMDCODE.RESPONSE: raise ClientError("wrong cmdcode:[{}]".format(cmdcode)) if class_name != "com.alipay.sofa.rpc.core.request.SofaRequest".encode( ): raise ClientError( "wrong class_name:[{}]".format(class_name)) asyncio.ensure_future( self._dispatch(call_type, header['request_id'], sofa_header, body, timeout_ms=header['timeout'], writer=writer)) except ClientError as e: logger.error(str(e)) yield from self._write_msg( writer, FailResponse.response_to( header['request_id'], RESPSTATUS.CLIENT_SEND_ERROR).to_stream()) continue except ProtocolError as e: logger.error(str(e)) yield from self._write_msg( writer, FailResponse.response_to( header['request_id'], RESPSTATUS.CODEC_EXCEPTION).to_stream()) continue except EOFError as e: logger.info("Connection closed: {}".format(e)) try: yield from writer.drain( ) # clean the buffer, avoid backpressure writer.write( FailResponse.response_to( header['request_id'], RESPSTATUS.CONNECTION_CLOSED).to_stream()) except: pass try: writer.write_eof() yield from writer.drain() except: pass writer.close() break except Exception: logger.error(traceback.format_exc()) try: yield from writer.drain( ) # clean the buffer, avoid backpressure writer.write( FailResponse.response_to( header['request_id'], RESPSTATUS.UNKNOWN).to_stream()) except: pass try: writer.write_eof() yield from writer.drain() except: pass writer.close() break