コード例 #1
0
ファイル: loggers.py プロジェクト: tiras-j/yelp_clog
    def __init__(self,
                 host,
                 port,
                 retry_interval,
                 report_status=None,
                 logging_timeout=None):
        # set up thrift and scribe objects
        timeout = logging_timeout if logging_timeout is not None else config.scribe_logging_timeout
        self.socket = thriftpy.transport.socket.TSocket(
            six.text_type(host), int(port))
        if timeout:
            self.socket.set_timeout(timeout)

        self.transport = TFramedTransportFactory().get_transport(self.socket)
        protocol = TBinaryProtocolFactory(strict_read=False).get_protocol(
            self.transport)
        self.client = TClient(scribe_thrift.scribe, protocol)

        # our own bookkeeping for connection
        self.connected = False  # whether or not we think we're currently connected to the scribe server
        self.last_connect_time = 0  # last time we got disconnected or failed to reconnect

        self.retry_interval = retry_interval
        self.report_status = report_status or get_default_reporter()
        self.__lock = threading.RLock()
        self._birth_pid = os.getpid()
コード例 #2
0
def make_client(service,
                host="localhost",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                timeout=None,
                cafile=None,
                ssl_context=None,
                certfile=None,
                keyfile=None):
    if unix_socket:
        socket = TSocket(unix_socket=unix_socket)
        if certfile:
            warnings.warn("SSL only works with host:port, not unix_socket.")
    elif host and port:
        if cafile or ssl_context:
            socket = TSSLSocket(host,
                                port,
                                socket_timeout=timeout,
                                cafile=cafile,
                                certfile=certfile,
                                keyfile=keyfile,
                                ssl_context=ssl_context)
        else:
            socket = TSocket(host, port, socket_timeout=timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    transport = trans_factory.get_transport(socket)
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)
コード例 #3
0
def client_context(service,
                   host="localhost",
                   port=9090,
                   unix_socket=None,
                   proto_factory=TBinaryProtocolFactory(),
                   trans_factory=TBufferedTransportFactory(),
                   timeout=None):
    if unix_socket:
        socket = TSocket(unix_socket=unix_socket)
    elif host and port:
        socket = TSocket(host, port)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    if timeout:
        socket.set_timeout(timeout)

    try:

        transport = trans_factory.get_transport(socket)
        protocol = proto_factory.get_protocol(transport)
        transport.open()
        yield TClient(service, protocol)

    finally:
        transport.close()
コード例 #4
0
    def _refresh_thrift_client(self):
        """Refresh the Thrift socket, transport, and client."""
        socket = TSocket(host=self.host, port=self.port, socket_timeout=self.timeout)

        self.transport = self._transport_class(socket)
        protocol = self._protocol_class(self.transport, decode_response=False)
        self.client = TClient(Hbase, protocol)
コード例 #5
0
 def get_new_client(self):
     """
     轮询在每个ip:port的连接池中获取连接(线程安全)
     从当前队列右侧取出ip:port信息,获取client
     将连接池对象放回到当前队列的左侧
     请求或连接超时时间,默认30秒
     :return:
     """
     with self.lock:
         if self.pool_size < self.maxActive:
             try:
                 ip = self.load_balance_queue.pop()
             except IndexError:
                 raise CTECThriftClientError('没有可用的服务提供者列表!')
             if ip:
                 self.load_balance_queue.appendleft(ip)
                 # 创建新的thrift client
                 t_socket = TSocket(ip.split(':')[0], int(ip.split(':')[1]),
                                    socket_timeout=1000 * self.socket_timeout)
                 proto_factory = TBinaryProtocolFactory()
                 trans_factory = TBufferedTransportFactory()
                 transport = trans_factory.get_transport(t_socket)
                 protocol = proto_factory.get_protocol(transport)
                 transport.open()
                 client = TClient(self.service, protocol)
                 self.pool_size += 1
             return client
         else:
             return None
コード例 #6
0
ファイル: client.py プロジェクト: btdlin/myPyUtils
 def __create_client(self):
     socket = TSocket(self.__host,
                      self.__port,
                      socket_timeout=self.__timeout)
     self.__transport = TCyFramedTransportFactory().get_transport(socket)
     protocol = TCyBinaryProtocolFactory().get_protocol(self.__transport)
     self.__client = TClient(recs_thrift.RecommendationsService, protocol)
コード例 #7
0
ファイル: rpc.py プロジェクト: pawl-rs/thriftpy
def client_context(service, host="localhost", port=9090, unix_socket=None,
                   proto_factory=TBinaryProtocolFactory(),
                   trans_factory=TBufferedTransportFactory(),
                   timeout=None, socket_timeout=3000, connect_timeout=3000,
                   cafile=None, ssl_context=None, certfile=None, keyfile=None):
    if timeout:
        warnings.warn("`timeout` deprecated, use `socket_timeout` and "
                      "`connect_timeout` instead.")
        socket_timeout = connect_timeout = timeout

    if unix_socket:
        socket = TSocket(unix_socket=unix_socket,
                         connect_timeout=connect_timeout,
                         socket_timeout=socket_timeout)
    elif host and port:
        socket = TSocket(host, port,
                         connect_timeout=connect_timeout,
                         socket_timeout=socket_timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    try:
        transport = trans_factory.get_transport(socket)
        protocol = proto_factory.get_protocol(transport)
        transport.open()
        yield TClient(service, protocol)

    finally:
        transport.close()
コード例 #8
0
 def __getattr__(self, item):
     socket = TSocket(self.host, self.port)
     socket.set_timeout(self.timeout)
     transport = TRANS_FACTORY().get_transport(socket)
     protocol = PROTO_FACTORY().get_protocol(transport)
     client = TClient(self.service, protocol)
     attr = getattr(client, item)
     return _wrapper_api(attr, transport)
コード例 #9
0
 def get_tclient(self, service, protocol):
     if self.tracking is True:
         from thriftpy.contrib.tracking import TTrackedClient
         client = TTrackedClient(self.tracker_factory, service, protocol)
     else:
         from thriftpy.thrift import TClient
         client = TClient(service, protocol)
     return client
コード例 #10
0
def client_context(service, host, port,
                   proto_factory=TBinaryProtocolFactory()):
    try:
        transport = TBufferedTransport(TSocket(host, port))
        protocol = proto_factory.get_protocol(transport)
        transport.open()
        yield TClient(service, protocol)
    finally:
        transport.close()
コード例 #11
0
def make_client(service, host, port,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                timeout=None):
    socket = TSocket(host, port)
    if timeout:
        socket.set_timeout(timeout)
    transport = trans_factory.get_transport(socket)
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)
コード例 #12
0
    def open(self):
        if self.transport and self.transport.is_open():
            logger.debug("transport was opened.")
            return
        logger.debug("new transport...")
        socket = TSocket(host=self.host, port=self.port, socket_timeout=self.timeout)
        self.transport = self.transport_class(socket)
        self.protocol = self.protocol_class(self.transport)
        self.client = TClient(hbase_thrift.Hbase, self.protocol)

        self.transport.open()
コード例 #13
0
def make_client(service,
                host,
                port,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TFramedTransportFactory()):

    socket = TNonBlockingSocket(host, port)
    transport = trans_factory.get_transport(socket)
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)
コード例 #14
0
 def __init__(self, service, socket_config, service_name=None):
     trans_socket = TSocket(**socket_config)
     self.__transport = TBufferedTransportFactory()\
         .get_transport(trans_socket)
     if service_name:
         protocol_factory = TMultiplexedProtocolFactory(
             TBinaryProtocolFactory(), service_name)
     else:
         protocol_factory = TBinaryProtocolFactory()
     protocol = protocol_factory.get_protocol(self.__transport)
     self.__client = TClient(service, protocol)
     self.__is_open = False
コード例 #15
0
    def _refresh_thrift_client(self):
        """Refresh the Thrift socket, transport, and client."""
        socket = TSocket(self.host, self.port)
        if self.timeout is not None:
            socket.set_timeout(self.timeout)

        self.transport = self._transport_class(socket)
        if self.use_kerberos:
            self.transport = TSaslClientTransport(self.transport, self.host,
                                                  self.sasl_service_name)
        protocol = self._protocol_class(self.transport, decode_response=False)
        self.client = TClient(Hbase, protocol)
コード例 #16
0
def client_context(service, host, port,
                   proto_factory=TBinaryProtocolFactory(),
                   trans_factory=TBufferedTransportFactory(),
                   timeout=None):
    try:
        socket = TSocket(host, port)
        if timeout:
            socket.set_timeout(timeout)
        transport = trans_factory.get_transport(socket)
        protocol = proto_factory.get_protocol(transport)
        transport.open()
        yield TClient(service, protocol)
    finally:
        transport.close()
コード例 #17
0
def make_client(service, host="localhost", port=9090, unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                timeout=None):
    if unix_socket:
        socket = TSocket(unix_socket=unix_socket)
    elif host and port:
        socket = TSocket(host, port, socket_timeout=timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    transport = trans_factory.get_transport(socket)
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)
コード例 #18
0
ファイル: http.py プロジェクト: tubular/thriftpy
def make_client(service,
                host,
                port,
                path='',
                scheme='http',
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                ssl_context_factory=None,
                timeout=DEFAULT_HTTP_CLIENT_TIMEOUT_MS):
    uri = HTTP_URI.format(scheme=scheme, host=host, port=port, path=path)
    http_socket = THttpClient(uri, timeout, ssl_context_factory)
    transport = trans_factory.get_transport(http_socket)
    iprot = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, iprot)
コード例 #19
0
    def _refresh_thrift_client(self):
        """Refresh the Thrift socket, transport, and client."""
        socket = TSocket(host=self.host,
                         port=self.port,
                         socket_timeout=self.timeout)

        self.transport = self._transport_class(socket)
        if self.use_kerberos:
            self.transport = TSaslClientTransport(self.transport, self.host,
                                                  self.sasl_service_name)
            sasl_auth = 'GSSAPI'

            def sasl_factory():
                sasl_client = sasl.Client()
                sasl_client.setAttr('host', self.host)
                sasl_client.setAttr('service', self.sasl_service_name)
                sasl_client.init()
                return sasl_client

            self.transport = TSaslClientTransport(sasl_factory, sasl_auth,
                                                  socket)
        protocol = self._protocol_class(self.transport, decode_response=False)
        self.client = TClient(Hbase, protocol)
コード例 #20
0
 def get_tclient(self, service, protocol):
     from thriftpy.thrift import TClient
     return TClient(service, protocol)
コード例 #21
0
def make_client(service, host, port, proto_factory=TBinaryProtocolFactory()):
    transport = TBufferedTransport(TSocket(host, port))
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)