n0-computer / iroh

A toolkit for building distributed applications
https://iroh.computer
Apache License 2.0
2.59k stars 163 forks source link

Allow specifying multiple ALPNs in `Endoint::connect` #2949

Open matheus23 opened 6 hours ago

matheus23 commented 6 hours ago

When implementing a backwards-compatible protocol, one wants to assign different ALPNs to different protocol versions. Examples:

It's important to indicate which versions you support, and often is the case that you indicate multiple versions simultaneously. E.g. sending an HTTP request using ["http/1.1", "h2", "h3"] as the ALPNs.

We currently don't have a way of specifying multiple ALPNs when doing an Endpoint::connect.

On the server side, you usually configure an ordered list of ALPNs and it chooses the first matching one. This is described in RFC 7301:

It is expected that a server will have a list of protocols that it supports, in preference order, and will only select a protocol if the client supports it. In that case, the server SHOULD select the most highly preferred protocol that it supports and that is also advertised by the client.

This manifests itself in APIs like that of rustls, its docs write:

The server can specify supported ALPN protocols by setting ServerConfig::alpn_protocols. During the handshake, the server will select the first protocol configured that the client supports.

The docs for ServerConfig::alpn_protocols say:

Protocol names we support, most preferred first.

dignifiedquire commented 4 hours ago

why do we need this, instead of doing

builder
  .accept("version0", my_protocol.clone())
  .accept("version1", my_protocol.clone())

which we should test, but ideally already works today

dignifiedquire commented 4 hours ago

Sorry, I understood now, you are talking about the connect part..