Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP retrieval proposal #747

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

HTTP retrieval proposal #747

wants to merge 16 commits into from

Conversation

hsanjuan
Copy link
Contributor

@hsanjuan hsanjuan commented Dec 9, 2024

This is a proposal to add HTTP retrieval to Boxo. The current state is highly WIP, but I successfully retrieved something over HTTP, so posting to initiate a discussion over the approach and if we want to pursue it until the end.

Approach

The high-level idea is that most of what lives in bitswap/client is actually an "exchange" implementation, with the only real "Bitswap" thing being that bitswap/network sends HAS/GET requests over bitswap-protocol streams. As such, we should be able to complement bitswap/network with an HTTP-retrieval implementation which, instead of fetching things over the bitswap protocol, calls HTTP endpoints as indicated by the provider's /http addresses entries.

Note that conceptually at least, this is not adding HTTP retrieval into bitswap, but promoting most of the bitswap code to be a reference "Exchange" implementation, which is re-usable for different retrieval protocols (bitswap, http...). That is, we would be talking of an "exchange network" component and not a "bitswap network" component. Renames to this extent are still missing.

Implementation

In order to introduce an http-retrieval "exchange network" we need to:

  • Know when something should be retrieved via HTTP - that is, an item has an /http provider.
  • Use HTTP network for that.

To this end:

  • We have a router which select the http-network or the bitswap-network (or both) based on the existance of /http addresses in the peerstore of the given peer.
  • We have implemented an http-network as a PoC that performs GET requests to /http endpoints when handling a WANT.

image

In my tests plugging it to Kubo, the http-network can be used to retrieve content from a gateway over http. 🥳

The main advantange to this approach is that it is relatively clean to incorporate to the codebase, and keeps most of the code untouched, without having to duplicate any of the complex areas.

Challenges

  • Connectivity tracking is not implemented yet and we will have to see to what extent it can be implemented (I'm guessing we can plug into the TCP dialer directly).
  • Options like timeouts etc. are not implemented
  • We use a single HTTP client rather than a pool
  • Of course testing is fully lacking.

Bitswap places a lot of importance on managing connectivity events to peers. We avoid requesting things from peers that have not signaled connectivity, we clean peers that have disconnected and re-queue things for peers that disconnect. Thus it seems we must support http-connectivity events. When a libp2p peer connects for bitswap, we know that the connection is setup, handshake has been performed and protocol negotiation has happened. For HTTP these things may not exist so we need to define what means "Connected" (i.e. in the case of https it would mean we have completed SSL handshakes).

Apart from that, the question is what are the elements in the current bitswap/client stack that do not apply to HTTP (peerqueues, messagequeues, broadcast, wantsending, prioritization etc.)... and why not? What if a peer disconnects from bitswap but not from http or vice-versa? What if Latency is much worse for bitswap than for http? Perhaps this is all logic for the network-router to know how to choose which network to use to send messages.

Otherwise perhaps it is not possible to have a satisfactory implementation this way and we need to start thinking what to copy-paste into a separate "http-exchange" (at least the client part).

Related: #608

@hsanjuan hsanjuan self-assigned this Dec 9, 2024
@hsanjuan hsanjuan requested a review from a team as a code owner December 9, 2024 19:44
Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @hsanjuan, would be extremely nice if we can pull it off with such small set of changes.

Once we have HTTP basics like user-agent, status code metrics, 503/429/Retry-After (details inline), this is worth testing on Rainbow staging (do A/B test with bitswap-only box and bitswap+http).

ps. Whatever we do, HTTP should be opt-in, with a big EXPERIMENTAL warning.

return nil, fmt.Errorf("failed to extract host: %w", err)
}
host = hostVal
case "tcp":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: include "udp" to future proof HTTP/3 deployments?

Comment on lines +30 to +31
case "http", "https":
schema = comp.Name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be enough due to resolved multiaddrs that have explicit /tls/http or even /tls/sni/../

  • /ip4/..../tcp/../tls/http
  • /ip4/..../tcp/../tls/sni/example.com/http (for example, libp2p.direct peers could announce HTTP capability this way)
    • here we need to construct https://example.com (or manually ensure request is sent with correct SNI and Host header)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm to clarify... why wouldn't libp2p direct use a /dns/<libp2p-direct-hostname>/tcp/.../https address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lidel sorry, is a multiaddr in the form /ip4/..../tcp/.../tls/http valid? does it mean "do not verify certificate?

For things in the form /ip4/..../tcp/../tls/sni/example.com/http... yeah should setup Host header (they have been resolved and we don't want to re-do that).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hsanjuan hm.. /ip4/.../tls/http should be valid as-is – some CAs like ZeroSSL.com allow certs for IP instead of DNS name, and they should pass verification just fine

bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Outdated Show resolved Hide resolved
bitswap/network/router.go Outdated Show resolved Hide resolved
bitswap/network/httpnet/httpnet.go Show resolved Hide resolved
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

Attention: Patch coverage is 55.68797% with 409 lines in your changes missing coverage. Please review.

Project coverage is 60.31%. Comparing base (6397847) to head (6d063e0).

Files with missing lines Patch % Lines
bitswap/network/httpnet/httpnet.go 60.50% 102 Missing and 7 partials ⚠️
bitswap/network/httpnet/pinger.go 12.71% 102 Missing and 1 partial ⚠️
bitswap/network/httpnet/msg_sender.go 76.25% 58 Missing and 13 partials ⚠️
bitswap/network/router.go 0.00% 67 Missing ⚠️
bitswap/network/http_multiaddr.go 52.63% 24 Missing and 3 partials ⚠️
bitswap/network/bsnet/ipfs_impl.go 28.57% 20 Missing ⚠️
bitswap/network/httpnet/request_tracker.go 81.57% 5 Missing and 2 partials ⚠️
bitswap/network/httpnet/transport.go 76.92% 2 Missing and 1 partial ⚠️
bitswap/testnet/virtual.go 80.00% 2 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #747      +/-   ##
==========================================
- Coverage   60.46%   60.31%   -0.16%     
==========================================
  Files         245      252       +7     
  Lines       31133    32023     +890     
==========================================
+ Hits        18826    19314     +488     
- Misses      10627    11001     +374     
- Partials     1680     1708      +28     
Files with missing lines Coverage Δ
bitswap/client/client.go 88.81% <100.00%> (+2.09%) ⬆️
bitswap/client/internal/peermanager/peermanager.go 91.79% <100.00%> (-0.07%) ⬇️
bitswap/network/bsnet/options.go 50.00% <ø> (ø)
bitswap/network/connecteventmanager.go 88.54% <100.00%> (+2.29%) ⬆️
bitswap/server/server.go 55.37% <100.00%> (ø)
bitswap/testinstance/testinstance.go 86.44% <ø> (ø)
bitswap/testnet/peernet.go 38.46% <100.00%> (ø)
examples/bitswap-transfer/main.go 41.21% <ø> (ø)
bitswap/testnet/virtual.go 70.38% <80.00%> (-0.23%) ⬇️
bitswap/network/httpnet/transport.go 76.92% <76.92%> (ø)
... and 7 more

... and 11 files with indirect coverage changes

This and subsequent commits introduce an httpnet module at what is known as
the "bitswap network layer". The bitswap network layer connects bitswap-peers,
sends bitswap messages and receives responses.

Bitswap messages are basically a wantlist, a list of CIDs that should be sent
if available.

httpnet does the same, except instead of sending the bitswap message over
bitswap, it triggers http requests for the requested blocks. httpnet is a
drop-in addon so that we can request blocks over http, and not only via bitswap.

As httpnet is a network, it benefits from all existing wantlist management
logic. Any http/2 endpoint should benefit from streamlined requests on a
single http connection. A router-network ensures that messages are correctly
handled by bitswap or by http requests depending on what the peers are
advertising. HTTP requests are given priority in the presence of both.

Here are some of the httpnet features:

* Peers are marked as Connected when they are able to handle http requets.
* Peers are marked as Disconnected when http requests fail repeatedly (MaxRetries).
* Server errors trigger backoffs preventing more requests to happen to the same
  url for a period (Retry-After header or configuration value)
* We support several urls per peer, meaning a peer can provide alternative
  http endpoints which are tried based on number of failures or existing cooldowns.
* We translate HAVE requests to HTTP-HEAD requests and BLOCK requests to HTTP-GETs
* We support cancellations: ongoing or soon to happen requests for a CID
  can be cancelled using a "cancel" entry in the wantlist.
* We record latency information for peers by pinging regularly.
* We discriminate between different errors so that we know whether to
  move to the next block in a wantlist, or to retry with a different url,
  or to completely abort.
* Options to configure user-agent, max retries etc. are supported.
@hsanjuan hsanjuan requested a review from a team January 16, 2025 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants