cannam@133: --- cannam@133: layout: post cannam@133: title: "Cap'n Proto v0.4: Time Traveling RPC" cannam@133: author: kentonv cannam@133: --- cannam@133: cannam@133: Well, [Hofstadter](http://en.wikipedia.org/wiki/Hofstadter's_law) kicked in and this release took cannam@133: way too long. But, after three long months, I'm happy to announce: cannam@133: cannam@133: ### Time-Traveling RPC _(Promise Pipelining)_ cannam@133: cannam@133: cannam@133: cannam@133: v0.4 finally introduces the long-promised [RPC system]({{ site.baseurl }}rpc.html). Traditionally, cannam@133: RPC is plagued by the fact that networks have latency, and pretending that latency doesn't exist by cannam@133: hiding it behind what looks like a normal function call only makes the problem worse. cannam@133: Cap'n Proto has a simple solution to this problem: send call results _back in time_, so they cannam@133: arrive at the client at the point in time when the call was originally made! cannam@133: cannam@133: Curious how Cap'n Proto bypasses the laws of physics? cannam@133: [Check out the docs!]({{ site.baseurl }}rpc.html) cannam@133: cannam@133: _UPDATE: There has been some confusion about what I'm claiming. I am NOT saying that using cannam@133: promises alone (i.e. being asynchronous) constitutes "time travel". Cap'n Proto implements a cannam@133: technique called Promise Pipelining which allows a new request to be formed based on the content cannam@133: of a previous result (in part or in whole) before that previous result is returned. Notice in the cannam@133: diagram that the result of foo() is being passed to bar(). Please cannam@133: [see the docs]({{ site.baseurl }}rpc.html) or cannam@133: [check out the calculator example](https://github.com/kentonv/capnproto/blob/master/c++/samples) cannam@133: for more._ cannam@133: cannam@133: ### Promises in C++ cannam@133: cannam@133: _UPDATE: More confusion. This section is **not** about pipelining ("time travel"). This section cannam@133: is just talking about implementing a promise API in C++. Pipelining is another feature on top of cannam@133: that. Please [see the RPC page]({{ site.baseurl }}rpc.html) if you want to know more about cannam@133: pipelining._ cannam@133: cannam@133: If you do a lot of serious Javascript programming, you've probably heard of cannam@133: [Promises/A+](http://promisesaplus.com/) and similar proposals. Cap'n Proto RPC introduces a cannam@133: similar construct in C++. In fact, the API is nearly identical, and its semantics are nearly cannam@133: identical. Compare with cannam@133: [Domenic Denicola's Javascript example](http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/): cannam@133: cannam@133: {% highlight c++ %} cannam@133: // C++ version of Domenic's Javascript promises example. cannam@133: getTweetsFor("domenic") // returns a promise cannam@133: .then([](vector tweets) { cannam@133: auto shortUrls = parseTweetsForUrls(tweets); cannam@133: auto mostRecentShortUrl = shortUrls[0]; cannam@133: // expandUrlUsingTwitterApi returns a promise cannam@133: return expandUrlUsingTwitterApi(mostRecentShortUrl); cannam@133: }) cannam@133: .then(httpGet) // promise-returning function cannam@133: .then( cannam@133: [](string responseBody) { cannam@133: cout << "Most recent link text:" << responseBody << endl; cannam@133: }, cannam@133: [](kj::Exception&& error) { cannam@133: cerr << "Error with the twitterverse:" << error << endl; cannam@133: } cannam@133: ); cannam@133: {% endhighlight %} cannam@133: cannam@133: This is C++, but it is no more lines -- nor otherwise more complex -- than the equivalent cannam@133: Javascript. We're doing several I/O operations, we're doing them asynchronously, and we don't cannam@133: have a huge unreadable mess of callback functions. Promises are based on event loop concurrency, cannam@133: which means you can perform concurrent operations with shared state without worrying about mutex cannam@133: locking -- i.e., the Javascript model. (Of course, if you really want threads, you can run cannam@133: multiple event loops in multiple threads and make inter-thread RPC calls between them.) cannam@133: cannam@133: [More on C++ promises.]({{ site.baseurl }}cxxrpc.html#kj_concurrency_framework) cannam@133: cannam@133: ### Python too cannam@133: cannam@133: [Jason](https://github.com/jparyani) has been diligently keeping his cannam@133: [Python bindings](http://jparyani.github.io/pycapnp/) up to date, so you can already use RPC there cannam@133: as well. The Python interactive interpreter makes a great debugging tool for calling C++ servers. cannam@133: cannam@133: ### Up Next cannam@133: cannam@133: Cap'n Proto is far from done, but working on it in a bubble will not produce ideal results. cannam@133: Starting after the holidays, I will be refocusing some of my time into an adjacent project which cannam@133: will be a heavy user of Cap'n Proto. I hope this experience will help me discover first hand cannam@133: the pain points in the current interface and keep development going in the right direction. cannam@133: cannam@133: This does, however, mean that core Cap'n Proto development will slow somewhat (unless contributors cannam@133: pick up the slack! ;) ). I am extremely excited about this next project, though, and I think you cannam@133: will be too. Stay tuned!