下面显示的是英文原文。
from_pool
RedisBroker::from_pool wraps a fred Pool the service built itself, for client settings the broker's own builders do not reach.
fred's Builder sets what the broker leaves at fred's defaults: a command timeout, a reconnection policy, buffer sizes, any topology a fred Config describes.
The service builds the pool in its #[ruststream::app] builder and hands it over unconnected.
The broker dials it when the service starts, and an unreachable server fails the start, as it does for a broker built from an address.
A pool the service has already connected is adopted as it is.
The pool's size is the pool of the broker.
Each subscription's own connection is built from the pool's config and reconnects by the pool's policy.
The generated document reports no host for such a broker, because the address sits inside the pool's own config.
A TestApp test runs the same app in process: it reads the pool's size and config and dials nothing.
Example
The dependency line:
fred = "10"
The pool holds eight connections, bounds every command at two seconds and reconnects every half second.
- Macros
- Manual
/// Eight connections, a two-second bound on every command, and a reconnect every half second.
fn pool(url: &str) -> Result<Pool, FredError> {
Builder::from_config(Config::from_url(url)?)
.with_performance_config(|performance| {
performance.default_command_timeout = Duration::from_secs(2);
})
.set_policy(ReconnectPolicy::new_constant(0, 500))
.build_pool(8)
}
#[ruststream::app]
fn app() -> impl App<State = ()> {
// A constant URL and a non-zero size: the pool always builds.
let pool = pool("redis://localhost:6379").expect("a pool over a valid URL");
// The pool is not connected here: the broker dials it when the service starts.
service(RedisBroker::from_pool(pool))
}
/// Eight connections, a two-second bound on every command, and a reconnect every half second.
fn pool(url: &str) -> Result<Pool, FredError> {
Builder::from_config(Config::from_url(url)?)
.with_performance_config(|performance| {
performance.default_command_timeout = Duration::from_secs(2);
})
.set_policy(ReconnectPolicy::new_constant(0, 500))
.build_pool(8)
}
fn app() -> impl App<State = ()> {
// A constant URL and a non-zero size: the pool always builds.
let pool = pool("redis://localhost:6379").expect("a pool over a valid URL");
// The pool is not connected here: the broker dials it when the service starts.
service(RedisBroker::from_pool(pool))
}