1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// 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 io;
pub use 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 ;
pub use longshore_stream as stream;
pub use longshore_tcp as tcp;
pub use longshore_tls as tls;