|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::net::SocketAddr; |
| 3 | +use std::sync::Arc; |
| 4 | +use std::time::Duration; |
| 5 | +use tokio::time::sleep; |
| 6 | + |
| 7 | +use async_broadcast::Receiver; |
| 8 | +use volo::{ |
| 9 | + context::Endpoint, |
| 10 | + discovery::{Change, Discover, Instance}, |
| 11 | + loadbalance::{ |
| 12 | + LoadBalance, |
| 13 | + error::LoadBalanceError, |
| 14 | + least_conn::LeastConnectionBalance, |
| 15 | + p2c::P2c, |
| 16 | + random::WeightedRandomBalance, |
| 17 | + response_time_weighted::ResponseTimeWeightedBalance, |
| 18 | + round_robin::{RoundRobinBalance, WeightedRoundRobinBalance}, |
| 19 | + }, |
| 20 | + net::Address, |
| 21 | +}; |
| 22 | + |
| 23 | +// Mock service discovery implementation |
| 24 | +struct MockDiscover { |
| 25 | + instances: Vec<Arc<Instance>>, |
| 26 | +} |
| 27 | + |
| 28 | +impl MockDiscover { |
| 29 | + fn new(instances: Vec<Arc<Instance>>) -> Self { |
| 30 | + Self { instances } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Discover for MockDiscover { |
| 35 | + type Key = String; |
| 36 | + type Error = LoadBalanceError; |
| 37 | + |
| 38 | + fn key(&self, _: &Endpoint) -> Self::Key { |
| 39 | + String::from("mock-discover") |
| 40 | + } |
| 41 | + |
| 42 | + async fn discover(&self, _: &Endpoint) -> Result<Vec<Arc<Instance>>, Self::Error> { |
| 43 | + Ok(self.instances.clone()) |
| 44 | + } |
| 45 | + |
| 46 | + fn watch(&self, _: Option<&[String]>) -> Option<Receiver<Change<String>>> { |
| 47 | + None |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[tokio::main] |
| 52 | +async fn main() { |
| 53 | + // Create some test instances |
| 54 | + let instances = vec![ |
| 55 | + Arc::new(Instance { |
| 56 | + address: Address::from(SocketAddr::from(([127, 0, 0, 1], 8080))), |
| 57 | + weight: 100, |
| 58 | + tags: HashMap::new(), |
| 59 | + }), |
| 60 | + Arc::new(Instance { |
| 61 | + address: Address::from(SocketAddr::from(([127, 0, 0, 1], 8081))), |
| 62 | + weight: 200, |
| 63 | + tags: HashMap::new(), |
| 64 | + }), |
| 65 | + Arc::new(Instance { |
| 66 | + address: Address::from(SocketAddr::from(([127, 0, 0, 1], 8082))), |
| 67 | + weight: 300, |
| 68 | + tags: HashMap::new(), |
| 69 | + }), |
| 70 | + ]; |
| 71 | + |
| 72 | + let discover = MockDiscover::new(instances); |
| 73 | + let endpoint = Endpoint::new("test-service".into()); |
| 74 | + |
| 75 | + // Example 1: Round Robin Load Balancer |
| 76 | + println!("\n=== Round Robin Load Balancer ==="); |
| 77 | + let round_robin = RoundRobinBalance::new(); |
| 78 | + demonstrate_lb(&round_robin, &discover, &endpoint).await; |
| 79 | + |
| 80 | + // Example 2: Weighted Round Robin Load Balancer |
| 81 | + println!("\n=== Weighted Round Robin Load Balancer ==="); |
| 82 | + let weighted_round_robin = WeightedRoundRobinBalance::new(vec![]); |
| 83 | + demonstrate_lb(&weighted_round_robin, &discover, &endpoint).await; |
| 84 | + |
| 85 | + // Example 3: P2C (Power of Two Choices) Load Balancer |
| 86 | + println!("\n=== P2C Load Balancer ==="); |
| 87 | + let p2c = P2c::default(); |
| 88 | + demonstrate_lb(&p2c, &discover, &endpoint).await; |
| 89 | + |
| 90 | + // Example 4: Weighted Random Load Balancer |
| 91 | + println!("\n=== Weighted Random Load Balancer ==="); |
| 92 | + let random = WeightedRandomBalance::new(); |
| 93 | + demonstrate_lb(&random, &discover, &endpoint).await; |
| 94 | + |
| 95 | + // Example 5: Least Connection Load Balancer |
| 96 | + println!("\n=== Least Connection Load Balancer ==="); |
| 97 | + let least_conn = LeastConnectionBalance::new(); |
| 98 | + demonstrate_lb(&least_conn, &discover, &endpoint).await; |
| 99 | + |
| 100 | + // Example 6: Response Time Weighted Load Balancer |
| 101 | + println!("\n=== Response Time Weighted Load Balancer ==="); |
| 102 | + let response_time = ResponseTimeWeightedBalance::new(100); // window size in items |
| 103 | + demonstrate_lb(&response_time, &discover, &endpoint).await; |
| 104 | +} |
| 105 | + |
| 106 | +async fn demonstrate_lb<L, D>(lb: &L, discover: &D, endpoint: &Endpoint) |
| 107 | +where |
| 108 | + L: LoadBalance<D>, |
| 109 | + D: Discover, |
| 110 | +{ |
| 111 | + match lb.get_picker(endpoint, discover).await { |
| 112 | + Ok(mut picker) => { |
| 113 | + // Demonstrate 10 picks |
| 114 | + for i in 0..10 { |
| 115 | + if let Some(addr) = picker.next() { |
| 116 | + println!("Pick {}: Selected instance: {}", i + 1, addr); |
| 117 | + } |
| 118 | + sleep(Duration::from_millis(100)).await; |
| 119 | + } |
| 120 | + } |
| 121 | + Err(e) => println!("Failed to get picker: {e:?}"), |
| 122 | + } |
| 123 | +} |
0 commit comments