|
| 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/ |
0 commit comments