pub struct IoPool { /* private fields */ }Expand description
A small pool of dedicated writer threads fed by a bounded channel.
See the module docs for the full design rationale (issue #569 phase 1, ADR-0001).
§Examples
use ssg::io_pool::IoPool;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let pool = IoPool::with_threads(2);
pool.write(dir.path().join("page.html"), b"<html/>".to_vec()).unwrap();
pool.flush().unwrap();
assert!(dir.path().join("page.html").exists());Implementations§
Source§impl IoPool
impl IoPool
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a pool with the default writer count:
min(4, max(1, available_parallelism / 2)).
§Examples
use ssg::io_pool::IoPool;
let pool = IoPool::new();
pool.flush().unwrap(); // empty pool flushes triviallySourcepub fn with_threads(threads: usize) -> Self
pub fn with_threads(threads: usize) -> Self
Creates a pool with an explicit writer-thread count.
threads is clamped to the 1..=4 range: zero writers would
deadlock producers, and more than four writers only adds seek
contention on the output disk.
§Examples
use ssg::io_pool::IoPool;
let pool = IoPool::with_threads(0); // clamped to 1
pool.flush().unwrap();Sourcepub fn write(
&self,
path: impl Into<PathBuf>,
bytes: Vec<u8>,
) -> Result<(), SsgError>
pub fn write( &self, path: impl Into<PathBuf>, bytes: Vec<u8>, ) -> Result<(), SsgError>
Enqueues a write of bytes to path.
Blocks when the bounded queue is full (backpressure). The
write itself happens asynchronously on a writer thread; any
failure is captured and reported by the next flush.
Returns an error only if the job could not be enqueued at all (all writer threads gone); in the degenerate zero-worker fallback the write is performed inline instead.
§Examples
use ssg::io_pool::IoPool;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let pool = IoPool::new();
pool.write(dir.path().join("x.txt"), b"x".to_vec()).unwrap();
pool.flush().unwrap();Sourcepub fn flush(&self) -> Result<(), SsgError>
pub fn flush(&self) -> Result<(), SsgError>
Barrier: blocks until every job enqueued so far is fully processed, then reports write failures.
Returns the first captured error (with its path); any
additional failures are logged at error level so nothing is
silently dropped. The error buffer is cleared, and the pool
stays alive — flush() does not shut the pool down and
may be called repeatedly.
§Examples
use ssg::io_pool::IoPool;
let pool = IoPool::new();
// Writing into a directory that does not exist fails at flush.
pool.write("/nonexistent-ssg-dir/x.txt", b"x".to_vec()).unwrap();
assert!(pool.flush().is_err());
// The pool remains usable after a failed flush.
pool.flush().unwrap();Sourcepub fn completed_writes(&self) -> usize
pub fn completed_writes(&self) -> usize
Number of writes that have completed successfully since the pool was created. Primarily useful for tests and diagnostics.
Note: this is a live counter; call IoPool::flush first for
a stable reading.
§Examples
use ssg::io_pool::IoPool;
use tempfile::tempdir;
let dir = tempdir().unwrap();
let pool = IoPool::new();
assert_eq!(pool.completed_writes(), 0);
pool.write(dir.path().join("y.txt"), b"y".to_vec()).unwrap();
pool.flush().unwrap();
assert_eq!(pool.completed_writes(), 1);Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for IoPool
impl !UnwindSafe for IoPool
impl Freeze for IoPool
impl Send for IoPool
impl Sync for IoPool
impl Unpin for IoPool
impl UnsafeUnpin for IoPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more