Exemplo n.º 1
0
 def invoke_async(self,
                  interface,
                  method,
                  content,
                  *,
                  spanctx,
                  callback=None,
                  timeout_ms=None,
                  **headers):
     """
     call callback if callback is a callable,
     otherwise return a future
     Callback should recv a bytes object as the only argument, which is the response pkg's content
     """
     header = SofaHeader.build_header(spanctx, interface, method, **headers)
     pkg = BoltRequest.new_request(header,
                                   content,
                                   timeout_ms=timeout_ms or -1)
     fut = asyncio.run_coroutine_threadsafe(self.invoke(pkg),
                                            loop=self._loop)
     if callable(callback):
         fut.add_done_callback(
             self.callback_wrapper(
                 callback, timeout_ms / 1000 if timeout_ms else None))
         return fut
     return fut
Exemplo n.º 2
0
    def _raw_invoke(self, interface, method_name, content, spanctx=None, target_app="", uid="",
                    timeout_ms=None, bolt_ptype=PTYPE.REQUEST, **sofa_headers_extra):

        """
        :param content:
        :param service:
        :param target_app:
        :param uid:
        :param rpc_trace_context: preserved, rpc_trace_context object, should be expanded as a dict like
                                  {'rpc_trace_context.traceId': 'xxxxx', ...}
        :param kwargs:
        """
        # FIXME spanctx might be None here
        logger.debug("Calling interface {}, spanctx: {}".format(interface, spanctx.baggage))
        header = SofaHeader.build_header(spanctx, interface, method_name, target_app=target_app, uid=uid,
                                         **sofa_headers_extra)
        p = BoltRequest.new_request(header, content, timeout_ms=timeout_ms or -1, ptype=bolt_ptype)
        for i in range(3):
            # try three times, to avoid connection failure
            try:
                conn = self._get_pool(interface).get_conn()
                conn.send(p.to_stream())
            except SocketValueError as e:
                logger.info("Call to interface {} failed, requiest_id: {}, "
                            "retried: {}, reason: {}".format(interface, p.request_id, i, e))
                continue
            except Exception as e:
                logger.error("Call to interface {} failed, requiest_id: {}, "
                             "retried: {}, reason: {}".format(interface, p.request_id, i, e))
                break
            else:
                break
        logger.debug("Called interface {}, request_id: {}".format(interface, p.request_id))
        return p.request_id, conn
Exemplo n.º 3
0
 def _raw_invoke(self,
                 interface,
                 method_name,
                 content,
                 spanctx=None,
                 target_app="",
                 uid="",
                 timeout_ms=None,
                 bolt_ptype=PTYPE.REQUEST,
                 **sofa_headers_extra):
     """
     :param content:
     :param service:
     :param target_app:
     :param uid:
     :param rpc_trace_context: preserved, rpc_trace_context object, should be expanded as a dict like
                               {'rpc_trace_context.traceId': 'xxxxx', ...}
     :param kwargs:
     """
     header = SofaHeader.build_header(spanctx,
                                      interface,
                                      method_name,
                                      target_app=target_app,
                                      uid=uid,
                                      **sofa_headers_extra)
     p = BoltRequest.new_request(header,
                                 content,
                                 timeout_ms=timeout_ms or -1,
                                 ptype=bolt_ptype)
     conn = self._get_pool(interface).get_conn()
     conn.send(p.to_stream())
     return p.request_id, conn
Exemplo n.º 4
0
 def invoke_oneway(self, interface, method, content, *, spanctx, **headers):
     header = SofaHeader.build_header(spanctx, interface, method, **headers)
     pkg = BoltRequest.new_request(
         header,
         content,
         timeout_ms=-1,
         codec=self._get_serialize_protocol(interface))
     asyncio.run_coroutine_threadsafe(self.invoke(pkg), loop=self._loop)
Exemplo n.º 5
0
 def invoke_sync(self, interface, method, content, *, spanctx, timeout_ms, **headers):
     """blocking call to interface, returns responsepkg.content(as bytes)"""
     assert isinstance(timeout_ms, (int, float))
     header = SofaHeader.build_header(spanctx, interface, method, **headers)
     pkg = BoltRequest.new_request(header, content, timeout_ms=timeout_ms)
     fut = asyncio.run_coroutine_threadsafe(self.invoke(pkg), loop=self._loop)
     try:
         ret = fut.result(timeout=timeout_ms / 1000)
     except (TimeoutError, CancelledError) as e:
         logger.error("call to [{}:{}] timeout/cancelled. {}".format(interface, method, e))
         raise
     return ret.content