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
Errormessage from the server - a reply of a kind that the session does not expect
- a
GetReplywhosefoundisfalse, or that states no length, in reply toListRefs - 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
getorlist_refsfuture after the call took its place - a drop of a
PullBodybefore its end - a
finishwhile 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.
impl StreamSession
The open of a session over a pair of byte streams, and its calls.
Sourcepub async fn over_stream<R, W>(
input: R,
output: W,
opts: StreamSessionOptions,
) -> Result<Self>
pub async fn over_stream<R, W>( input: R, output: W, opts: StreamSessionOptions, ) -> Result<Self>
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
Errormessage from the server, as theErrorvariant of its code. Error::VersionUnsupportedif the reply states a version out of1..=PULL_PROTOCOL_VERSION. The session then closesoutputand sends noGet.Error::Protocolif the reply is a message of another kind, or if the codec refuses a frame of the reply.Error::Protocolifagentholds a NUL byte.Error::LimitExceededif thePullHelloframe or a frame of the reply is longer thanMIN_FRAME_LIMIT.Error::Ioif a read or a write of a stream fails. Ifinputends before the reply, the kind isUnexpectedEof.
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.
Sourcepub async fn get(&self, path: &str, max_len: u64) -> Result<Option<PullBody>>
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::LimitExceededif theGetframe ofpathis longer thanMIN_FRAME_LIMIT. The session stays usable, and the call writes nothing.Error::Protocolifpathholds 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::LimitExceededif the stated length of the body is more thanmax_len. The session ends before it reads the body.Error::LimitExceededif a frame of the reply is longer thanMIN_FRAME_LIMIT.- An
Errormessage from the server, as theErrorvariant of its code. Error::Protocolif the reply is a message of another kind, or if the codec refuses a frame of the reply.Error::Ioif a read or a write of the stream fails. If the stream ends before the reply, the kind isUnexpectedEof.
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.
Sourcepub async fn list_refs(
&self,
refs: &RefSelector,
collections: Option<&RefSelector>,
max_len: u64,
) -> Result<PullBody>
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
Error::InvalidInputif the entries break a limit of Ref patterns. The session stays usable, and the call writes nothing.Error::LimitExceededif theListRefsframe is longer thanMIN_FRAME_LIMIT. The session stays usable, and the call writes nothing.Error::Protocolif the reply is aGetReplywhosefoundisfalse, or that states no length. The session fails.- Each failure of the session that
getlists. If the server refuses an entry, the error isError::Protocol. If the listing is over the cap of the server, the error isError::LimitExceeded.
A failure ends the session, as Failures states.
Sourcepub async fn finish(self) -> Result<()>
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::InvalidInputif the body of a reply is not read to its end, for example a body that the caller still holds.Error::Transportwith the exit status, if the ssh client exited with a failure status and the error of the session isError::Io. Only a session thatconnectopened returns it.
Source§impl StreamSession
The open of a session over ssh.
impl StreamSession
The open of a session over ssh.
Sourcepub async fn connect(
address: &SshAddress,
options: &SshOptions,
session: StreamSessionOptions,
) -> Result<StreamSession>
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
- An
Errormessage from the server, as theErrorvariant of its code. Error::InvalidInputif the ssh command or the send command is empty or holds only white space, or if the value ofOSTRYA_SSH_COMMANDis not UTF-8.Error::Transportif the ssh client cannot be started. The message names the program.Error::Transportif the session fails withError::Ioand the ssh client exits with a failure status. The message holds the status.Error::VersionUnsupportedif the reply has a pull version outside 1 toPULL_PROTOCOL_VERSION. The session then closes its output and sends noGet.Error::Protocolif the reply is notPullHelloReply.Error::Protocolif theagentofsessionholds a NUL byte.Error::LimitExceededif thePullHelloframe is more thanMIN_FRAME_LIMITbytes.Error::ProtocolorError::LimitExceededif the codec refuses a frame of the server, by the rules ofproto.Error::Ioif a read or a write of the session fails, or if the session ends before the reply.
§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?;Sourcepub fn check_command_line(
address: &SshAddress,
options: &SshOptions,
) -> Result<()>
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::InvalidInputif the ssh command or the send command is empty or holds only white space, or if the value ofOSTRYA_SSH_COMMANDis not UTF-8.