baseplate.clients.thrift
¶
Thrift is a cross-language framework for cross-service communication. Developers write a language-independent definition of a service’s API (the “IDL”) and Thrift’s code generator makes server and client libraries for that API.
Example¶
To add a Thrift client to your application, ensure that your service has
Baseplate.py’s Thrift build step integrated in its root setup.py
:
from baseplate.frameworks.thrift.command import ThriftBuildPyCommand
...
setup(
...
cmdclass={
"build_py": ThriftBuildPyCommand,
},
)
Then add the downstream service’s IDL to your application directory:
$ cp ~/src/other/other/other.thrift myservice/
Baseplate.py will automatically run the Thrift compiler on this when building
your application and put the output into a Python package within your
application like yourservice.other_thrift
. Import the client class and add
it to your context object:
from .other_thrift import OtherService
...
def make_wsgi_app(app_config):
...
baseplate.configure_context(
app_config,
{
...
"foo": ThriftClient(OtherService.Client),
...
}
)
then configure it in your application’s configuration file:
[app:main]
...
# required: the host:port to find the service at
foo.endpoint = localhost:9999
# optional: the size of the connection pool (default 10)
foo.size = 10
# optional: how long a connection can be alive before we
# recycle it (default 1 minute)
foo.max_age = 1 minute
# optional: how long before we time out when connecting
# or doing an RPC (default 1 second)
foo.timeout = 1 second
# optional: how many times we'll retry connecting (default 3)
foo.max_retries = 3
...
and finally use the attached client in request:
def my_method(request):
request.foo.is_healthy()
Classes¶
-
class
baseplate.clients.thrift.
ThriftClient
(client_cls, **kwargs)[source]¶ Configure a Thrift client.
This is meant to be used with
baseplate.Baseplate.configure_context()
.See
baseplate.lib.thrift_pool.thrift_pool_from_config()
for available configuration settings.Parameters: client_cls ( Any
) – The class object of a Thrift-generated client class, e.g.YourService.Client
.
-
class
baseplate.clients.thrift.
ThriftContextFactory
(pool, client_cls)[source]¶ Thrift client pool context factory.
This factory will attach a proxy object with the same interface as your thrift client to an attribute on the
RequestContext
. When a thrift method is called on this proxy object, it will check out a connection from the connection pool and execute the RPC, automatically recording diagnostic information.Parameters: - pool (
ThriftConnectionPool
) – The connection pool. - client_cls (
Any
) – The class object of a Thrift-generated client class, e.g.YourService.Client
.
The proxy object has a
retrying
method which takes the same parameters asRetryPolicy.new
and acts as a context manager. The context manager returns another proxy object where Thrift service method calls will be automatically retried with the specified retry policy when transient errors occur:with context.my_service.retrying(attempts=3) as svc: svc.some_method()
- pool (
Runtime Metrics¶
In addition to request-level metrics reported through spans, this wrapper reports connection pool statistics periodically via the Process-level metrics system. All metrics are prefixed as follows:
{namespace}.runtime.{hostname}.PID{pid}.clients.{name}
where namespace
is the application’s namespace, hostname
and pid
come from the operating system, and name
is the name given to
add_to_context()
when registering this
context factory.
The following metrics are reported:
pool.size
- The size limit for the connection pool.
pool.in_use
- How many connections have been established and are currently checked out and being used.