Skip to main content

StreamSession

Struct StreamSession 

Source
pub struct StreamSession { /* private fields */ }
Expand description

A client session of a pull, which reads the files and ref listings of a server.

connect opens a session over ssh, and over_stream opens one over a pair of byte streams. The session is Send + Sync. Concurrent get and list_refs calls share the pipeline, and the replies come in the order of the frames.

If the session is dropped without finish, it sends nothing more and closes its output, so the server reads the end of its input. This is also true while the caller holds a PullBody. If connect opened the session, the drop then drops the ssh client without a wait.

§Pipeline

The session opens with PullHello. It asks for files by their path with get, and for ref listings with list_refs. All requests share one stream as a pipeline. A call takes its place and writes its frame under one lock, so the frames go on the wire in the order of the places.

A reply carries no request id. The reply of a call is the next reply after the reply of the call before it.

At most max_outstanding calls hold a place at a time. A call holds its place from the write of its frame to the end of its reply.

If a get gets a GetReply whose found is false, that GetReply is the end of the reply. For each other reply, the end is the chunk that ends the body. When the session reads that chunk, it moves to the next reply, also while the caller still holds the PullBody.

§Failures

Each of these events is a failure, and a failure ends the session:

  • an Error message from the server
  • a reply of a kind that the session does not expect
  • a GetReply whose found is false, or that states no length, in reply to ListRefs
  • a stated length that is more than the cap of the call, or that is different from the sum of the chunks
  • a body that is longer than the cap of the call
  • a failed read or write
  • a drop of a get or list_refs future after the call took its place
  • a drop of a PullBody before its end
  • a finish while the body of a reply is not read to its end

After a failure, each later call and each later read of a body fails. Its error has the variant and the message of the first failure.

Implementations§

Source§

impl StreamSession

The open of a session over a pair of byte streams, and its calls.

Source

pub async fn over_stream<R, W>( input: R, output: W, opts: StreamSessionOptions, ) -> Result<Self>
where R: AsyncRead + Unpin + Send + 'static, W: AsyncWrite + Unpin + Send + 'static,

Opens a session over a pair of byte streams.

input carries the bytes from the server, and output the bytes to it. The session sends PullHello with PULL_PROTOCOL_VERSION and reads PullHelloReply.

The caller owns the liveness of the two streams. The session puts no time limit on a read. A peer that stays silent keeps a call waiting until the caller drops its future or closes the streams.

§Errors
  • An Error message from the server, as the Error variant of its code.
  • Error::VersionUnsupported if the reply states a version out of 1..=PULL_PROTOCOL_VERSION. The session then closes output and sends no Get.
  • Error::Protocol if the reply is a message of another kind, or if the codec refuses a frame of the reply.
  • Error::Protocol if agent holds a NUL byte.
  • Error::LimitExceeded if the PullHello frame or a frame of the reply is longer than MIN_FRAME_LIMIT.
  • Error::Io if a read or a write of a stream fails. If input ends before the reply, the kind is UnexpectedEof.

If the write of PullHello fails with Error::Io, the call then reads the message that the server can have sent before it closed. If that message is an Error message, the call returns its variant.

Source

pub async fn get(&self, path: &str, max_len: u64) -> Result<Option<PullBody>>

Asks for the file at path and waits for the head of its reply.

path is relative to the repository root. The call returns None if the server does not serve the path. The PullBody holds the body to max_len bytes and to its stated length.

If max_outstanding calls hold a place, the call waits. It then takes its place, and writes and flushes its Get frame under one lock.

The caller must read each body to its end, or drop it, before it awaits a later get. A later call waits for the end of each earlier body before it reads its own reply. If the future of the call is dropped after the call took its place, the session ends.

If the write of the Get frame fails, the call waits for the turn of its reply. It then reads the message that the server can have sent before it closed. If connect opened the session, this read takes the ssh time limits of PushSession::connect. If over_stream opened the session, this read has no time limit.

§Errors
  • Error::LimitExceeded if the Get frame of path is longer than MIN_FRAME_LIMIT. The session stays usable, and the call writes nothing.
  • Error::Protocol if path holds a NUL byte. The session stays usable, and the call writes nothing.
  • The repeat of the first failure, if the session failed before the call or while it waits.
  • Error::LimitExceeded if the stated length of the body is more than max_len. The session ends before it reads the body.
  • Error::LimitExceeded if a frame of the reply is longer than MIN_FRAME_LIMIT.
  • An Error message from the server, as the Error variant of its code.
  • Error::Protocol if the reply is a message of another kind, or if the codec refuses a frame of the reply.
  • Error::Io if a read or a write of the stream fails. If the stream ends before the reply, the kind is UnexpectedEof.

If the write of the Get frame fails and the server sent an Error message before it closed, the call returns the variant of that message. Otherwise it returns the Error::Io of the write.

A failure ends the session, as Failures states.

Source

pub async fn list_refs( &self, refs: &RefSelector, collections: Option<&RefSelector>, max_len: u64, ) -> Result<PullBody>

Asks for the ref listing of refs and collections and waits for the head of its reply.

refs selects the local refs. collections selects the collection refs by ID/NAME, and None asks for no collection ref. The call sends the entries of the two selectors by the rules of Ref patterns. The PullBody holds the listing to max_len bytes and to its stated length.

The call takes its place in the pipeline as get does, and it has the same rules for the body and for a drop of its future.

§Errors

A failure ends the session, as Failures states.

Source

pub async fn finish(self) -> Result<()>

Ends the session.

An end is clean if the session read the end of every reply. After a clean end, the session closes its output at a frame boundary. The call then returns Ok, whatever the exit status of the ssh client.

At any other end, the session closes its output and drops its input before it waits for the ssh client. Because the session drops the input first, a server that is blocked in a write of a body does not keep the client open. The session then fails with the error that the call returns, and a later read of a held body repeats that error.

If connect opened the session, the call then waits for the ssh client to exit. The wait takes the ssh time limits of PushSession::connect.

§Errors
  • The repeat of the first failure, if the session failed.
  • Error::InvalidInput if the body of a reply is not read to its end, for example a body that the caller still holds.
  • Error::Transport with the exit status, if the ssh client exited with a failure status and the error of the session is Error::Io. Only a session that connect opened returns it.
Source§

impl StreamSession

The open of a session over ssh.

Source

pub async fn connect( address: &SshAddress, options: &SshOptions, session: StreamSessionOptions, ) -> Result<StreamSession>

Opens a stream session to the ssh address address.

The call starts the ssh client, sends PullHello, and reads PullHelloReply. The command line of the ssh client comes from options by the rules of the ssh command line of a pull. session gives the agent in PullHello and the request limit of the session.

The session has the ssh time limits of PushSession::connect. finish waits for the ssh client as commit and abort of a push do. The session puts no time limit on a read of a reply.

Under the tokio backend, the call must run within a runtime that has the IO driver and the time driver enabled, as PushSession::connect states. The session runs on the runtime that opened it.

§Errors
§Examples
use futures_lite::AsyncReadExt;
use ostrya_remote::session::{StreamSession, StreamSessionOptions};
use ostrya_remote::transport::{SshAddress, SshOptions};
let address = SshAddress::parse("builder@repo.example.com:/srv/repo")?;
let options = SshOptions::default();
let session = StreamSession::connect(&address, &options, StreamSessionOptions::default()).await?;
if let Some(mut body) = session.get("config", 1 << 20).await? {
    let mut config = Vec::new();
    body.read_to_end(&mut config).await?;
}
session.finish().await?;
Source

pub fn check_command_line( address: &SshAddress, options: &SshOptions, ) -> Result<()>

Checks the command line of the ssh client that connect builds for address and options.

The call reads the OSTRYA_SSH_COMMAND environment variable, and applies the Empty values rules of a pull. It starts no ssh client. A caller that opens sessions to more than one remote can make this check for each remote before its first request.

§Errors
  • Error::InvalidInput if the ssh command or the send command is empty or holds only white space, or if the value of OSTRYA_SSH_COMMAND is not UTF-8.

Trait Implementations§

Source§

impl Drop for StreamSession

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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