index.md 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
---
layout: slides
title: "Slides: What's Next for Cap'n Proto"
---

<!--===================================================================================-->

<section markdown="1" id="slides-cover">

What's Next for Cap'n Proto?

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Streaming">

Cap'n Proto supports streaming!

{% highlight capnp %}
interface FileStore {
  get @0 (name :Text, stream :Stream);
  put @1 (name :Text) -> (stream :Stream);
}

interface Stream {
  write @0 (data :Data);
  end @1 ();
}
{% endhighlight %}

But flow control is up to the app.

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Flow Control">

Let's build it in.

{% highlight capnp %}
interface Stream {
  write @0 (data :Data) -> bulk;
  end @1 ();
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Realtime">

What about realtime streams?

{% highlight capnp %}
interface VideoCallStream {
  sendFrame @0 (frame :Frame) -> realtime;
}
{% endhighlight %}

<br>Best served on a UDP transport...

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph.png">

Forwarded request.

Where does response go?

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph-proxy.png">

Classic solution:

Proxy

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph-redirect.png">

Classic solution:

Redirect

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph-0rt.png">

Cap'n Proto:

3-Party Handoff

(aka 3PH)

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph-0rt.png">

Cap'n Proto:

3-Party Handoff

(aka 3PH)

... gonna need UDP

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

<img class="ph3" src="3ph-0rt.png">

Cap'n Proto:

3-Party Handoff

(aka 3PH)

... gonna need UDP

... and 0-RT crypto

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Three-Party Handoff">

API: "Tail call"

{% highlight c++ %}
kj::Promise<void> myRpc(MyRpcContext context) override {
  // Begin sub-request.
  auto subRequest = someCapability.someRpcRequest();
  subRequest.setSomeParam(someValue);

  // Send as a tail call.
  return context.tailCall(kj::mv(subRequest));
}
{% endhighlight %}

Today: Will proxy<br>Future: 3PH

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

KJ client networking, no TLS:

{% highlight c++ %}
void send() {
  auto io = kj::setupAsyncIo();
  auto& network = io.provider->getNetwork();
  auto addr = network.parseAddress("capnproto.org", 80)
      .wait(io.waitScope);
  auto connection = addr->connect().wait(io.waitScope);
  connection->write("GET /", 5).wait(io.waitScope);
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

KJ client networking with TLS:

{% highlight c++ %}
void send() {
  auto io = kj::setupAsyncIo();
  kj::TlsContext tls;
  auto network = tls.wrapNetwork(io.provider->getNetwork());
  auto addr = network->parseAddress("capnproto.org", 443)
      .wait(io.waitScope);
  auto connection = addr->connect().wait(io.waitScope);
  connection->write("GET /", 5).wait(io.waitScope);
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

Diff:

{% highlight c++ %}
void send() {

  kj::TlsContext tls;
                 tls.wrapNetwork(                         );




}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

{% highlight c++ %}
void receive() {
  auto io = kj::setupAsyncIo();
  auto& network = io.provider->getNetwork();
  auto addr = network.parseAddress("*", 80)
      .wait(io.waitScope);
  auto listener = addr->listen();
  auto connection = listener->accept().wait(io.waitScope);
  connection->write("HTTP/1.1 404 Not Found\r\n\r\n", 26)
      .wait(io.waitScope);
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

{% highlight c++ %}
void receive() {
  auto io = kj::setupAsyncIo();
  kj::TlsKeypair keypair { KEY_PEM_TEXT, CERT_PEM_TEXT };
  kj::TlsContext::Options options;
  options.defaultKeypair = keypair;
  kj::TlsContext tls(options);
  auto& network = io.provider->getNetwork();
  auto addr = network.parseAddress("*", 443).wait(io.waitScope);
  auto listener = tls.wrapPort(addr->listen());
  auto connection = listener->accept().wait(io.waitScope);
  connection->write("HTTP/1.1 404 Not Found\r\n\r\n", 26)
      .wait(io.waitScope);
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ TLS Bindings">

{% highlight c++ %}
void receive() {

  kj::TlsKeypair keypair { KEY_PEM_TEXT, CERT_PEM_TEXT };
  kj::TlsContext::Options options;
  options.defaultKeypair = keypair;
  kj::TlsContext tls(options);


                  tls.wrapPort(              );



}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ HTTP Library">

{% highlight c++ %}
auto io = kj::setupAsyncIo();
kj::HttpHeaderTable headerTable;
auto client = kj::newHttpClient(
    *headerTable, io.provider->getNetwork());

kj::HttpHeaders headers(*headerTable);
auto response = client->request(
    kj::HttpMethod::GET, "http://capnproto.org", headers)
    .response.wait(io.waitScope);

KJ_ASSERT(response.statusCode == 200);
KJ_LOG(INFO, response.body->readAllText().wait(io.waitScope));
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="KJ HTTP Library">

Headers identified by small numbers.

{% highlight c++ %}
kj::HttpHeaderTable::Builder builder;
kj::HttpHeaderId userAgent = builder.add("User-Agent");
auto headerTable = builder.build();

kj::HttpHeaders headers(*headerTable);
headers.set(kj::HttpHeaderId::HOST, "capnproto.org");
headers.set(userAgent, "kj-http/0.6");
{% endhighlight %}

Header parsing is zero-copy.

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Designated Initializers">

Kenton Varda's avatar
Kenton Varda committed
339
Ugly imperative code:
340 341 342 343 344

{% highlight c++ %}
capnp::MallocMessageBuilder message;

auto root = message.initRoot<MyStruct>();
Kenton Varda's avatar
Kenton Varda committed
345 346 347 348
root.setFoo(123);
root.setBar("foo");
auto inner = root.initBaz();
inner.setQux(true);
349 350 351 352 353 354 355 356 357 358

capnp::writeMessageToFd(fd, message);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Designated Initializers">

Kenton Varda's avatar
Kenton Varda committed
359
Nice declarative code:
360 361 362 363 364 365

{% highlight c++ %}
using namespace capnp::init;

capnp::MallocMessageBuilder message;
message.initRoot<MyStruct>(
Kenton Varda's avatar
Kenton Varda committed
366 367 368 369
  $foo = 123,
  $bar = "foo",
  $baz(
    $qux = true
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  )
);
capnp::writeMessageToFd(fd, message);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Designated Initializers">

Even better:

{% highlight c++ %}
using namespace capnp::init;

capnp::writeMessageToFd<MyStruct>(fd,
Kenton Varda's avatar
Kenton Varda committed
387 388 389 390
  $foo = 123,
  $bar = "foo",
  $baz(
    $qux = true
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  )
);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="Designated Initializers">

{% highlight c++ %}
struct {
  template <typename T>
  struct Setter {
    T value;
    template <typename U> void operator()(U& target) {
Kenton Varda's avatar
Kenton Varda committed
407
      target.setFoo(kj::fwd<T>(value));
408 409 410 411 412 413 414
    }
  };

  template <typename T>
  Setter<T> operator=(T&& value) {
    return { kj::fwd<T>(value) };
  }
Kenton Varda's avatar
Kenton Varda committed
415
} $foo;
416 417 418 419 420 421 422 423
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="POCS">

Kenton Varda's avatar
Kenton Varda committed
424
Not idiomatic:
425 426 427 428 429

{% highlight c++ %}
capnp::MallocMessageBuilder message;

MyStruct::Builder root = message.initRoot<MyStruct>();
Kenton Varda's avatar
Kenton Varda committed
430 431 432 433
root.setFoo(123);
root.setBar("foo");
InnerStruct::Builder inner = root.initBaz();
inner.setQux(true);
434 435 436 437 438 439 440 441 442 443 444 445 446 447

capnp::writeMessageToFd(fd, message);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="POCS">

Plain Old C++ Structs?

{% highlight c++ %}
MyStruct root;
Kenton Varda's avatar
Kenton Varda committed
448 449
root.foo = 123;
root.bar = "foo";
450
InnerStruct inner;
Kenton Varda's avatar
Kenton Varda committed
451 452
inner.qux = true;
root.baz = kj::mv(inner);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

capnp::writeMessageToFd(fd, message);
{% endhighlight %}

Caveat: No longer zero-copy.

</section>

<!--===================================================================================-->

<section markdown="1" data-title="POCS">

{% highlight c++ %}
capnp::MallocMessageBuilder message;
capnp::readMessageCopy(input, message);
auto root = message.getRoot<MyStruct>();
auto oldListOrphan = root.disownStructList();
auto oldList = oldListOrphan.getReader();
auto newList = root.initStructList(oldList.size() - 1);
for (auto i: kj::indices(newList)) {
  newList.setWithCaveats(i,
      oldList[i < indexToRemove ? i : i + 1]);
}
capnp::MallocMessageBuilder message2;
message2.setRoot(root.asReader());
capnp::writeMessage(output, message2);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="POCS">

{% highlight c++ %}
auto root = capnp::readMessageCopy<MyStruct>(input);
root.structList.erase(indexToRemove);
capnp::writeMessageCopy(output, root);
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="JSON-HTTP Bridge">

{% highlight capnp %}
interface AddressBook {
  getPerson @0 (id :UInt32 $httpPath)
            -> (person :Person $httpBody(type = json))
      $http(method = get, route = "person");
  # GET /person/<id>
  # JSON response body

  updatePerson @1 (id :UInt32 $httpPath,
                   person :Person $httpBody(type = json));
      $http(method = put, route = "person");
  # PUT /person/<id>
  # JSON request body
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="JSON-HTTP Bridge">

{% highlight capnp %}
addPerson @2 (person :Person $httpBody(type = json))
          -> (id :UInt32 $httpBody(type = jsonField));
    $http(method = post, route = "person");
# POST /person
# JSON request body
# JSON response body (object containing field `id`)

getAll @3 (page :UInt32 = 0 $httpQuery)
       -> (people: List(Person) $httpBody(type = json));
    $http(method = get);
# GET /?page=<num>
# Query is optional.
# JSAN (JSON array) repsonse body.
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" data-title="JSON-HTTP Bridge">

{% highlight capnp %}
interface AddressBookService {
  getAddressBook @0 (key :String $httpPath)
                 -> (result :AddressBook $httpPipeline);
      $http(route = "book");
  # GET /book/JrpmUduyHd8uW3x3TOXn2g/person/123
  # Becomes:
  #     service.getAddressBook("JrpmUduyHd8uW3x3TOXn2g").send()
  #            .getResult().getPerson(123).send()
  #
  # GET /book/JrpmUduyHd8uW3x3TOXn2g
  # Becomes:
  #     service.getAddressBook("JrpmUduyHd8uW3x3TOXn2g").send()
  #            .getResult().getAll().send()
}
{% endhighlight %}

</section>

<!--===================================================================================-->

<section markdown="1" id="slides-cover">

Questions?

</section>