Skip to main content

Module discover

Module discover 

Source
Available on crate feature discover only.
Expand description

Service discovery

This module provides the Change enum, which indicates the arrival or departure of a service from a collection of similar services. Most implementations should use the Discover trait in their bounds to indicate that they can handle services coming and going. Discover itself is primarily a convenience wrapper around TryStream<Ok = Change>.

Every discovered service is assigned an identifier that is distinct among the currently active services. If that service later goes away, a Change::Remove is yielded with that service’s identifier. From that point forward, the identifier may be re-used.

§Examples

use std::future::poll_fn;
use futures_util::pin_mut;
use tower::discover::{Change, Discover};
async fn services_monitor<D: Discover>(services: D) {
    pin_mut!(services);
    while let Some(Ok(change)) = poll_fn(|cx| services.as_mut().poll_discover(cx)).await {
        match change {
            Change::Insert(key, svc) => {
                // a new service with identifier `key` was discovered
            }
            Change::Remove(key) => {
                // the service with identifier `key` has gone away
            }
        }
    }
}

Structs§

ServiceList
Static service discovery based on a predetermined list of services.

Enums§

Change
A change in the service set.

Traits§

Discover
A dynamically changing set of related services.