Hi Erik

> There's been some discussions about asynchronous C++ clients and
> servers (THRIFT-1). Inspired by the Twisted server (THRIFT-148) by
> Esteve Fernandez, as well as his C++ ideas (THRIFT-311), we (Erik
> Bernhardsson, Mattias de Zalenski) decided to implement our own
> version here at Spotify.

Glad to know that you found the support for Twisted useful, at least for
inspiring you to build an asynchronous client and server :-)

Some comments on your implementation:

(1) it changes services' interface (adding a new argument to the generated code)
(2) it forces the developer to call the callback function (the one
passed as an argument), instead of being automatically called when the
operation finishes
(3) it follows the Thrift interface too closely (TAsyncOutputTransport,
TAsioClient, TAsioServer, etc.). IMHO, it feels more natural to treat
Thrift as any other protocol and plug it into ASIO, than the other way
around. For example, there's no implementation of the TServer
interface in Twisted, because that would feel too unnatural to
developers familiar with the latter.

1 & 2 can be solved using futures. For example, instead of returning the
actual value from a client-side call or forcing to pass a callback, you return
a future (templatized to the return type) to which you would attach a callback
function:

void gotResults(int value) {
    std::cout << "Got results: " << value << std::endl;
}

future<int> f = calculatorClient.add(1, 2);
f.add_callback(boost::bind(&gotResults, _1));

when the client gets the value for Calculator#add, the gotResults function
will be called. This way you don't need to change the service interface, but
you can add callbacks to an asynchronous operation.

As for 3, I had to implement my own server in THRIFT-311 because I wanted to
run io_service.run() in a separate thread and use a threadpool, but that
should be up to the developer. For example, in the asynchronous TCP daytime
server from the ASIO distribution
(http://think-async.com/Asio/asio-1.3.1/doc/asio/tutorial/tutdaytime3/src.html),
you only have to implement an acceptor that takes an io_service.

Cheers.

Reply via email to