def __init__( self, endpoint: config.EndpointConfiguration, size: int = 10, max_age: int = 120, timeout: int = 1, max_connection_attempts: Optional[int] = None, max_retries: Optional[int] = None, protocol_factory: TProtocolFactory = THeaderProtocol.THeaderProtocolFactory(), ): if max_connection_attempts and max_retries: raise Exception("do not mix max_retries and max_connection_attempts") if max_retries: warn_deprecated( "ThriftConnectionPool's max_retries is now named max_connection_attempts" ) max_connection_attempts = max_retries elif not max_connection_attempts: max_connection_attempts = 3 self.endpoint = endpoint self.max_age = max_age self.retry_policy = RetryPolicy.new(attempts=max_connection_attempts) self.timeout = timeout self.protocol_factory = protocol_factory self.size = size self.pool: ProtocolPool = queue.LifoQueue() for _ in range(size): self.pool.put(None)
class HeaderAcceleratedCompactTest(AbstractTest): protocol_factory = THeaderProtocol.THeaderProtocolFactory( True, [ THeaderTransport.HEADERS_CLIENT_TYPE, THeaderTransport.FRAMED_DEPRECATED, THeaderTransport.UNFRAMED_DEPRECATED, THeaderTransport.HTTP_CLIENT_TYPE ])
class HeaderAcceleratedCompactTest(AbstractTest): protocol_factory = THeaderProtocol.THeaderProtocolFactory( True, [CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED, CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER] )
class HeaderBase(AbstractTest): protocol_factory = THeaderProtocol.THeaderProtocolFactory( True, [ CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED, CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER, ], )
def __init__(self, endpoint, size=10, max_age=120, timeout=1, max_retries=3, protocol_factory=THeaderProtocol.THeaderProtocolFactory()): self.endpoint = endpoint self.max_age = max_age self.retry_policy = RetryPolicy.new(attempts=max_retries) self.timeout = timeout self.protocol_factory = protocol_factory self.pool = queue.LifoQueue() for _ in range(size): self.pool.put(None)
for _iteration in range(1, numberOfFlaps + 1): # Ignore the SIGHUP signal to maintain session when the server port # is turned down. old_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) self.log.debug("Flap iteration {}".format(_iteration)) self.check_output(command_for_interface_down) time.sleep(sleepDuration) self.check_output(command_for_interface_up) # Change the SIGHUP settings back to original value. signal.signal(signal.SIGHUP, old_handler) if __name__ == '__main__': handler = TestServer() transport = TSocket.TServerSocket(DEFAULT_PORT) tfactory = TTransport.TBufferedTransportFactory() pfactory = THeaderProtocol.THeaderProtocolFactory() server = TServer.TSimpleServer(handler, transport, tfactory, pfactory) # You could do one of these for a multithreaded server # server = TServer.TThreadedServer(handler, transport, tfactory, pfactory) print('Starting the server...') server.serve() print('Done.')
if options.size is not None: kwargs.setdefault("size", options.size) if options.max_age is not None: kwargs.setdefault("max_age", options.max_age.total_seconds()) if options.timeout is not None: kwargs.setdefault("timeout", options.timeout.total_seconds()) if options.max_connection_attempts is not None: kwargs.setdefault("max_connection_attempts", options.max_connection_attempts) if options.max_retries is not None: raise Exception("max_retries was renamed to max_connection_attempts") return ThriftConnectionPool(endpoint=options.endpoint, **kwargs) _DEFAULT_PROTOCOL_FACTORY = THeaderProtocol.THeaderProtocolFactory( default_protocol=THeaderProtocol.THeaderSubprotocolID.COMPACT, ) class ThriftConnectionPool: """A pool that maintains a queue of open Thrift connections. :param endpoint: The remote address of the Thrift service. :param size: The maximum number of connections that can be open before new attempts to open block. :param max_age: The maximum number of seconds a connection should be kept alive. Connections older than this will be reaped. :param timeout: The maximum number of seconds a connection attempt or RPC call can take before a TimeoutError is raised. :param max_connection_attempts: The maximum number of times the pool will attempt to open a connection.
class HeaderTest(AbstractTest): protocol_factory = THeaderProtocol.THeaderProtocolFactory( True, [ THeaderTransport.HEADERS_CLIENT_TYPE, THeaderTransport.FRAMED_DEPRECATED, THeaderTransport.UNFRAMED_DEPRECATED, THeaderTransport.HTTP_CLIENT_TYPE ]) def testZlibCompression(self): htrans = self.protocol.trans if isinstance(htrans, THeaderTransport): htrans.add_transform(THeaderTransport.ZLIB_TRANSFORM) self.testStruct() def testSnappyCompression(self): htrans = self.protocol.trans if isinstance(htrans, THeaderTransport): htrans.add_transform(THeaderTransport.SNAPPY_TRANSFORM) self.testStruct() def testMultipleCompression(self): htrans = self.protocol.trans if isinstance(htrans, THeaderTransport): htrans.add_transform(THeaderTransport.ZLIB_TRANSFORM) htrans.add_transform(THeaderTransport.SNAPPY_TRANSFORM) self.testStruct() def testKeyValueHeader(self): if self.server_header and self.server_type == 'TNonblockingServer': # TNonblockingServer uses different protocol instances for input # and output so persistent header won't work return htrans = self.protocol.trans if isinstance(htrans, THeaderTransport): # Try just persistent header htrans.set_persistent_header(b"permanent", b"true") self.client.testString('test') headers = htrans.get_headers() self.assertTrue(b"permanent" in headers) self.assertEquals(headers[b"permanent"], b"true") # Try with two transient headers htrans.set_header(b"transient1", b"true") htrans.set_header(b"transient2", b"true") self.client.testString('test') headers = htrans.get_headers() self.assertTrue(b"permanent" in headers) self.assertEquals(headers[b"permanent"], b"true") self.assertTrue(b"transient1" in headers) self.assertEquals(headers[b"transient1"], b"true") self.assertTrue(b"transient2" in headers) self.assertEquals(headers[b"transient2"], b"true") # Add one, update one and delete one transient header htrans.set_header(b"transient2", b"false") htrans.set_header(b"transient3", b"true") self.client.testString('test') headers = htrans.get_headers() self.assertTrue(b"permanent" in headers) self.assertEquals(headers[b"permanent"], b"true") self.assertTrue(b"transient1" not in headers) self.assertTrue(b"transient2" in headers) self.assertEquals(headers[b"transient2"], b"false") self.assertTrue(b"transient3" in headers) self.assertEquals(headers[b"transient3"], b"true")
class HeaderCompactToDefault(HeaderTest, unittest.TestCase): serialize_factory = HeaderDefaultFactory( THeaderProtocol.THeaderProtocol.T_COMPACT_PROTOCOL) deserialize_factory = THeaderProtocol.THeaderProtocolFactory()
class HeaderBinaryToDefault(HeaderTest, unittest.TestCase): serialize_factory = HeaderDefaultFactory( THeaderProtocol.THeaderProtocol.T_BINARY_PROTOCOL) deserialize_factory = THeaderProtocol.THeaderProtocolFactory()
type="int", dest="port", default=9090) parser.add_option("--timeout", action="store", type="int", dest="timeout", default=60) options, args = parser.parse_args() event_handler = TestServerEventHandler() if options.header: pfactory = THeaderProtocol.THeaderProtocolFactory( True, [ CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED, CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER ]) else: pfactory = TBinaryProtocol.TBinaryProtocolFactory() if options.context: processor = ThriftTest.ContextProcessor( TestContextHandler(options.port)) else: processor = ThriftTest.Processor(TestHandler()) if options.multiple: processor = TMultiplexedProcessor.TMultiplexedProcessor() if options.context: processor.registerProcessor(
dest="port", default=9090) parser.add_option("--timeout", action="store", type="int", dest="timeout", default=60) options, args = parser.parse_args() event_handler = TestServerEventHandler() if options.header: pfactory = THeaderProtocol.THeaderProtocolFactory( True, [ THeaderTransport.HEADERS_CLIENT_TYPE, THeaderTransport.FRAMED_DEPRECATED, THeaderTransport.UNFRAMED_DEPRECATED, THeaderTransport.HTTP_CLIENT_TYPE ]) else: pfactory = TBinaryProtocol.TBinaryProtocolFactory() if options.context: processor = ThriftTest.ContextProcessor( TestContextHandler(options.port)) else: processor = ThriftTest.Processor(TestHandler()) if options.multiple: processor = TMultiplexedProcessor.TMultiplexedProcessor() if options.context: