longshore 0.3.0

Object storage client for Linux io_uring: one thread, memory allocated at start-up, object bodies sent without copying
Documentation
// Copyright 2026 Carver Automation Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// SPDX-License-Identifier: Apache-2.0

//! An object storage client for Linux `io_uring` and portable POSIX backends.
//!
//! longshore moves a lot of data with little CPU. One thread drives
//! everything from one completion queue (`io_uring` on Linux, or the
//! portable `kqueue`/`epoll` engine elsewhere). Buffers, connection slots
//! and operation slots are allocated when the client is built, and the
//! client makes no copy of an upload's body.
//!
//! # Highlights
//!
//! - One handle, [`client::Client`], that any thread can share, with calls
//!   that are futures and can also block.
//! - The client makes no copy of an upload's body. By default it reads the
//!   body once to compute its checksum (off with `checksums = false`), and
//!   over HTTPS sealing reads it once more and writes the records that are
//!   sent. A send of 64 KiB or more is submitted as a zero-copy send. A
//!   smaller send, and what remains after a short send over HTTPS, is an
//!   ordinary send that the kernel copies.
//! - No allocation after the client is built, apart from the TLS handshake.
//! - By default a checksum goes with every upload for the store to verify,
//!   and a download is checked against the checksum the store returns.
//! - No async runtime of its own. It works under any executor and from
//!   plain threads.
//! - Every protocol layer runs unchanged in a deterministic simulator (the
//!   `sim` module, behind the `sim` feature), which is how the retry and
//!   failure paths are tested.
//!
//! # Stores
//!
//! | Store | State |
//! |---|---|
//! | Amazon S3 | Works over HTTP and HTTPS |
//! | S3-compatible stores | Bucket in the host name or in the path. Run against Linode Object Storage and versitygw |
//! | Google Cloud Storage | Works with a bearer token |
//! | Azure Blob Storage | Request signing only. The store cannot use it yet |
//!
//! # Platform
//!
//! Linux 6.1 or later, on x86-64 or AArch64, uses `io_uring` by default and
//! can select the portable `epoll` backend via [`client::Builder::portable`].
//! macOS runs on the portable `kqueue` backend (requesting CPU pinning via
//! [`client::Builder::pin`] returns [`client::BuildError::Setting`]). The
//! crate builds on other systems and the simulator runs there, while
//! [`client::Builder::build`] returns [`client::BuildError::Unsupported`].
//!
//! In a container two things have to be set on the node. The runtime's
//! default seccomp profile refuses the `io_uring` system calls, so the
//! container needs a profile that allows `io_uring_setup`, `io_uring_enter`
//! and `io_uring_register`. And the buffers are locked memory, so
//! `RLIMIT_MEMLOCK` has to be at least the size of the client, where a
//! container commonly gets 8 MiB. The repository's documentation says how
//! each was done on Kubernetes.
//!
//! # Using this crate
//!
//! ## Building a client
//!
//! A [`client::Builder`] holds the settings. Change the ones that differ
//! from the defaults with its setters, which are named after the settings
//! and chain, then call `build`, which checks them and starts the I/O
//! threads.
//!
//! ```no_run
//! use longshore::client::Builder;
//! use longshore::store::creds::Credentials;
//!
//! let credentials = Credentials::new("ACCESS_KEY", "SECRET_KEY").unwrap();
//! let client = Builder::new("my-bucket.s3.us-east-1.amazonaws.com", "us-east-1", credentials)
//!     .tls(true)
//!     .port(443)
//!     .threads(4) // four independent I/O threads
//!     .build()
//!     .unwrap();
//! ```
//!
//! A store that expects the bucket in the path takes the endpoint as the
//! host and the bucket separately:
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let credentials = Credentials::new("ACCESS_KEY", "SECRET_KEY").unwrap();
//! let settings = Builder::new("objects.example.com", "us-east-1", credentials)
//!     .bucket_in_path(Some("my-bucket".into()));
//! ```
//!
//! ## Storing and fetching
//!
//! A body lives in a [`client::Buffer`] borrowed from the client. An
//! operation takes the buffer and hands it back with its result, so the
//! same buffer can carry the next request.
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! let mut body = client.buffer_blocking();
//! body.as_mut_slice()[..5].copy_from_slice(b"hello");
//!
//! let (stored, body) = client.store("greeting", body, 5).wait();
//! stored.unwrap();
//!
//! let (fetched, body) = client.fetch("greeting", body).wait();
//! assert_eq!(fetched.unwrap(), Some(5)); // None would mean no such object
//! assert_eq!(&body.as_slice()[..5], b"hello");
//!
//! client.erase("greeting").wait().unwrap();
//! ```
//!
//! ## Storing only if nothing is there
//!
//! [`client::Client::store_no_clobber`] stores an object only when the key
//! is free. It says whether this call stored it.
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! # let body = client.buffer_blocking();
//! let (stored, body) = client.store_no_clobber("lock", body, 0).wait();
//! match stored.unwrap() {
//!     true => println!("this call stored it"),
//!     false => println!("an object was already there"),
//! }
//! ```
//!
//! ## Reading part of an object
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! # let into = client.buffer_blocking();
//! // Bytes 0 to 1023, both ends included.
//! let (fetched, into) = client.fetch_range("video.mp4", 0, 1023, into).wait();
//! let len = fetched.unwrap().expect("no such object");
//! let bytes = &into.as_slice()[..len];
//! ```
//!
//! ## Objects larger than one buffer
//!
//! One operation moves at most one buffer. A stream moves an object of any
//! size a buffer at a time: [`client::Client::upload`] sends each full
//! buffer as one part of a multipart upload, and
//! [`client::Client::download`] fetches ranges of one buffer and checks
//! the whole object against the store's checksum at the end.
//!
//! ```no_run
//! # use longshore::client::{Builder, Piece};
//! # use longshore::store::creds::Credentials;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! # let bytes = vec![0u8; 20 << 20];
//! let mut upload = client.upload("big.bin").wait().unwrap();
//! upload.write(&bytes).wait().unwrap(); // or `send` a filled buffer, with no copy
//! upload.finish().wait().unwrap();
//!
//! let mut download = client.download("big.bin").wait().unwrap().expect("stored above");
//! while let Piece::Bytes(buffer, n) = download.read_blocking().unwrap() {
//!     println!("{n} bytes"); // in `buffer`, until it is dropped
//! }
//! ```
//!
//! [`client::Client::store_from`] and [`client::Client::fetch_into`] do
//! the same from and into the caller's own slice, at the cost of one copy.
//!
//! A caller that places the parts itself uses a multipart upload directly:
//! begin it, store each buffer as a numbered part, and complete it with the
//! parts in order. Parts are separate operations, so several can be in
//! flight at once.
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! let upload = client.upload_begin("big.bin").wait().unwrap();
//!
//! let mut parts = Vec::new();
//! let mut body = client.buffer_blocking();
//! for number in 1..=3 {
//!     let len = body.len(); // fill the buffer here
//!     let (part, back) = client.upload_part("big.bin", &upload, number, body, len).wait();
//!     parts.push(part.unwrap());
//!     body = back;
//! }
//!
//! // The list of parts is written into the buffer passed here.
//! let (done, _body) = client.upload_complete("big.bin", &upload, &parts, body).wait();
//! done.unwrap();
//! ```
//!
//! An upload that is neither completed nor aborted keeps its parts in the
//! store. [`client::Client::upload_abort`] discards one.
//!
//! ## Asking about objects
//!
//! [`client::Client::stat`] says whether an object exists, and its size,
//! tag and stored checksum, without fetching it. [`client::Client::list`]
//! returns a page of keys under a prefix as the store's document, which
//! [`store::list::Listing`] reads in place.
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! use longshore::store::list::Listing;
//! # let client = Builder::new("b.s3.us-east-1.amazonaws.com", "us-east-1", Credentials::new("K", "S").unwrap()).build().unwrap();
//! if let Some(stat) = client.stat("big.bin").wait().unwrap() {
//!     println!("{} bytes", stat.size());
//! }
//! let (len, page) = client.list("logs/", Some(b'/'), 1000, client.buffer_blocking()).wait();
//! let listing = Listing::parse(&page.as_slice()[..len.unwrap()]).unwrap();
//! for entry in listing.entries().flatten() {
//!     println!("{:?}", entry.key_raw());
//! }
//! ```
//!
//! `longshore_s3`'s `examples/` directory has a complete program for each
//! of these forms, run against a store the environment names.
//!
//! ## From async code
//!
//! Every operation returns a [`client::Pending`], which is a `Future`. The
//! same calls work under any executor. A call waits when no buffer or
//! operation slot is free, so nothing queues without bound.
//!
//! ```no_run
//! use longshore::client::{Client, Error};
//!
//! async fn save(client: &Client, key: &str, bytes: &[u8]) -> Result<(), Error> {
//!     let mut body = client.buffer().await;
//!     body.as_mut_slice()[..bytes.len()].copy_from_slice(bytes);
//!     let (stored, _body) = client.store(key, body, bytes.len()).await;
//!     stored
//! }
//! ```
//!
//! ## Knowing exactly what the store answered
//!
//! `store` and `fetch` retry and decide what an answer means. A caller that
//! has to account for every request uses [`client::Client::put`],
//! [`client::Client::head`] and [`client::Client::get`]. They take a
//! [`client::Budget`] of attempts and a deadline, and return a
//! [`store::report::Report`] of what was sent and what the store answered,
//! without turning a status into success or failure.
//!
//! ## Credentials that change
//!
//! Temporary credentials expire. Give the builder a file and each I/O
//! thread rereads it, tries new credentials with one request, and adopts
//! them if the store accepts them.
//!
//! ```no_run
//! # use longshore::client::Builder;
//! # use longshore::store::creds::Credentials;
//! # let credentials = Credentials::new("ACCESS_KEY", "SECRET_KEY").unwrap();
//! let settings = Builder::new("my-bucket.s3.us-east-1.amazonaws.com", "us-east-1", credentials)
//!     .credentials_file(Some("/run/secrets/s3-credentials".into()))
//!     .credentials_reread_secs(60);
//! ```
//!
//! ## Memory
//!
//! Everything is allocated when the client is built, per I/O thread:
//! [`client::Builder::buffers`] buffers of [`client::Builder::buffer_size`]
//! bytes lent to callers, and what [`client::Builder::connections`]
//! connections need. The defaults are 64 buffers of 8 MiB and 64
//! connections for each thread. `buffer_size` is the largest object one
//! operation moves. The buffers are registered with the kernel, which locks
//! them in memory.
//!
//! # The store underneath
//!
//! [`store`] is the single-threaded API the client is built on. Its caller
//! owns the event loop, gives the store registered buffers, submits each
//! operation with a token and collects outcomes by polling. It has what the
//! client does not expose yet: copy, batch erase, object metadata and
//! presigned URLs. [`client::Worker`] is one I/O thread's
//! logic without the thread, for a program that already runs an event loop
//! on each core.
//!
//! # Features
//!
//! - `aws-lc` (default, also named `tls`): TLS 1.3 with AWS-LC sealing and
//!   opening records, no copy, and rustls's AWS-LC provider for the
//!   handshake.
//! - `ring`: TLS 1.3 with ring sealing and opening records, one copy per
//!   record, and rustls's ring provider. No AWS-LC in the build.
//! - `rustls`: TLS 1.3 entirely from rustls, its record layer included, with
//!   its ring provider. rustls allocates a buffer for each record it seals;
//!   that runs inside the allocation exception the handshake uses.
//! - None of the three: plain HTTP, with neither `longshore_tls`, `rustls`
//!   nor AWS-LC. With several, [`client::Builder::tls_provider`] chooses.
//! - `sim`: the deterministic simulator, for tests.
//! - `fault-injection`: lets a test break a running client's ring. Off
//!   everywhere else.
//!
//! # Crates
//!
//! This crate holds no code. It gathers the crates the client is made of
//! under one name:
//!
//! - [`io`], from `longshore_common`: the [`io::Io`] trait every layer is
//!   written against, the `io_uring` backend, and the portable POSIX
//!   (`kqueue`/`epoll`) backend.
//! - [`mailbox`], from `longshore_mailbox`: bounded lock-free mailboxes
//!   between I/O cores and caller waiter slots.
//! - [`dns`], [`stream`], [`http`]: the protocols, from `longshore_dns`,
//!   `longshore_stream` and `longshore_http_client`.
//! - [`tcp`], from `longshore_tcp`: connections for protocols that run
//!   straight over TCP.
//! - [`file`](mod@file), from `longshore_file`: file I/O, one API on every
//!   backend.
//! - `tls` (with a TLS provider feature), from `longshore_tls`: TLS 1.3
//!   handshake and record layer.
//! - [`checksum`], from `longshore_crc`.
//! - [`store`], from `longshore_s3`: the object store API: operations,
//!   signing, retries, listing and credentials.
//! - [`client`], from `longshore_s3`: a handle any thread can share, over
//!   one or more I/O threads that each run a store.
//! - [`crypto`], from `longshore_s3`: the request-signing primitives.
//! - `sim` (feature `sim`), from `longshore_common`: a deterministic
//!   simulator that stands in for the kernel in tests.

pub use longshore_common::io;
#[cfg(feature = "sim")]
pub use longshore_common::sim;
pub use longshore_crc as checksum;
pub use longshore_dns as dns;
pub use longshore_file as file;
pub use longshore_http_client as http;
pub use longshore_mailbox as mailbox;
pub use longshore_s3 as store;
pub use longshore_s3::{client, crypto};
pub use longshore_stream as stream;
pub use longshore_tcp as tcp;
#[cfg(feature = "tls-core")]
pub use longshore_tls as tls;