def main():
    n = 100000

    print("binary protocol struct benchmark for {} times:".format(n))
    encode(n)
    decode(n)

    print("\naccelerated protocol struct benchmark for {} times:".format(n))
    encode(n, TBinaryProtocolAcceleratedFactory())
    decode(n, TBinaryProtocolAcceleratedFactory())
Ejemplo n.º 2
0
def _bench_thrift(loops=1000):
    """Measure using a thrift-generated library N times.

    The target is a simple addressbook.  We measure the following:

    * create an addressbook with 1 person in it
    * serialize it
    * deserialize it into a new addressbook

    For each iteration we repeat this 100 times.
    """
    # proto_factory = TBinaryProtocolFactory()
    proto_factory = TBinaryProtocolAcceleratedFactory()

    elapsed = 0
    times = []
    for _ in range(loops):
        # This is a macro benchmark for a Python implementation
        # so "elapsed" covers more than just how long the Addressbook ops take.
        t0 = pyperf.perf_counter()
        for _ in range(100):
            # First, create the addressbook.
            ab = make_addressbook()
            # Then, round-trip through serialization.
            encoded = serialize(ab, proto_factory)
            ab2 = ttypes.AddressBook()
            deserialize(ab2, encoded, proto_factory)
        t1 = pyperf.perf_counter()

        elapsed += t1 - t0
        times.append(t0)
    times.append(pyperf.perf_counter())
    return elapsed, times
Ejemplo n.º 3
0
def main():
    op = optparse.OptionParser(usage='%prog [options]', add_help_option=False)
    op.add_option('-p', '--port',
                  action='store', type='int', dest='port', default=1234,
                  help='The server port')
    op.add_option('-c', '--with-context',
                  action='store_true', help='Use the generated ContextIface')
    op.add_option('-h', '--header',
                  action='store_true', help='Use the generated ContextIface')
    op.add_option('-?', '--help',
                  action='help',
                  help='Show this help message and exit')

    (options, args) = op.parse_args()
    if args:
        op.error('trailing arguments: ' + ' '.join(args))

    if options.with_context:
        handler = AsyncContextLoadHandler()
        processor = LoadTest.ContextProcessor(handler)
    else:
        handler = AsyncLoadHandler()
        processor = LoadTest.Processor(handler)

    if options.header:
        proto_factory = THeaderProtocolFactory(True,
                          [THeaderTransport.HEADERS_CLIENT_TYPE,
                           THeaderTransport.FRAMED_DEPRECATED])
    else:
        proto_factory = TBinaryProtocolAcceleratedFactory()
    server_factory = ThriftServerFactory(processor, proto_factory)
    reactor.listenTCP(options.port, server_factory)

    print 'Serving requests on port %d...' % (options.port,)
    reactor.run()
Ejemplo n.º 4
0
 def __init__(
     self,
     thrift_class: Type[T],
     protocol_factory: TProtocolFactory = TBinaryProtocolAcceleratedFactory(
     ),
 ):
     self.thrift_class = thrift_class
     self.factory = protocol_factory
Ejemplo n.º 5
0
def get_server(api, handler, port, **kwargs):
    processor = api.Processor(handler)
    socket = TSocket.TServerSocket(port=port)
    tfactory = kwargs.get('transport') or TTransport.TFramedTransportFactory()
    pfactory = kwargs.get('protocol') or TBinaryProtocolAcceleratedFactory()
    server = TServer.TThreadPoolServer(processor, socket, tfactory, pfactory)  
    server.setNumThreads(8)
    return server
Ejemplo n.º 6
0
    def serve_thrift(self):
        handler = Handler(self.forsun)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6458)
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.bind(port, bind_address)
        self.server.start(1)
        logging.info("starting server by %s:%s", bind_address, port)
Ejemplo n.º 7
0
def test():
    try:
        transport = TStreamPool('127.0.0.1', 20000, max_stream=10)
        client = PoolClient(Client, transport,
                            TBinaryProtocolAcceleratedFactory())

        start = time.time()
        futures = []
        for i in range(10000):
            futures.append(client.add(0, i))
        yield futures
        print(time.time() - start)

    except Thrift.TException as ex:
        print("%s" % (ex.message))
Ejemplo n.º 8
0
    def serve(self):
        handler = Handler(self.manager)
        processor = Processor(handler)
        tfactory = TIOStreamTransportFactory()
        protocol = TBinaryProtocolAcceleratedFactory()
        self.server = TTornadoServer(processor, tfactory, protocol)
        self.server.processor._processMap["pull"] = process_pull

        bind_address = config.get("BIND_ADDRESS", "127.0.0.1")
        port = config.get("PORT", 6455)
        self.server.bind(port, bind_address)
        self.server.start(1)

        ioloop = IOLoop.instance()
        self.manager.start()
        logging.info("starting server by %s:%s", bind_address, port)
        ioloop.start()
Ejemplo n.º 9
0
 def __init__(self,
              service_cls,
              protocol_factory=TBinaryProtocolAcceleratedFactory()):
     """
 Args:
   service_cls - The thrift generated interface class.
   protocol_factory - A class implementing getProtocol(...).  By default,
    TBinaryProtocolAcceleratedFactory is used.
 """
     self._protocol_factory = protocol_factory
     self._seq_id = 0
     self._service_modules = [
         sys.modules[c.__module__] for c in inspect.getmro(service_cls)
         if not c is object
     ]
     if len(self._service_modules) == 1:
         self._FindClass = self._FindClassNoInheritance
     else:
         self._attr_cache = {}
         self._FindClass = self._FindClassInheritance
Ejemplo n.º 10
0
class EdgeRequestContext(object):
    """Contextual information about the initial request to an edge service

    :param bytes header: Serialized "Edge-Request" header.
    :param baseplate.core.AuthenticationContext authentication_context:
        Authentication context for the current request if it is authenticated.
    """

    _HEADER_PROTOCOL_FACTORY = TBinaryProtocolAcceleratedFactory()

    def __init__(self, header, authentication_context):
        self._header = header
        self._authentication_context = authentication_context
        self._t_request = None
        self._user = None
        self._oauth_client = None
        self._session = None

    def attach_context(self, context):
        """Attach this to the provided Baseplate context.

        :param context: request context to attach this to
        """
        context.request_context = self

    def header_values(self):
        """Dictionary of the serialized headers with the request context data

        Used to get the values to forward with upstream service calls.
        """
        return {
            'Edge-Request': self._header,
            'Authentication': self._authentication_context._token,
        }

    def event_fields(self):
        """Dictionary of values to be added to events in the current context
        """
        try:
            oauth_client_id = self.oauth_client.id
        except (WithheldAuthenticationError, UndefinedSecretsException):
            oauth_client_id = None

        fields = {
            'session_id': self.session.id,
            'oauth_client_id': oauth_client_id,
        }
        fields.update(self.user.event_fields())
        return fields

    @classmethod
    def create(cls,
               authentication_context=None,
               loid_id=None,
               loid_created_ms=None,
               session_id=None):
        """Factory method to create a new EdgeRequestContext object.

        Builds a new EdgeRequestContext object with the given parameters and
        serializes the "Edge-Request" header.

        :param baseplate.core.AuthenticationContext authentication_context:
            (Optional) AuthenticationContext for the current request if it is
            authenticated.
        :param str loid_id: (Optional) ID for the current LoID in fullname
            format.
        :param int loid_created_ms: (Optional) Epoch milliseconds when the
            current LoID cookie was created.
        :param str session_id: (Optional) ID for the current session cookie.

        """
        # Importing the Thrift models inline so that building them is not a
        # hard, import-time dependency for tasks like building the docs.
        from .thrift.ttypes import Loid as TLoid
        from .thrift.ttypes import Request as TRequest
        from .thrift.ttypes import Session as TSession

        if loid_id is not None and not loid_id.startswith("t2_"):
            raise ValueError(
                "loid_id <%s> is not in a valid format, it should be in the "
                "fullname format with the '0' padding removed: 't2_loid_id'",
                loid_id)

        loid = TLoid(id=loid_id, created_ms=loid_created_ms)
        session = TSession(id=session_id)
        request = TRequest(loid=loid, session=session)
        header = Serializer.serialize(cls._HEADER_PROTOCOL_FACTORY, request)
        if authentication_context is None:
            authentication_context = AuthenticationContext()
        request_context = cls(header, authentication_context)
        # Set the _t_request property so we can skip the deserialization step
        # since we already have the thrift object.
        request_context._t_request = request
        return request_context

    @property
    def user(self):
        """:py:class:`baseplate.core.User` object for the current context"""
        if self._user is None:
            t_context = self._thrift_request_context()
            self._user = User(
                authentication_context=self._authentication_context,
                loid=t_context.loid.id,
                cookie_created_ms=t_context.loid.created_ms,
            )
        return self._user

    @property
    def oauth_client(self):
        """:py:class:`baseplate.core.OAuthClient` object for the current context
        """
        if self._oauth_client is None:
            self._oauth_client = OAuthClient(self._authentication_context)
        return self._oauth_client

    @property
    def session(self):
        """:py:class:`baseplate.core.Session` object for the current context"""
        if self._session is None:
            t_context = self._thrift_request_context()
            self._session = Session(id=t_context.session.id)
        return self._session

    def _thrift_request_context(self):
        # Importing the Thrift models inline so that building them is not a
        # hard, import-time dependency for tasks like building the docs.
        from .thrift.ttypes import Loid as TLoid
        from .thrift.ttypes import Request as TRequest
        from .thrift.ttypes import Session as TSession
        if self._t_request is None:
            self._t_request = TRequest()
            self._t_request.loid = TLoid()
            self._t_request.session = TSession()
            if self._header:
                try:
                    Serializer.deserialize(
                        self._HEADER_PROTOCOL_FACTORY,
                        self._header,
                        self._t_request,
                    )
                except Exception:
                    logger.debug(
                        "Invalid Edge-Request header. %s",
                        self._header,
                    )
        return self._t_request
Ejemplo n.º 11
0
def main():
    op = optparse.OptionParser(usage='%prog [options]', add_help_option=False)
    op.add_option('-p',
                  '--port',
                  action='store',
                  type='int',
                  dest='port',
                  default=1234,
                  help='The server port')
    op.add_option('-s',
                  '--servertype',
                  action='store',
                  type='string',
                  dest='servertype',
                  default='TNonblockingServer',
                  help='Type name of server')
    op.add_option('-w',
                  '--num_workers',
                  action='store',
                  type='int',
                  dest='workers',
                  default=4,
                  help='Number of worker processes/threads')
    op.add_option('-Q',
                  '--max_queue_size',
                  action='store',
                  type='int',
                  dest='max_queue_size',
                  default=0,
                  help='Max queue size, passed to TNonblockingServer')
    op.add_option('-h',
                  '--header',
                  action='store_true',
                  help='Use the generated ContextIface')
    op.add_option('-?',
                  '--help',
                  action='help',
                  help='Show this help message and exit')

    (options, args) = op.parse_args()
    if args:
        op.error('trailing arguments: ' + ' '.join(args))

    handler = LoadHandler()
    processor = LoadTest.Processor(handler)

    if options.header:
        pfactory = THeaderProtocolFactory(True, [
            CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED,
            CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER
        ])
        if options.servertype == 'TCppServer':
            print('C++ ThriftServer, Header transport, backwards compatible '
                  'with all other types')
        elif options.servertype == 'TNonblockingServer':
            print('Header transport, backwards compatible with framed')
        else:
            print('Header transport, backwards compatible with ' +
                  'unframed, framed, http')
    else:
        if options.servertype == 'TCppServer':
            if not options.header:
                op.error('TCppServer cannot be used without header')
        elif options.servertype == 'TNonblockingServer':
            print('Framed transport')
        else:
            print('Unframed transport')
        pfactory = TBinaryProtocolAcceleratedFactory()

    if options.servertype == 'TCppServer':
        server = TCppServer.TCppServer(processor)
        server.setPort(options.port)
        print('Worker threads: ' + str(options.workers))
        server.setNumIOWorkerThreads(options.workers)
    else:
        transport = TSocket.TServerSocket(options.port)
        tfactory = TTransport.TBufferedTransportFactory()
        if options.servertype == "TNonblockingServer":
            server = TNonblockingServer.TNonblockingServer(
                processor,
                transport,
                pfactory,
                maxQueueSize=options.max_queue_size)
        elif options.servertype == "TProcessPoolServer":
            server = TProcessPoolServer.TProcessPoolServer(
                processor, transport, tfactory, pfactory)
            print('Worker processes: ' + str(options.workers))
            server.setNumWorkers(options.workers)
        else:
            ServerClass = getattr(TServer, options.servertype)
            server = ServerClass(processor, transport, tfactory, pfactory)

    print('Serving ' + options.servertype + ' requests on port %d...' %
          (options.port, ))
    server.serve()
Ejemplo n.º 12
0
def main():
    op = optparse.OptionParser(usage='%prog [options]', add_help_option=False)
    op.add_option('-p',
                  '--port',
                  action='store',
                  type='int',
                  dest='port',
                  default=1234,
                  help='The server port')
    op.add_option('-s',
                  '--servertype',
                  action='store',
                  type='string',
                  dest='servertype',
                  default='TGeventServer',
                  help='Type name of server')
    op.add_option('-w',
                  '--num_workers',
                  action='store',
                  type='int',
                  dest='workers',
                  default=4,
                  help='Number of worker processes')
    op.add_option('-Q',
                  '--max_queue_size',
                  action='store',
                  type='int',
                  dest='max_queue_size',
                  default=0,
                  help='Max queue size, passed to TNonblockingServer')
    op.add_option('-h',
                  '--header',
                  action='store_true',
                  help='Use the generated ContextIface')
    op.add_option('-?',
                  '--help',
                  action='help',
                  help='Show this help message and exit')

    (options, args) = op.parse_args()
    if args:
        op.error('trailing arguments: ' + ' '.join(args))

    handler = LoadHandler()
    if options.servertype == 'TGeventServer':
        # only import TGeventServer when necessary. TGeventServer calls
        # monkey_patch, which breaks other servers
        from apache.thrift.test.sync_load_handler import GeventLoadHandler
        from thrift.server import TGeventServer
        handler = GeventLoadHandler()
    processor = LoadTest.Processor(handler)

    if options.header:
        pfactory = THeaderProtocolFactory(True, \
                                          [THeaderTransport.HEADERS_CLIENT_TYPE,
                                           THeaderTransport.FRAMED_DEPRECATED,
                                           THeaderTransport.UNFRAMED_DEPRECATED,
                                           THeaderTransport.HTTP_CLIENT_TYPE])
        if options.servertype == 'TNonblockingServer':
            print 'Header transport, backwards compatible with framed'
        else:
            print 'Header transport, backwards compatible with ' + \
                'unframed, framed, http'
    else:
        if options.servertype == 'TNonblockingServer':
            print 'Framed transport'
        else:
            print 'Unframed transport'
        pfactory = TBinaryProtocolAcceleratedFactory()

    transport = TSocket.TServerSocket(options.port)
    tfactory = TTransport.TBufferedTransportFactory()
    if options.servertype == "TNonblockingServer":
        server = TNonblockingServer.TNonblockingServer(
            processor,
            transport,
            pfactory,
            maxQueueSize=options.max_queue_size)
    elif options.servertype == "TProcessPoolServer":
        server = TProcessPoolServer.TProcessPoolServer(processor, transport,
                                                       tfactory, pfactory)
        print 'Worker processes: ' + str(options.workers)
        server.setNumWorkers(options.workers)
    elif options.servertype == "TGeventServer":
        print 'Worker processes: ' + str(options.workers)
        # Gevent makes its own server transport.
        server = TGeventServer.TGeventServer(options.port, processor, None,
                                             tfactory, pfactory)
        server.setNumWorkers(options.workers)
    else:
        ServerClass = getattr(TServer, options.servertype)
        server = ServerClass(processor, transport, tfactory, pfactory)

    print 'Serving ' + options.servertype + \
        ' requests on port %d...' % (options.port,)
    server.serve()
Ejemplo n.º 13
0
class EdgeRequestContext:
    """Contextual information about the initial request to an edge service.

    Construct this using an
    :py:class:`~baseplate.lib.edge_context.EdgeRequestContextFactory`.

    """

    _HEADER_PROTOCOL_FACTORY = TBinaryProtocolAcceleratedFactory()

    def __init__(self, authn_token_validator: AuthenticationTokenValidator,
                 header: Optional[bytes]):
        self._authn_token_validator = authn_token_validator
        self._header = header

    def attach_context(self, context: RequestContext) -> None:
        """Attach this to the provided :py:class:`~baseplate.RequestContext`.

        :param context: request context to attach this to

        """
        context.request_context = self
        context.raw_request_context = self._header

    def event_fields(self) -> Dict[str, Any]:
        """Return fields to be added to events."""
        fields = {"session_id": self.session.id}
        fields.update(self.user.event_fields())
        fields.update(self.oauth_client.event_fields())
        return fields

    @cached_property
    def authentication_token(self) -> AuthenticationToken:
        return self._authn_token_validator.validate(
            self._t_request.authentication_token)

    @cached_property
    def user(self) -> User:
        """:py:class:`~baseplate.lib.edge_context.User` object for the current context."""
        return User(
            authentication_token=self.authentication_token,
            loid=self._t_request.loid.id,
            cookie_created_ms=self._t_request.loid.created_ms,
        )

    @cached_property
    def oauth_client(self) -> OAuthClient:
        """:py:class:`~baseplate.lib.edge_context.OAuthClient` object for the current context."""
        return OAuthClient(self.authentication_token)

    @cached_property
    def session(self) -> Session:
        """:py:class:`~baseplate.lib.edge_context.Session` object for the current context."""
        return Session(id=self._t_request.session.id)

    @cached_property
    def service(self) -> Service:
        """:py:class:`~baseplate.lib.edge_context.Service` object for the current context."""
        return Service(self.authentication_token)

    @cached_property
    def _t_request(self) -> TRequest:  # pylint: disable=method-hidden
        _t_request = TRequest()
        _t_request.loid = TLoid()
        _t_request.session = TSession()
        if self._header:
            try:
                TSerialization.deserialize(_t_request, self._header,
                                           self._HEADER_PROTOCOL_FACTORY)
            except Exception:
                logger.debug("Invalid Edge-Request header. %s", self._header)
        return _t_request
Ejemplo n.º 14
0
class EdgeContext:
    """Contextual information about the initial request to an edge service.

    Once the :py:class:`~reddit_edgecontext.EdgeContextFactory` is set up, an
    instance of this object will be available at ``request.edge_context``.

    """

    _HEADER_PROTOCOL_FACTORY = TBinaryProtocolAcceleratedFactory()

    def __init__(self, authn_token_validator: AuthenticationTokenValidator,
                 header: Optional[bytes]):
        self._authn_token_validator = authn_token_validator
        self._header = header

    def event_fields(self) -> Dict[str, Any]:
        """Return fields to be added to events."""
        fields = {"session_id": self.session.id}
        if self.device.id:
            fields["device_id"] = self.device.id
        fields.update(self.user.event_fields())
        fields.update(self.oauth_client.event_fields())
        return fields

    @cached_property
    def authentication_token(self) -> AuthenticationToken:
        return self._authn_token_validator.validate(
            self._t_request.authentication_token)

    @cached_property
    def user(self) -> User:
        """:py:class:`~reddit_edgecontext.User` object for the current context."""
        return User(
            authentication_token=self.authentication_token,
            loid_=self._t_request.loid.id,
            cookie_created_ms=self._t_request.loid.created_ms,
        )

    @cached_property
    def oauth_client(self) -> OAuthClient:
        """:py:class:`~reddit_edgecontext.OAuthClient` object for the current context."""
        return OAuthClient(self.authentication_token)

    @cached_property
    def device(self) -> Device:
        """:py:class:`~reddit_edgecontext.Device` object for the current context."""
        return Device(id=self._t_request.device.id)

    @cached_property
    def session(self) -> Session:
        """:py:class:`~reddit_edgecontext.Session` object for the current context."""
        return Session(id=self._t_request.session.id)

    @cached_property
    def service(self) -> Service:
        """:py:class:`~reddit_edgecontext.Service` object for the current context."""
        return Service(self.authentication_token)

    @cached_property
    def origin_service(self) -> OriginService:
        """:py:class:`~reddit_edgecontext.Origin` object for the current context."""
        return OriginService(self._t_request.origin_service.name)

    @cached_property
    def geolocation(self) -> Geolocation:
        """:py:class:`~reddit_edgecontext.Geolocation` object for the current context."""
        return Geolocation(
            country_code=self._t_request.geolocation.country_code)

    @cached_property
    def _t_request(self) -> TRequest:
        _t_request = TRequest()
        _t_request.loid = TLoid()
        _t_request.session = TSession()
        _t_request.device = TDevice()
        _t_request.origin_service = TOriginService()
        _t_request.geolocation = TGeolocation()
        if self._header:
            try:
                TSerialization.deserialize(_t_request, self._header,
                                           self._HEADER_PROTOCOL_FACTORY)
            except Exception:
                logger.debug("Invalid Edge-Request header. %s", self._header)
        return _t_request

    def attach_context(self, context: RequestContext) -> None:
        """Attach this to the provided :py:class:`~baseplate.RequestContext`.

        :param context: request context to attach this to

        """
        context.edge_context = self
        context.raw_edge_context = self._header
Ejemplo n.º 15
0
sys.path.insert(
    0,
    os.path.abspath(os.path.dirname(os.path.abspath(
        os.path.dirname(__file__)))))
sys.path.append(
    os.path.abspath(
        os.path.abspath(os.path.dirname(__file__)) + os.sep + "gen-py"))

from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from torthrift.transport import TIOStreamTransportFactory
from torthrift.server import TTornadoServer
from example.Example import Processor
from tornado.ioloop import IOLoop
from torthrift import gen


class Handler(object):
    def add(self, a, b):
        return a + b


if __name__ == "__main__":
    handler = Handler()
    processor = Processor(handler)
    tfactory = TIOStreamTransportFactory()
    protocol = TBinaryProtocolAcceleratedFactory()

    server = TTornadoServer(processor, tfactory, protocol)
    server.bind(20000)
    server.start(0)
    IOLoop.instance().start()
Ejemplo n.º 16
0
 def test_TBinaryProtocolAccelerated(self):
     buf = TTransport.TMemoryBuffer()
     transport = TTransport.TBufferedTransportFactory().getTransport(buf)
     factory = TBinaryProtocolAcceleratedFactory(transport)
     self.verify(self.binary_serialized, factory)
Ejemplo n.º 17
0
def main():
    op = optparse.OptionParser(usage="%prog [options]", add_help_option=False)
    op.add_option(
        "-p",
        "--port",
        action="store",
        type="int",
        dest="port",
        default=1234,
        help="The server port",
    )
    op.add_option(
        "-s",
        "--servertype",
        action="store",
        type="string",
        dest="servertype",
        default="TCppServer",
        help="Type name of server",
    )
    op.add_option(
        "-w",
        "--num_workers",
        action="store",
        type="int",
        dest="workers",
        default=4,
        help="Number of worker processes/threads",
    )
    op.add_option(
        "-h",
        "--header",
        action="store_true",
        default=True,
        help="Use the generated ContextIface",
    )
    op.add_option("-?",
                  "--help",
                  action="help",
                  help="Show this help message and exit")

    (options, args) = op.parse_args()
    if args:
        op.error("trailing arguments: " + " ".join(args))

    handler = LoadHandler()
    processor = LoadTest.Processor(handler)

    if options.header:
        pfactory = THeaderProtocolFactory(
            True,
            [
                CLIENT_TYPE.HEADER,
                CLIENT_TYPE.FRAMED_DEPRECATED,
                CLIENT_TYPE.UNFRAMED_DEPRECATED,
                CLIENT_TYPE.HTTP_SERVER,
            ],
        )
        if options.servertype == "TCppServer":
            print("C++ ThriftServer, Header transport, backwards compatible "
                  "with all other types")
        else:
            print("Header transport, backwards compatible with " +
                  "unframed, framed, http")
    else:
        if options.servertype == "TCppServer":
            if not options.header:
                op.error("TCppServer cannot be used without header")
        else:
            print("Unframed transport")
        pfactory = TBinaryProtocolAcceleratedFactory()

    if options.servertype == "TCppServer":
        server = TCppServer.TCppServer(processor)
        server.setPort(options.port)
        print("Worker threads: " + str(options.workers))
        server.setNumIOWorkerThreads(options.workers)
    else:
        transport = TSocket.TServerSocket(options.port)
        tfactory = TTransport.TBufferedTransportFactory()
        ServerClass = getattr(TServer, options.servertype)
        server = ServerClass(processor, transport, tfactory, pfactory)

    print("Serving " + options.servertype + " requests on port %d..." %
          (options.port, ))
    server.serve()
Ejemplo n.º 18
0
 def setUp(self):
     self.fac = TBinaryProtocolAcceleratedFactory()
Ejemplo n.º 19
0
# coding=utf-8

import inspect
import functools
import time
import logging
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from thrift.transport.TTransport import TBufferedTransportFactory
from thrift.transport.TSocket import TSocket

logger = logging.getLogger(__name__)
DEFAULT_THRIFT_PROTOCOL_FACTORY = TBinaryProtocolAcceleratedFactory()
DEFAULT_THRIFT_TRANSPORT_FACTORY = TBufferedTransportFactory()
SECS_FOR_DISCONNECT = 10
REQUEST_NUM_FOR_DISCONNECT = 1


class EnsureConnectionClient(type):

    def __new__(mcs, class_name, class_bases, class_dict):
        thrift_client_class = class_bases[0]  # thrift client

        def __init__(self, host_selector, req_timeout=5000, socket_connection_timeout=1000,
                     retry_count=3, protocol_factory=DEFAULT_THRIFT_PROTOCOL_FACTORY,
                     transport_factory=DEFAULT_THRIFT_TRANSPORT_FACTORY):

            self._host_selector = host_selector
            # 选择host
            host = self._host_selector.get_host()
            ip, port = host.split(':')
            self._ip = ip
Ejemplo n.º 20
0
class EdgeRequestContext:
    """Contextual information about the initial request to an edge service.

    Construct this using an
    :py:class:`~baseplate.core.EdgeRequestContextFactory`.

    """

    _HEADER_PROTOCOL_FACTORY = TBinaryProtocolAcceleratedFactory()

    def __init__(self, authn_token_validator, header):
        self._authn_token_validator = authn_token_validator
        self._header = header

    def attach_context(self, context):
        """Attach this to the provided :term:`context object`.

        :param context: request context to attach this to

        """
        context.request_context = self
        context.raw_request_context = self._header

    def event_fields(self):
        """Return fields to be added to events."""
        fields = {"session_id": self.session.id}
        fields.update(self.user.event_fields())
        fields.update(self.oauth_client.event_fields())
        return fields

    @cached_property
    def authentication_token(self):
        return self._authn_token_validator.validate(self._t_request.authentication_token)

    @cached_property
    def user(self):
        """:py:class:`~baseplate.core.User` object for the current context."""
        return User(
            authentication_token=self.authentication_token,
            loid=self._t_request.loid.id,
            cookie_created_ms=self._t_request.loid.created_ms,
        )

    @cached_property
    def oauth_client(self):
        """:py:class:`~baseplate.core.OAuthClient` object for the current context."""
        return OAuthClient(self.authentication_token)

    @cached_property
    def session(self):
        """:py:class:`~baseplate.core.Session` object for the current context."""
        return Session(id=self._t_request.session.id)

    @cached_property
    def service(self):
        """:py:class:`~baseplate.core.Service` object for the current context."""
        return Service(self.authentication_token)

    @cached_property
    def _t_request(self):  # pylint: disable=method-hidden
        # Importing the Thrift models inline so that building them is not a
        # hard, import-time dependency for tasks like building the docs.
        from baseplate.thrift.ttypes import Loid as TLoid
        from baseplate.thrift.ttypes import Request as TRequest
        from baseplate.thrift.ttypes import Session as TSession

        _t_request = TRequest()
        _t_request.loid = TLoid()
        _t_request.session = TSession()
        if self._header:
            try:
                TSerialization.deserialize(_t_request, self._header, self._HEADER_PROTOCOL_FACTORY)
            except Exception:
                logger.debug("Invalid Edge-Request header. %s", self._header)
        return _t_request
Ejemplo n.º 21
0
            sink_stack.AsyncProcessResponseMessage(
                MethodReturnMessage(
                    error=ClientError('Invalid message type.')))
            return
        try:
            self._serializer.SerializeThriftCall(msg, buf)
        except Exception as ex:
            sink_stack.AsyncProcessResponseMessage(
                MethodReturnMessage(error=ex))
            return

        sink_stack.Push(self)
        self.next_sink.AsyncProcessRequest(sink_stack, msg, buf, headers)

    def AsyncProcessResponse(self, sink_stack, context, stream, msg):
        if msg:
            # No need to deserialize, it already is.
            sink_stack.AsyncProcessResponseMessage(msg)
        else:
            try:
                msg = self._serializer.DeserializeThriftCall(stream)
            except Exception as ex:
                msg = MethodReturnMessage(error=ex)
            sink_stack.AsyncProcessResponseMessage(msg)


ThriftSerializerSink.Builder = SinkProvider(
    ThriftSerializerSink,
    SinkRole.Formatter,
    protocol_factory=TBinaryProtocolAcceleratedFactory())