capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

2013-12-12-capnproto-0.4-time-travel.md (4449B)


      1 ---
      2 layout: post
      3 title: "Cap'n Proto v0.4: Time Traveling RPC"
      4 author: kentonv
      5 ---
      6 
      7 Well, [Hofstadter](http://en.wikipedia.org/wiki/Hofstadter's_law) kicked in and this release took
      8 way too long.  But, after three long months, I'm happy to announce:
      9 
     10 ### Time-Traveling RPC _(Promise Pipelining)_
     11 
     12 <img src='{{ site.baseurl }}images/time-travel.png' style='max-width:639px'>
     13 
     14 v0.4 finally introduces the long-promised [RPC system]({{ site.baseurl }}rpc.html).  Traditionally,
     15 RPC is plagued by the fact that networks have latency, and pretending that latency doesn't exist by
     16 hiding it behind what looks like a normal function call only makes the problem worse.
     17 Cap'n Proto has a simple solution to this problem:  send call results _back in time_, so they
     18 arrive at the client at the point in time when the call was originally made!
     19 
     20 Curious how Cap'n Proto bypasses the laws of physics?
     21 [Check out the docs!]({{ site.baseurl }}rpc.html)
     22 
     23 _UPDATE:  There has been some confusion about what I'm claiming.  I am NOT saying that using
     24 promises alone (i.e. being asynchronous) constitutes "time travel".  Cap'n Proto implements a
     25 technique called Promise Pipelining which allows a new request to be formed based on the content
     26 of a previous result (in part or in whole) before that previous result is returned.  Notice in the
     27 diagram that the result of foo() is being passed to bar().  Please
     28 [see the docs]({{ site.baseurl }}rpc.html) or
     29 [check out the calculator example](https://github.com/kentonv/capnproto/blob/master/c++/samples)
     30 for more._
     31 
     32 ### Promises in C++
     33 
     34 _UPDATE:  More confusion.  This section is **not** about pipelining ("time travel").  This section
     35 is just talking about implementing a promise API in C++.  Pipelining is another feature on top of
     36 that.  Please [see the RPC page]({{ site.baseurl }}rpc.html) if you want to know more about
     37 pipelining._
     38 
     39 If you do a lot of serious JavaScript programming, you've probably heard of
     40 [Promises/A+](http://promisesaplus.com/) and similar proposals.  Cap'n Proto RPC introduces a
     41 similar construct in C++.  In fact, the API is nearly identical, and its semantics are nearly
     42 identical.  Compare with
     43 [Domenic Denicola's JavaScript example](http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/):
     44 
     45 {% highlight c++ %}
     46 // C++ version of Domenic's JavaScript promises example.
     47 getTweetsFor("domenic") // returns a promise
     48   .then([](vector<Tweet> tweets) {
     49     auto shortUrls = parseTweetsForUrls(tweets);
     50     auto mostRecentShortUrl = shortUrls[0];
     51     // expandUrlUsingTwitterApi returns a promise
     52     return expandUrlUsingTwitterApi(mostRecentShortUrl);
     53   })
     54   .then(httpGet) // promise-returning function
     55   .then(
     56     [](string responseBody) {
     57       cout << "Most recent link text:" << responseBody << endl;
     58     },
     59     [](kj::Exception&& error) {
     60       cerr << "Error with the twitterverse:" << error << endl;
     61     }
     62   );
     63 {% endhighlight %}
     64 
     65 This is C++, but it is no more lines -- nor otherwise more complex -- than the equivalent
     66 JavaScript.  We're doing several I/O operations, we're doing them asynchronously, and we don't
     67 have a huge unreadable mess of callback functions.  Promises are based on event loop concurrency,
     68 which means you can perform concurrent operations with shared state without worrying about mutex
     69 locking -- i.e., the JavaScript model.  (Of course, if you really want threads, you can run
     70 multiple event loops in multiple threads and make inter-thread RPC calls between them.)
     71 
     72 [More on C++ promises.]({{ site.baseurl }}cxxrpc.html#kj_concurrency_framework)
     73 
     74 ### Python too
     75 
     76 [Jason](https://github.com/jparyani) has been diligently keeping his
     77 [Python bindings](http://jparyani.github.io/pycapnp/) up to date, so you can already use RPC there
     78 as well.  The Python interactive interpreter makes a great debugging tool for calling C++ servers.
     79 
     80 ### Up Next
     81 
     82 Cap'n Proto is far from done, but working on it in a bubble will not produce ideal results.
     83 Starting after the holidays, I will be refocusing some of my time into an adjacent project which
     84 will be a heavy user of Cap'n Proto.  I hope this experience will help me discover first hand
     85 the pain points in the current interface and keep development going in the right direction.
     86 
     87 This does, however, mean that core Cap'n Proto development will slow somewhat (unless contributors
     88 pick up the slack! ;) ).  I am extremely excited about this next project, though, and I think you
     89 will be too.  Stay tuned!