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