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, which makes no promise ofSync. - The endpoint is
Send + Syncand has noClone. A host shares it in anArc. - 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 receive endpoint specifies the routes, the request bodies, the
session id, the statuses, and the end of a session. Error codes gives
the status of each wire code.
Authentication of the receive endpoint gives the refusals of the
authentication.
EndpointOptions::parallel_uploads is the number of objects requests
that one session runs at the same time. One more objects request gets
422 with limit-exceeded, and the session ends.
EndpointOptions::max_sessions is the number of sessions that are open
at the same time. A session request past the limit gets 503 with
limit-exceeded. A refusal of the authentication carries the headers
that the ReceiveAuth adds to it, except Content-Length,
Transfer-Encoding, and Connection, which the endpoint sets itself.
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 and HTTP/1.0, if the body did
not reach its end, the response gets Connection: close.
§Sessions
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 links the specification
of 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,
unless the session commits.
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.