dfinity / agent-rs

A collection of libraries and tools for building software around the Internet Computer, in Rust.
https://sdk.dfinity.org/
Apache License 2.0
124 stars 77 forks source link

feat: add `fn n_routes()` to `RouteProvider` trait #584

Closed nikolay-komarevskiy closed 3 months ago

nikolay-komarevskiy commented 3 months ago

Description

RouteProvider trait is extended with fn n_routes(), which generates up to n different routing urls:

pub trait RouteProvider: std::fmt::Debug + Send + Sync {
    /// Generates the next routing URL based on the internal routing logic.
    ///
    /// This method returns a single `Url` that can be used for routing.
    /// The logic behind determining the next URL can vary depending on the implementation
    fn route(&self) -> Result<Url, AgentError>;

    /// Generates up to `n` different routing URLs in order of priority.
    ///
    /// This method returns a vector of `Url` instances, each representing a routing
    /// endpoint. The URLs are ordered by priority, with the most preferred route
    /// appearing first. The returned vector can contain fewer than `n` URLs if
    /// fewer are available.
    fn n_ordered_routes(&self, n: usize) -> Result<Vec<Url>, AgentError>;
}

This newly introduced method should facilitate retry logic, in case routing url is unhealthy. Calling the fn route() for retries was (and still is) possible. However, implementations do not provide guarantees that urls returned in sequential invocations are unique. Function fn n_routes() comes at rescue and returns up to n unique routing urls, which are also ordered by priority.

How Has This Been Tested?

Additional tests have been added.

Checklist: