pub struct ReceiveEndpoint<A: ReceiveAuth> { /* private fields */ }Expand description
The receive endpoint of a push, which a host mounts in its own router.
The endpoint serves one repository and uses the authentication A. It
opens no listener and does no TLS. Server runs the
same endpoint with its built-in authentication methods.
§Mounting
handletakes a raw path that starts with/_ostrya/receive/v1/. If a host mounts the endpoint under a prefix, for example/api/v1/push, the host removes the prefix before the call.Router::nest_serviceof axum removes the prefix itself. The push address of a client is thenhttps://HOST/api/v1/push.- The request body does not need to be
Sync, so the body of axum fits. The response body isReceiveBody. - Run the future of
sweeponce, in a task of its own, when the host starts. If no task runs it, no session reaches its idle timeout. - Call
shutdownbefore the graceful shutdown of the listener. - Serve the endpoint on a dedicated listener, with a port or an SNI name of its own. A listener for other routes keeps its own windows.
- On that listener, set the HTTP/2 windows of
h2_windows. Set the maximum number of concurrent streams of one connection to 32, the value of the server. Both HTTP/2 settings apply only to a TLS listener that offersh2through ALPN, because the push client speaks HTTP/1.1 over cleartext. - The request bytes that the host did not read are at most the connection window times the number of open connections. The host grants the windows when it sets up a connection, before it authenticates a request.
- The runtime backend of the crate must be the runtime of the host: the
tokiofeature for a host on tokio. Under tokio,handleandsweepmust run within a tokio runtime with the time driver enabled.
§Responses
Each request under the raw path prefix /_ostrya/receive/v1/ is one
step of a push session of ReceiveService. The
path is not percent-decoded.
POST session, with a body of oneHelloframe, opens a session. The response is 200 with theHelloReplyframe. The headerOstrya-Sessioncarries the session id: 64 lowercase hex digits of 32 bytes from the random source of the operating system.POST session/ID/have, with oneHaveframe, getsHaveReply.POST session/ID/objects, with one object stream, getsObjectsReply. Up toEndpointOptions::parallel_uploadsof these run at the same time in one session, and one more getslimit-exceeded.POST session/ID/commit, with oneCommitframe, getsCommitReply. The commit runs in a task of its own, so a disconnect, aDELETE, or the idle timeout does not stop it. If the session already commits, a secondcommitgets 422 withprotocol, and the first commit goes on.DELETE session/IDends the session and the requests of the session in flight. It gets 204 with no body and noContent-Length. If the session commits, theDELETEgets 422 withprotocol, and the commit goes on.
These requests get 404 with no body:
- a path under the prefix that names no route
- an id that is not a session of the owner of the request
- a session that ended
A known path with another method, also GET and HEAD, gets 405 with
Allow: POST or Allow: DELETE. The body of session, have, and
commit holds one frame of at most 1 MiB. An empty body or a byte after
the frame is a protocol error.
Each other response body is one frame. An error is an Error frame, and
its code sets the status:
ref-mismatchandnon-fast-forwardget 409.internalgets 500.- Every other code gets 422.
- An error of the server with no wire code gets 500 with
internaland the text of the error. - If
EndpointOptions::max_sessionssessions are open, asessionrequest whoseHellopasses its checks gets 503 withlimit-exceeded. - If the authentication refuses a request, the request gets 401 or 403
with
unauthorized.
No response that the endpoint builds itself carries Content-Type or
Retry-After. A refusal of the authentication carries the headers that
the authentication adds to it.
A refusal can come before the endpoint reads the body, for example a 404
or a 405. Before such a refusal, the endpoint reads and drops up to 1 MiB
of the body. The time limit of this read is the idle timeout or 5
seconds, whichever is shorter. On HTTP/1.1, if the body did not reach its
end, the response gets Connection: close.
§Sessions
A failed step ends its session. The other requests of the session in
flight get 422 with protocol and the cause.
If a request ends before its response, for example because the client closes the connection, its session ends too, unless the session commits. A request body can fail, for example because the client closes the connection in the middle of the body. The cause is then that of a request that ended before its response.
If a session has no request in progress for
EndpointOptions::session_idle_timeout, the endpoint aborts it. The
endpoint also aborts a session with a request body that delivers no byte
for that time. While the endpoint does not read a request body, that body
does not count as silent. The body of a session request must arrive in
full within the idle timeout.
The future of sweep applies the timeout. A
session that commits is never aborted. It holds its slot until the commit
ends, also if the commit fails or panics.
shutdown aborts every session that does
not commit. A session request after the call gets 503. A commit that
runs goes on to its end.
The endpoint reads the repository settings once, at start. A change of the settings applies at the next start.
§Examples
A host that mounts the endpoint under the prefix /api/v1/push:
use std::sync::Arc;
use hyper::body::Incoming;
use hyper::{Request, Response};
use ostrya_server::{ReceiveAuth, ReceiveBody, ReceiveEndpoint};
// Runs the idle sweep in a task of its own, once, when the host starts.
fn start<A: ReceiveAuth>(endpoint: &Arc<ReceiveEndpoint<A>>) {
let endpoint = endpoint.clone();
drop(ostrya_rt::spawn(async move { endpoint.sweep().await }));
}
// Removes the prefix of the host, then lets the endpoint answer.
async fn route<A: ReceiveAuth>(
endpoint: &ReceiveEndpoint<A>,
mut req: Request<Incoming>,
) -> Response<ReceiveBody> {
let path = req.uri().path().strip_prefix("/api/v1/push").unwrap_or("/");
*req.uri_mut() = path.parse().expect("a path is a valid URI");
endpoint.handle(req).await
}Implementations§
Source§impl<A: ReceiveAuth> ReceiveEndpoint<A>
impl<A: ReceiveAuth> ReceiveEndpoint<A>
Sourcepub fn new(
repo: Repo,
auth: A,
options: EndpointOptions,
) -> Result<ReceiveEndpoint<A>>
pub fn new( repo: Repo, auth: A, options: EndpointOptions, ) -> Result<ReceiveEndpoint<A>>
Creates an endpoint over repo with the authentication auth.
§Errors
Error::Optionsifparallel_uploadsis outside1..=31.Error::Optionsifsession_idle_timeoutis zero.Error::Optionsifmax_sessionsis zero.
Sourcepub async fn handle<B>(&self, req: Request<B>) -> Response<ReceiveBody>
pub async fn handle<B>(&self, req: Request<B>) -> Response<ReceiveBody>
Returns the response to one request of the endpoint.
The raw request path must start with /_ostrya/receive/v1/. The path
is not percent-decoded, and the endpoint ignores the query.
Mounting states how a host removes its
prefix. Responses states the routes and
the responses.
A request body that fails counts as a request that ended before its
response. The request gets 500 with internal, and its session ends.
Under the tokio backend, the call must run within a tokio runtime with the time driver enabled. The commit runs in a task of its own, and the time limits use the timer. A server signer that runs the GnuPG binaries also needs the IO driver.
§Panics
A panic in the commit or in a hook of the session is not caught. The commit runs in a task of its own. The panic resumes in this call when the call joins that task, so it panics the request task of the host.
Over HTTP/2, hyper then resets the stream. Over HTTP/1.1, hyper closes
the connection. The session ends and frees its slot. A panic in
ReceiveHooks::after_update
occurs after the endpoint writes the refs.
If the future of handle is dropped first, for example because the
client resets the stream, the commit task becomes detached. The
runtime then catches the panic and drops it.
Sourcepub async fn sweep(&self)
pub async fn sweep(&self)
Aborts each session past its idle timeout, until
shutdown.
The future also aborts each session with a request body that delivered
no byte for that time. A session that commits is never aborted. After
shutdown, the future completes at once.
The host runs the future once, in a task of its own, while the endpoint serves. If no task runs it, no session reaches its idle timeout. Under the tokio backend, the future must run within a tokio runtime with the time driver enabled.
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Stops the endpoint.
The call aborts each session that does not commit, and ends
sweep. A commit that runs goes on to its
end, also through the hooks of its session. A session request after
the call gets 503. A second call has no further effect.
Sourcepub fn h2_windows(&self) -> (u32, u32)
pub fn h2_windows(&self) -> (u32, u32)
Returns the HTTP/2 receive windows for a connection to the endpoint.
The first value is the window of each stream, 2 MiB. The second value
is the window of the connection: 2 MiB for each of the
parallel_uploads object streams
of a session. The request bodies of one connection hold at most this
number of bytes that the host did not read.