Search code examples
thrift

Understanding Thrift and Implementing a handler


I'm a new user of Apache Thrift trying to solidify my understanding of how to use Thrift properly.

I understand the Thrift is used to abstract application functions and generates boilerplate code in a multitude of languages for a server and client, and I know that all I as the user need to do is to implement a handler to perform some sort of function, but the specifics confuse me.

Specifically, I am trying to use Thrift to generate a server for Facebook's Scribe in Golang that would listen for data and perform some function on that data as soon as it receives it. In this case, it seems that I don't even need a client to call, as I want the server to act every time data is received.

I am following this server as an example, as it seems to be an open sourced version of what I am interested in: https://github.com/mindreframer/golang-stuff/blob/master/github.com/tumblr/gocircuit/src/tumblr/scribe/server.go

Can anyone tell me if this fits my use case? If so, do I just have to implement the handler and perform my action in the log messages function? If this is the case, can someone point me to where that log messages function is actually called? I'm having trouble understanding the Thrift workflow. Thanks!


Solution

  • Specifically, I am trying to use Thrift to generate a server for Facebook's Scribe in Golang that would listen for data and perform some function on that data as soon as it receives it.

    As kind of a disclaimer, I don't know about Scribe, but I can give you some generic outlines.

    First and essentially, Thrift is an RPC and serialization framework. The distinction is not there by accident or to make it sound better, it has been deliberately put because it is possible to use the serialization stuff without the RPC part. Actually, this makes a lot of sense, possible use cases this include the serialization data for other purposes, or using Thrift in conjunction with MQ or message bus systems, to name just a few.

    In this case, it seems that I don't even need a client to call, as I want the server to act every time data is received.

    An remote procedure call (RPC) implies that we have a caller and a callee, or in the usual terminology, a client and a server. The server only recieves calls that someone sends - the client.

    server and client

    So you need at least some data source that feeds Thrift messages to the server by calling one of the interface methods defined in the service contract. If Scribe can do that (at least that's how the Go source looks to me) you probably need to tell Scribe somehow what server to call, by calling Listen().

    // Listen binds a Scribe protocol server to bind address 
    // and dispatches incoming requests to the handler. 
    func Listen(bind string, handler Handler) error { ...
    

    as shown in the test code. During test, these calls are simulated by a dedicated test client.