This crate is being made for learning purposes. Any feedback is welcome.
instances-rs is a crate for making the application aware of how
many of its instances its online.
To use instances-rs, add the following to your Cargo.toml: [NOT PUBLISHED YET]
[dependencies]
instances-rs = "0.1"Then build the Instances object that will start updating the current instance info
and fetching the other ones data. (See usability_test.rs
for example)
use instances_rs::config::Builder;
use std::time::Duration;
fn main() {
let instances_rs = Builder::default()
.with_update_interval(Duration::from_secs(10))
.with_backend(...) // One backend of your choose
.with_info_extractor(...) // Some function that will extract the current instance data that you want to publish
.build();
// Optionally you can wait for
instances_rs
.wait_for_first_update(Duration::from_secs(10))
.unwrap();
// To get the info about the current instance
instances_rs.get_instance_info();
// To get the instance count
instances_rs.instances_count();
// List all active instances
instances_rs.list_active_instances();
}You can choose wherever data you like to publish with your instance data. The only
restriction is that it must be Serializable and Deserializable.
Example:
.with_info_extractor(|| env::consts::OS.to_string())You can choose one of the available backends to store the instances' data or implement your own.
Right now there is no backend implemented, but I have plans to implement the following alternatives: MySQL, DynamoDB and Redis.
You can classify your instances choosing one LeaderStrategy. By default
LeaderStrategy::None is used.
You can choose one CommunicationErrorStrategy to handle error on updates.
CommunicationErrorStrategy::Errorwill make the update return anErrto the daemon and all the instances' info will be unavailable.CommunicationErrorStrategy::UseLastInfowill emit a warning during the update and the outdated data will still be available.
This project is licensed under the MIT license.