pub struct BindMany<S> { /* private fields */ }Expand description
This stream replaces tokio_core::net::Incoming and listens many sockets
It receives a stream of lists of addresses as an input. When a new value received on a stream it adapts:
- Removes sockets not in set we’re already received (already established connections aren’t interfered in any way)
- Adds sockets to set which wasn’t listened before
Instead of failing on bind error it logs the error and retries in a
second (you can change the delay using BindMany::retry_interval)
It’s good idea to pass a stream with a Void error, because on receiving
error BindMany will log a message (that doesn’t contain an error) and
will shutdown. It’s better to log specific error and send end-of-stream
instead, but that is user’s responsibility.
Note: we track identity of the sockets by SocketAddr used to bind it,
this means 0.0.0.0 and 127.0.0.1 for example can be bound/unbound
independently despite the fact that 0.0.0.0 can accept connections for
127.0.0.1.
§Example
Simple example:
lp.run(
BindMany::new(address_stream)
.sleep_on_error(TIME_TO_WAIT_ON_ERROR, &h2)
.map(move |(mut socket, _addr)| {
// Your future is here:
Proto::new(socket)
// Errors should not pass silently
// common idea is to log them
.map_err(|e| error!("Protocol error: {}", e))
})
.listen(MAX_SIMULTANEOUS_CONNECTIONS)
).unwrap(); // stream doesn't end in this caseExample using name resolution via abstract-ns + ns-env-config:
extern crate ns_env_config;
let mut lp = Core::new().unwrap();
let ns = ns_env_config::init(&lp.handle()).unwrap();
lp.run(
BindMany::new(ns.resolve_auto("localhost", 8080)
.map(|addr| addr.addresses_at(0)))
.sleep_on_error(TIME_TO_WAIT_ON_ERROR, &h2)
.map(move |(mut socket, _addr)| {
// Your future is here:
Proto::new(socket)
// Errors should not pass silently
// common idea is to log them
.map_err(|e| eprintln!("Protocol error: {}", e))
})
.listen(MAX_SIMULTANEOUS_CONNECTIONS)
).unwrap(); // stream doesn't end in this caseImplementations§
Source§impl<S> BindMany<S>
impl<S> BindMany<S>
Sourcepub fn retry_interval(&mut self, interval: Duration) -> &mut Self
pub fn retry_interval(&mut self, interval: Duration) -> &mut Self
Sets the retry interval
Each time binding socket fails (including he first one on start) istead of immediately failing we log the error and sleep this interval to retry (by default 1 second).
This behavior is important because if your configuration changes number of listening sockets, and one of them is either invalid or just DNS is temporarily down, you still need to serve other addresses.
This also helps if you have failover IP which can only be listened at when IP attached to the host, but server must be ready to listen it anyway (this one might be better achieved by non-local bind though).
Trait Implementations§
Source§impl<S> Stream for BindMany<S>
impl<S> Stream for BindMany<S>
Source§fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Error>
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Error>
None if
the stream is finished. Read moreSource§fn wait(self) -> Wait<Self>where
Self: Sized,
fn wait(self) -> Wait<Self>where
Self: Sized,
Source§fn into_future(self) -> StreamFuture<Self>where
Self: Sized,
fn into_future(self) -> StreamFuture<Self>where
Self: Sized,
Future. Read moreSource§fn filter<F>(self, f: F) -> Filter<Self, F>
fn filter<F>(self, f: F) -> Filter<Self, F>
Source§fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F>
fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F>
Source§fn then<F, U>(self, f: F) -> Then<Self, F, U>
fn then<F, U>(self, f: F) -> Then<Self, F, U>
f. Read moreSource§fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U>
fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U>
f. Read moreSource§fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U>
fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U>
f. Read moreSource§fn collect(self) -> Collect<Self>where
Self: Sized,
fn collect(self) -> Collect<Self>where
Self: Sized,
Source§fn concat2(self) -> Concat2<Self>
fn concat2(self) -> Concat2<Self>
Source§fn concat(self) -> Concat<Self>
fn concat(self) -> Concat<Self>
Stream::concat2 insteadSource§fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T>where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
Self: Sized,
fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T>where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R>
fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R>
true. Read moreSource§fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
true. Read moreSource§fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
Source§fn from_err<E>(self) -> FromErr<Self, E>
fn from_err<E>(self) -> FromErr<Self, E>
From for
this stream’s Error, returning a new stream. Read moreSource§fn take(self, amt: u64) -> Take<Self>where
Self: Sized,
fn take(self, amt: u64) -> Take<Self>where
Self: Sized,
amt items of the underlying stream. Read moreSource§fn skip(self, amt: u64) -> Skip<Self>where
Self: Sized,
fn skip(self, amt: u64) -> Skip<Self>where
Self: Sized,
amt items of the underlying stream. Read moreSource§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
finished. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn buffered(self, amt: usize) -> Buffered<Self>
fn buffered(self, amt: usize) -> Buffered<Self>
Source§fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self>
fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self>
Source§fn merge<S>(self, other: S) -> Merge<Self, S>
fn merge<S>(self, other: S) -> Merge<Self, S>
select nowSource§fn zip<S>(self, other: S) -> Zip<Self, S>
fn zip<S>(self, other: S) -> Zip<Self, S>
Source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek method. Read moreSource§fn chunks(self, capacity: usize) -> Chunks<Self>where
Self: Sized,
fn chunks(self, capacity: usize) -> Chunks<Self>where
Self: Sized,
Source§fn select<S>(self, other: S) -> Select<Self, S>
fn select<S>(self, other: S) -> Select<Self, S>
Source§fn forward<S>(self, sink: S) -> Forward<Self, S>
fn forward<S>(self, sink: S) -> Forward<Self, S>
Source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
Auto Trait Implementations§
impl<S> Freeze for BindMany<S>where
S: Freeze,
impl<S> !RefUnwindSafe for BindMany<S>
impl<S> Send for BindMany<S>where
S: Send,
impl<S> Sync for BindMany<S>where
S: Sync,
impl<S> Unpin for BindMany<S>where
S: Unpin,
impl<S> !UnwindSafe for BindMany<S>
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
Source§impl<T> ListenExt for Twhere
T: Stream,
impl<T> ListenExt for Twhere
T: Stream,
Source§fn sleep_on_error(self, delay: Duration) -> SleepOnError<Self>where
Self: Sized,
fn sleep_on_error(self, delay: Duration) -> SleepOnError<Self>where
Self: Sized,
TcpListener::incoming
into a stream that supresses errors and sleeps on resource shortage,
effectively allowing listening stream to resume on error.