|
| 1 | +#![deny(rust_2018_idioms, clippy::disallowed_methods, clippy::disallowed_types)] |
| 2 | +#![forbid(unsafe_code)] |
| 3 | + |
| 4 | +use std::{borrow::Cow, hash::Hash, net::SocketAddr, sync::Arc, time}; |
| 5 | + |
| 6 | +pub mod grpc; |
| 7 | +pub mod http; |
| 8 | +pub mod opaq; |
| 9 | + |
| 10 | +pub use linkerd_http_route as route; |
| 11 | +pub use linkerd_proxy_api_resolve::Metadata as EndpointMetadata; |
| 12 | + |
| 13 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 14 | +pub struct ClientPolicy { |
| 15 | + pub protocol: Protocol, |
| 16 | + pub backends: Arc<[Backend]>, |
| 17 | +} |
| 18 | + |
| 19 | +// TODO additional server configs (e.g. concurrency limits, window sizes, etc) |
| 20 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 21 | +pub enum Protocol { |
| 22 | + Detect { |
| 23 | + timeout: time::Duration, |
| 24 | + http1: http::Http1, |
| 25 | + http2: http::Http2, |
| 26 | + opaque: opaq::Opaque, |
| 27 | + }, |
| 28 | + |
| 29 | + Http1(http::Http1), |
| 30 | + Http2(http::Http2), |
| 31 | + Grpc(grpc::Grpc), |
| 32 | + |
| 33 | + Opaque(opaq::Opaque), |
| 34 | + |
| 35 | + // TODO(ver) TLS-aware type |
| 36 | + Tls(opaq::Opaque), |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug, Eq)] |
| 40 | +pub enum Meta { |
| 41 | + Default { |
| 42 | + name: Cow<'static, str>, |
| 43 | + }, |
| 44 | + Resource { |
| 45 | + group: String, |
| 46 | + kind: String, |
| 47 | + name: String, |
| 48 | + namespace: String, |
| 49 | + section: Option<String>, |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 54 | +pub struct RoutePolicy<T> { |
| 55 | + pub meta: Arc<Meta>, |
| 56 | + pub filters: Arc<[T]>, |
| 57 | + pub distribution: RouteDistribution<T>, |
| 58 | +} |
| 59 | + |
| 60 | +// TODO(ver) Weighted random WITHOUT availability awareness, as required by |
| 61 | +// HTTPRoute. |
| 62 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 63 | +pub enum RouteDistribution<T> { |
| 64 | + Empty, |
| 65 | + |
| 66 | + FirstAvailable(Arc<[RouteBackend<T>]>), |
| 67 | + |
| 68 | + RandomAvailable(Arc<[(RouteBackend<T>, u32)]>), |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 72 | +pub struct RouteBackend<T> { |
| 73 | + pub filters: Arc<[T]>, |
| 74 | + pub backend: Backend, |
| 75 | +} |
| 76 | + |
| 77 | +// TODO(ver) how does configuration like failure accrual fit in here? What about |
| 78 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 79 | +pub struct Backend { |
| 80 | + pub meta: Arc<Meta>, |
| 81 | + pub queue: Queue, |
| 82 | + pub dispatcher: BackendDispatcher, |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 86 | +pub struct Queue { |
| 87 | + pub capacity: usize, |
| 88 | + pub failfast_timeout: time::Duration, |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 92 | +pub enum BackendDispatcher { |
| 93 | + Forward(SocketAddr, EndpointMetadata), |
| 94 | + BalanceP2c(Load, EndpointDiscovery), |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 98 | +pub enum EndpointDiscovery { |
| 99 | + DestinationGet { path: String }, |
| 100 | +} |
| 101 | + |
| 102 | +/// Configures the load balancing strategy for a backend. |
| 103 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 104 | +pub enum Load { |
| 105 | + PeakEwma(PeakEwma), |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 109 | +pub struct PeakEwma { |
| 110 | + pub decay: time::Duration, |
| 111 | + pub default_rtt: time::Duration, |
| 112 | +} |
| 113 | + |
| 114 | +// === impl Meta === |
| 115 | + |
| 116 | +impl Meta { |
| 117 | + pub fn new_default(name: impl Into<Cow<'static, str>>) -> Arc<Self> { |
| 118 | + Arc::new(Self::Default { name: name.into() }) |
| 119 | + } |
| 120 | + |
| 121 | + pub fn group(&self) -> &str { |
| 122 | + match self { |
| 123 | + Self::Default { .. } => "", |
| 124 | + Self::Resource { group, .. } => group, |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + pub fn kind(&self) -> &str { |
| 129 | + match self { |
| 130 | + Self::Default { .. } => "default", |
| 131 | + Self::Resource { kind, .. } => kind, |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + pub fn name(&self) -> &str { |
| 136 | + match self { |
| 137 | + Self::Default { name } => name, |
| 138 | + Self::Resource { name, .. } => name, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + pub fn namespace(&self) -> &str { |
| 143 | + match self { |
| 144 | + Self::Default { .. } => "", |
| 145 | + Self::Resource { namespace, .. } => namespace, |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + pub fn section(&self) -> &str { |
| 150 | + match self { |
| 151 | + Self::Default { .. } => "", |
| 152 | + Self::Resource { section, .. } => section.as_deref().unwrap_or(""), |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl std::cmp::PartialEq for Meta { |
| 158 | + fn eq(&self, other: &Self) -> bool { |
| 159 | + // Resources that look like Defaults are considered equal. |
| 160 | + self.group() == other.group() && self.kind() == other.kind() && self.name() == other.name() |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl std::hash::Hash for Meta { |
| 165 | + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 166 | + // Resources that look like Defaults are considered the same. |
| 167 | + self.group().hash(state); |
| 168 | + self.kind().hash(state); |
| 169 | + self.name().hash(state); |
| 170 | + } |
| 171 | +} |
0 commit comments