def test_not_setted_connection_pool_connection_keepalive( pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service, fake_time): pool = ClientPool( pingpong_thrift_service, pingpong_thrift_client.host, pingpong_thrift_client.port, name=pingpong_service_key, raise_empty=False, max_conn=3, connction_class=pingpong_thrift_client.pool.connction_class, ) assert pool.keepalive is None with pool.connection_ctx() as conn: now = datetime.datetime.now() assert conn.alive_until is None assert conn.test_connection() old_connection = conn fake_time.FAKE_TIME = now + datetime.timedelta(seconds=0.1) with pool.connection_ctx() as conn: assert conn is old_connection fake_time.FAKE_TIME = now + datetime.timedelta(days=100) assert old_connection.test_connection() with pool.connection_ctx() as conn: assert old_connection is conn
def test_connection_pool_generation(pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service, fake_datetime): pool = ClientPool( pingpong_thrift_service, pingpong_thrift_client.host, pingpong_thrift_client.port, name=pingpong_service_key, raise_empty=False, max_conn=3, connction_class=pingpong_thrift_client.pool.connction_class, ) c = pool.produce_client() assert c.pool_generation == pool.generation == 0 pool.clear() c2 = pool.produce_client() assert c2.pool_generation == pool.generation == 1 pool.put_back_connection(c) pool.put_back_connection(c2) for c in pool.connections: assert c.pool_generation == pool.generation
def test_api_call_context(pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service, fake_time): from thrift_connector.hooks import before_call, after_call mock_before_hook = Mock() mock_after_hook = Mock() before_call.register(mock_before_hook) after_call.register(mock_after_hook) pool = ClientPool( pingpong_thrift_service, pingpong_thrift_client.host, pingpong_thrift_client.port, name=pingpong_service_key, raise_empty=False, max_conn=3, connction_class=pingpong_thrift_client.pool.connction_class, ) pool.ping() # get one client manually, there should be one client in pool, # since there's only one call client = pool.get_client() assert client.test_connection() pool.put_back_connection(client) mock_before_hook.assert_called_with(pool, client, 'ping', fake_time.time()) mock_after_hook.assert_called_with(pool, client, 'ping', fake_time.time(), 0)
def test_set_timeout(pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service, fake_time): pool = ClientPool( pingpong_thrift_service, pingpong_thrift_client.host, pingpong_thrift_client.port, name=pingpong_service_key, raise_empty=False, max_conn=3, connction_class=pingpong_thrift_client.pool.connction_class, ) client = pool.get_client() client.set_client_timeout(0.5 * 1000) assert client.sleep(0.2) == 'good morning' with pytest.raises(socket.timeout) as e: client.sleep(1) assert 'timed out' in str(e.value) client.close() with pytest.raises(socket.timeout) as e: with pool.connection_ctx(timeout=1) as client: client.sleep(2) assert 'timed out' in str(e.value)
def __init__(self, thrift_host, thrift_port, sender_key): self.thrift_host = thrift_host self.thrift_port = thrift_port self.sender_key = sender_key self.connection_pool = ClientPool(service=_thrift_file.HermesService, host=self.thrift_host, port=self.thrift_port, max_conn=30, connction_class=ThriftPyCyClient, keepalive=1800) self.get_client_ctx = self.connection_pool.connection_ctx
def test_conn_close_hook(pingpong_thrift_client, pingpong_service_key, pingpong_thrift_service, fake_time): pool = ClientPool( pingpong_thrift_service, pingpong_thrift_client.host, pingpong_thrift_client.port, name=pingpong_service_key, raise_empty=False, max_conn=3, connction_class=pingpong_thrift_client.pool.connction_class, ) close_mock = Mock() pool.register_after_close_func(close_mock) client = pool.get_client() client.close() close_mock.assert_called_with(pool, client)
class ProxyClient(object): """代理服务客户端简单封装""" proxy = ClientPool(proxy_thrift.ProxyService, setting.PROXY_SERVICE_HOST, setting.PROXY_SERVICE_PORT, connection_class=ThriftPyCyClient) @classmethod def get_proxy(cls, url): """ 获取代理 :param url: 请求的url地址,需要根据schema和host返回一个合适的代理 :return: """ req = proxy_thrift.ProxyReq() req.url = url try: response = cls.proxy.get_proxy(req) return response except Exception as e: logging.exception(e) return None
def pingpong_thrift_client(request, pingpong_service_key, pingpong_thrift_service): port = random.randint(55536, 65536) port2 = random.randint(35536, 45536) config_path = "examples/gunicorn_config.py" gunicorn_server = subprocess.Popen([ "gunicorn_thrift", "examples.pingpong_app.app:app", "-c", config_path, "--bind", "0.0.0.0:%s" % port, "--bind", "0.0.0.0:%s" % port2, ]) def shutdown(): os.kill(gunicorn_server.pid, signal.SIGTERM) request.addfinalizer(shutdown) time.sleep(4) from thrift_connector import ClientPool, ThriftPyCyClient pool = ClientPool(pingpong_thrift_service, 'localhost', port, name=pingpong_service_key, connction_class=ThriftPyCyClient) return TestServerInfo('localhost', port, gunicorn_server, pool, pingpong_thrift_service, port2=port2)
# -*- coding: utf-8 -*- from __future__ import print_function import thriftpy from thrift_connector import ClientPool, ThriftPyCyClient service = thriftpy.load("pingpong_app/pingpong.thrift") pool = ClientPool(service.PingService, 'localhost', 8880, connection_class=ThriftPyCyClient) print("Sending Ping...") print("Receive:", pool.ping()) print("Winning the match...") print("Receive:", pool.win())
# -*- coding: utf-8 -*- import json import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import thriftpy2 as thriftpy from thrift_connector import ClientPool, ThriftPyCyClient from settings import PRO_DIR kol_thrift = thriftpy.load(os.path.join( PRO_DIR, "./dolphin/service/douyin/data/kol_thrift.thrift"), module_name="kol_thrift_thrift") connection_pool = ClientPool(kol_thrift.KolServer, '192.168.1.12', 7000, timeout=6, connection_class=ThriftPyCyClient) if __name__ == '__main__': data = connection_pool.fetch_sig_and_dytk('89852104754') print(data) for item in json.loads(data): print(item)
# -*- coding: utf-8 -*- from pingpong_app.pingpong_sdk.pingpong import PingService from thrift_connector import ClientPool, ThriftClient pool = ClientPool(PingService, 'localhost', 8880, connction_class=ThriftClient) print "Sending Ping..." print "Receive:", pool.ping() print "Winning the match..." print "Receive:", pool.win()
# -*- coding: utf-8 -*- import json import os import sys sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))) import thriftpy2 as thriftpy from thrift_connector import ClientPool, ThriftPyCyClient from settings import PRO_DIR kol_thrift = thriftpy.load(os.path.join( PRO_DIR, "./dolphin/service/douyin/data/kol_thrift.thrift"), module_name="kol_thrift_thrift") connection_pool = ClientPool(kol_thrift.KolServer, '192.168.1.94', 7000, connection_class=ThriftPyCyClient) if __name__ == '__main__': data = connection_pool.fetch_all_works('89852104754') for item in json.loads(data): print(item)