Search code examples
rpcthrift

How to implement a generic Thrift Proxy?


In Apache Thrift is it possible to create a generic proxy? For e.g. in proxy I want to do request/response logging or measure performance. The flow should be like Client <-> Generic Proxy <-> Server for all RPC calls.


Solution

  • Implement an custom Thrift "layered" protocol or a custom Thrift transport which intercepts your calls as needed.

    A lot of languages have adopted the multiplexed protocol which uses a generic TProtocolDecorator. That piece of code looks quite handy for that task. Look at the implementation of TMultiplexedProtocol to see how it's used. Basically the TProtocolDecorator class does most of the magic, you only need to override some methods and plug your newly developed protocol into the Thrift transport/protocol stack as usual.

    Alternatively, your goal could be achieved by adding a layered transport, similar to TBufferedTransport. But in that case you do not have the semantics behind the data, you only see strings whereas at the protocol level you have methods like WriteMessageBegin or ReadMessageBegin which make live much easier.