Skip to content

Commit 36571b0

Browse files
committed
ipip: Streaming Routing V1 HTTP API
1 parent 4a03ecd commit 36571b0

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/ipips/ipip-0410.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: "IPIP-0410: Streaming Routing V1 HTTP API"
3+
date: 2023-05-12
4+
ipip: proposal
5+
editors:
6+
- name: Henrique Dias
7+
github: hacdias
8+
url: https://hacdias.com/
9+
relatedIssues:
10+
- https://github.com/ipfs/specs/issues/344
11+
- https://github.com/ipfs/boxo/pull/18
12+
- https://github.com/ipfs/kubo/pull/9868
13+
- https://github.com/ipfs/kubo/pull/9874
14+
order: 410
15+
tags: ['ipips']
16+
---
17+
18+
Introduce backwards-compatible streaming support to the Routing V1 HTTP API.
19+
For this, we use the `Accept` HTTP header (:cite[rfc9110]) for content type negotiation, as well
20+
as the Newline Delimited JSON ([NDJSON]) format.
21+
22+
## Motivation
23+
24+
The main motivation for this change is to allow servers to respond faster to the
25+
client with provider records, as soon as they are available. In the current state,
26+
the client requests a list of providers for a CID from the server. Then, the client
27+
has to wait for the server to collect their final list of providers. After that,
28+
the server can respond with the full list of providers.
29+
30+
With streaming support, the server is able to respond with provider records as soon
31+
as they are available. This reduces latency and allows for faster content discovery.
32+
33+
In addition, streaming responses may produce an unlimited amount of results, which
34+
is not the case for non-streamed responses.
35+
36+
## Detailed Design
37+
38+
In summary, streaming is supported by using the `Accept` HTTP header, which is used
39+
for content type negotiation as described in :cite[rfc9110]. The client sends an
40+
`Accept` HTTP header starting with `application/x-ndjson`, which is the content
41+
type for [NDJSON]. The following happens:
42+
43+
- The client adds the `Accept` HTTP header in the request starting with `application/x-ndjson`.
44+
- The server checks the `Accept` HTTP header from the request and, if it contains
45+
`application/x-ndjson`, they reply with NDJSON. If they don't support NDJSON, they
46+
can reply with JSON.
47+
- The server response MUST contain a `Content-Type` HTTP header indicating the
48+
response type, which may be either `application/json` for non-streaming responses,
49+
and `application/x-ndjson` for streamed responses.
50+
51+
For more details regarding the design, check :cite[http-routing-v1].
52+
53+
## Design Rationale
54+
55+
This feature is designed such that it does not break compatibility with existing
56+
clients and servers. The `Accept` HTTP header is OPTIONAL. By default, the server
57+
MUST respond with `application/json` unless the client explicitly asked for
58+
`application/x-ndjson`. If the server does not support NDJSON, it is allowed
59+
to still respond with non-streamed JSON.
60+
61+
### User Benefit
62+
63+
Users (clients) will benefit from this change as the servers will now be able
64+
to respond more promptly to provider record requests. Instead of waiting for the whole
65+
list to be constructed, servers can now return each provider record one by one,
66+
in a streaming fashion.
67+
68+
### Compatibility
69+
70+
The introduced changes are backwards-compatible. The introduced header is completely
71+
optional, and a server that does not support streaming is able to respond with a non-streaming
72+
response to the client. Equally, non-streaming responses are the default. Therefore, a
73+
client that does not support streaming will not receive a streamed response.
74+
75+
### Security
76+
77+
Security considerations are equivalent as the ones in :cite[ipip-0337].
78+
79+
### Copyright
80+
81+
Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).
82+
83+
[NDJSON]: http://ndjson.org/

src/routing/http-routing-v1.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ editors:
1111
github: guseggert
1212
- name: Masih H. Derkani
1313
github: masih
14+
- name: Henrique Dias
15+
url: https://hacdias.com/
16+
github: hacdias
1417
xref:
1518
- ipns-record
1619
order: 0
@@ -139,7 +142,23 @@ This API does not support pagination, but optional pagination can be added in a
139142

140143
## Streaming
141144

142-
This API does not currently support streaming, however it can be added in the future through a backwards-compatible update by using a content type other than `application/json`.
145+
JSON-based endpoints support streaming requests made by sending an `Accept` HTTP Header containing
146+
`application/x-ndjson`. The response will be formatted as [Newline Delimited JSON (ndjson)](https://github.com/ndjson/ndjson-spec),
147+
with one *read provider record* per line:
148+
149+
150+
```json
151+
{"Protocol": "<protocol_name>", "Schema": "<schema>", ...}
152+
{"Protocol": "<protocol_name>", "Schema": "<schema>", ...}
153+
{"Protocol": "<protocol_name>", "Schema": "<schema>", ...}
154+
...
155+
```
156+
157+
Streaming is backwards-compatible with clients that do not support streaming. Please note the following:
158+
159+
- Requests without an `Accept` header MUST default to regular, non-streaming, responses.
160+
- The server MAY respond with non-streaming response even if the client requests streaming.
161+
- The server MUST NOT respond with streaming response if the client did not request so.
143162

144163
## Error Codes
145164

0 commit comments

Comments
 (0)