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, which makes no promise of Sync.
  • The endpoint is Send + Sync and has no Clone. A host shares it in an Arc.
  • 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 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>

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 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.

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> Forget for ReceiveEndpoint<A>

§

impl<A> Freeze for ReceiveEndpoint<A>
where *const ArcInner<RepoInner> is !null: Freeze, PhantomData<ArcInner<RepoInner>>: Freeze, A: Freeze, *const ArcInner<dyn Fn(ReceiveReport) + Sync + Send> is !null: Freeze, PhantomData<ArcInner<dyn Fn(ReceiveReport) + Sync + Send>>: Freeze, *const ArcInner<SessionTable> is !null: Freeze, PhantomData<ArcInner<SessionTable>>: Freeze, *const ArcInner<Shutdown> is !null: Freeze, PhantomData<ArcInner<Shutdown>>: Freeze,

§

impl<A> Move for ReceiveEndpoint<A>

§

impl<A> Send for ReceiveEndpoint<A>
where Arc<RepoInner>: Send, Arc<dyn Fn(ReceiveReport) + Sync + Send>: Send, Arc<SessionTable>: Send, Arc<Shutdown>: Send,

§

impl<A> Sync for ReceiveEndpoint<A>
where Arc<RepoInner>: Sync, Arc<dyn Fn(ReceiveReport) + Sync + Send>: Sync, Arc<SessionTable>: Sync, Arc<Shutdown>: Sync,

§

impl<A> Unpin for ReceiveEndpoint<A>
where A: Unpin, Arc<RepoInner>: Unpin, Arc<dyn Fn(ReceiveReport) + Sync + Send>: Unpin, Arc<SessionTable>: Unpin, Arc<Shutdown>: Unpin,

§

impl<A> UnsafeUnpin for ReceiveEndpoint<A>
where *const ArcInner<RepoInner> is !null: UnsafeUnpin, PhantomData<ArcInner<RepoInner>>: UnsafeUnpin, A: UnsafeUnpin, *const ArcInner<dyn Fn(ReceiveReport) + Sync + Send> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn(ReceiveReport) + Sync + Send>>: UnsafeUnpin, *const ArcInner<SessionTable> is !null: UnsafeUnpin, PhantomData<ArcInner<SessionTable>>: UnsafeUnpin, *const ArcInner<Shutdown> is !null: UnsafeUnpin, PhantomData<ArcInner<Shutdown>>: 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