Skip to main content

ReceiveEndpoint

Struct ReceiveEndpoint 

Source
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

  • handle takes 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_service of axum removes the prefix itself. The push address of a client is then https://HOST/api/v1/push.
  • The request body does not need to be Sync, so the body of axum fits. The response body is ReceiveBody.
  • Run the future of sweep once, in a task of its own, when the host starts. If no task runs it, no session reaches its idle timeout.
  • Call shutdown before 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 offers h2 through 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 tokio feature for a host on tokio. Under tokio, handle and sweep must 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 one Hello frame, opens a session. The response is 200 with the HelloReply frame. The header Ostrya-Session carries the session id: 64 lowercase hex digits of 32 bytes from the random source of the operating system.
  • POST session/ID/have, with one Have frame, gets HaveReply.
  • POST session/ID/objects, with one object stream, gets ObjectsReply. Up to EndpointOptions::parallel_uploads of these run at the same time in one session, and one more gets limit-exceeded.
  • POST session/ID/commit, with one Commit frame, gets CommitReply. The commit runs in a task of its own, so a disconnect, a DELETE, or the idle timeout does not stop it. If the session already commits, a second commit gets 422 with protocol, and the first commit goes on.
  • DELETE session/ID ends the session and the requests of the session in flight. It gets 204 with no body and no Content-Length. If the session commits, the DELETE gets 422 with protocol, 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-mismatch and non-fast-forward get 409.
  • internal gets 500.
  • Every other code gets 422.
  • An error of the server with no wire code gets 500 with internal and the text of the error.
  • If EndpointOptions::max_sessions sessions are open, a session request whose Hello passes its checks gets 503 with limit-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>

Source

pub fn new( repo: Repo, auth: A, options: EndpointOptions, ) -> Result<ReceiveEndpoint<A>>

Creates an endpoint over repo with the authentication auth.

§Errors
Source

pub async fn handle<B>(&self, req: Request<B>) -> Response<ReceiveBody>
where B: Body<Data = Bytes> + Send + Unpin + 'static, B::Error: Into<Box<dyn Error + Send + Sync>>,

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<A: ReceiveAuth> Debug for ReceiveEndpoint<A>

The debug text shows the options. It leaves out the repository and the authentication.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> !RefUnwindSafe for ReceiveEndpoint<A>

§

impl<A> !UnwindSafe for ReceiveEndpoint<A>

§

impl<A> Freeze for ReceiveEndpoint<A>
where Receive<A>: Freeze,

§

impl<A> Send for ReceiveEndpoint<A>
where Receive<A>: Send,

§

impl<A> Sync for ReceiveEndpoint<A>
where Receive<A>: Sync,

§

impl<A> Unpin for ReceiveEndpoint<A>
where Receive<A>: Unpin,

§

impl<A> UnsafeUnpin for ReceiveEndpoint<A>
where Receive<A>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more