1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
// Copyright 2020 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! `Envoy` `Extension Factory`. //! //! [`ExtensionFactory`] is responsible for handling extension configuration //! and creating new instances of extension. //! //! # Examples //! //! #### Basic [`ExtensionFactory`]: //! //! ``` //! # use envoy_sdk as envoy; //! # use envoy::extension::HttpFilter; //! # //! # /// My very own `HttpFilter`. //! # struct MyHttpFilter; //! # impl HttpFilter for MyHttpFilter {} //! # //! use envoy::extension::{ExtensionFactory, InstanceId, Result}; //! //! /// `ExtensionFactory` for `MyHttpFilter`. //! struct MyHttpFilterFactory; //! //! impl ExtensionFactory for MyHttpFilterFactory { //! type Extension = MyHttpFilter; //! //! fn name() -> &'static str { "my_http_filter" } //! //! fn new_extension(&mut self, _instance_id: InstanceId) -> Result<Self::Extension> { //! Ok(MyHttpFilter) //! } //! } //! ``` //! //! [`ExtensionFactory`]: trait.ExtensionFactory.html use crate::extension::{factory, InstanceId, Result}; use crate::host::{self, ByteString}; pub(crate) use self::context::{ChildContextFactory, ExtensionFactoryContext}; mod context; mod ops; /// Possible responses to the request to (re-)configure the extension. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub enum ConfigStatus { /// Extension has accepted the new configuration. Accepted, /// Extension has rejected the new configuration. Rejected, } impl ConfigStatus { pub(crate) fn as_bool(&self) -> bool { match self { ConfigStatus::Accepted => true, ConfigStatus::Rejected => false, } } } /// Possible responses to the request to drain the extension. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub enum DrainStatus { /// Extension is being drained and cannot be removed just yet. Ongoing, /// Extension has been drained and can be removed now. Complete, } impl DrainStatus { pub(crate) fn as_bool(&self) -> bool { match self { DrainStatus::Ongoing => false, DrainStatus::Complete => true, } } } /// An interface of the `Envoy` `Extension Factory`. /// /// [`ExtensionFactory`] is responsible for /// * handling extension configuration, /// * owning state shared by all extension instances, /// * creating new instances of extension, injecting their dependencies and shared state. /// /// At the moment, [`ExtensionFactory`] can be used for [`HttpFilter`] and [`NetworkFilter`] /// extensions. /// /// [`AccessLogger`] extension has a different lifecycle and therefore manages its /// configuration differently. /// /// # Examples /// /// #### Basic [`ExtensionFactory`]: /// /// ``` /// # use envoy_sdk as envoy; /// # use envoy::extension::HttpFilter; /// # /// # /// My very own `HttpFilter`. /// # struct MyHttpFilter; /// # impl HttpFilter for MyHttpFilter {} /// # /// use envoy::extension::{ExtensionFactory, InstanceId, Result}; /// /// /// `ExtensionFactory` for `MyHttpFilter`. /// struct MyHttpFilterFactory; /// /// impl ExtensionFactory for MyHttpFilterFactory { /// type Extension = MyHttpFilter; /// /// fn name() -> &'static str { "my_http_filter" } /// /// fn new_extension(&mut self, _instance_id: InstanceId) -> Result<Self::Extension> { /// Ok(MyHttpFilter) /// } /// } /// ``` /// /// #### Handling extension configuration: /// /// ``` /// # use envoy_sdk as envoy; /// # use envoy::extension::HttpFilter; /// # /// # /// My very own `HttpFilter`. /// # struct MyHttpFilter { /// # config: std::rc::Rc<String>, /// # } /// # impl HttpFilter for MyHttpFilter {} /// # impl MyHttpFilter { /// # fn new(config: std::rc::Rc<String>) -> Self { /// # MyHttpFilter { config } /// # } /// # } /// # /// use std::rc::Rc; /// use envoy::extension::{factory, ConfigStatus, ExtensionFactory, InstanceId, Result}; /// use envoy::host::ByteString; /// /// /// `ExtensionFactory` for `MyHttpFilter`. /// struct MyHttpFilterFactory { /// // This example shows how multiple filter instances could share /// // the same configuration. /// config: Rc<String>, /// } /// /// impl ExtensionFactory for MyHttpFilterFactory { /// type Extension = MyHttpFilter; /// /// fn name() -> &'static str { "my_http_filter" } /// /// /// Called when extension is being (re-)configured on `Envoy Listener` update. /// fn on_configure(&mut self, config: ByteString, ops: &dyn factory::ConfigureOps) -> Result<ConfigStatus> { /// let config = if config.is_empty() { String::default() } else { /// String::from_utf8(config.into_bytes())? /// }; /// self.config = Rc::new(config); /// Ok(ConfigStatus::Accepted) /// } /// /// fn new_extension(&mut self, _instance_id: InstanceId) -> Result<Self::Extension> { /// Ok(MyHttpFilter::new(Rc::clone(&self.config))) /// } /// } /// ``` /// /// #### Sharing `Stats` between filter instances: /// /// ``` /// # use envoy_sdk as envoy; /// use std::rc::Rc; /// use envoy::extension::{factory, ConfigStatus, ExtensionFactory, InstanceId, Result}; /// use envoy::host::stats::{Counter, Stats}; /// /// /// Stats shared between multiple filter instances. /// pub struct MyStats { /// requests_total: Box<dyn Counter>, /// } /// /// # use envoy::extension::HttpFilter; /// # /// # /// My very own `HttpFilter`. /// # struct MyHttpFilter { /// # stats: std::rc::Rc<MyStats>, /// # } /// # impl HttpFilter for MyHttpFilter {} /// # impl MyHttpFilter { /// # fn new(stats: std::rc::Rc<MyStats>) -> Self { /// # MyHttpFilter { stats } /// # } /// # } /// # /// /// `ExtensionFactory` for `MyHttpFilter`. /// struct MyHttpFilterFactory { /// // This example shows how multiple filter instances could share stats. /// stats: Rc<MyStats>, /// } /// /// impl ExtensionFactory for MyHttpFilterFactory { /// type Extension = MyHttpFilter; /// /// fn name() -> &'static str { "my_http_filter" } /// /// fn new_extension(&mut self, _instance_id: InstanceId) -> Result<Self::Extension> { /// Ok(MyHttpFilter::new(Rc::clone(&self.stats))) /// } /// } /// /// impl MyHttpFilterFactory { /// /// Creates a new factory. /// pub fn new(stats: &dyn Stats) -> Result<Self> { /// let my_stats = MyStats{ /// requests_total: stats.counter("examples.http_filter.requests_total")?, /// }; /// Ok(MyHttpFilterFactory { /// stats: Rc::new(my_stats), /// }) /// } /// /// /// Creates a new factory bound to the actual `Envoy` `ABI`. /// pub fn default() -> Result<Self> { /// Self::new(Stats::default()) /// } /// } /// ``` /// /// [`ExtensionFactory`]: trait.ExtensionFactory.html /// [`HttpFilter`]: ../filter/http/trait.HttpFilter.html /// [`NetworkFilter`]: ../filter/network/trait.NetworkFilter.html /// [`AccessLogger`]: ../access_logger/trait.AccessLogger.html pub trait ExtensionFactory { type Extension; /// Returns a name the extension should be referred to in `Envoy` configuration. fn name() -> &'static str where Self: Sized; /// Called when extension is being (re-)configured on `Envoy Listener` update. /// /// # Arguments /// /// * `_config` - configuration. /// * `_ops` - a [`trait object`][`ConfigureOps`] with operations available in this context. /// /// # Return value /// /// [`ConfigStatus`] telling `Envoy` whether configuration has been successfully applied. /// /// [`ConfigStatus`]: enum.ConfigStatus.html /// [`ConfigureOps`]: trait.ConfigureOps.html fn on_configure( &mut self, _config: ByteString, _ops: &dyn factory::ConfigureOps, ) -> Result<ConfigStatus> { Ok(ConfigStatus::Accepted) } /// Called to create a new instance of the extension, e.g. [`HttpFilter`] or [`NetworkFilter`]. /// /// # Arguments /// /// * `instance_id` - opaque identifier of the extension instance. /// /// # Return value /// /// a new instance of the extension. /// /// [`HttpFilter`]: ../filter/http/trait.HttpFilter.html /// [`NetworkFilter`]: ../filter/network/trait.NetworkFilter.html fn new_extension(&mut self, _instance_id: InstanceId) -> Result<Self::Extension>; /// Called when `ExtensionFactory` is about to be destroyed. /// /// # Return value /// /// [`DrainStatus`] telling `Envoy` whether `ExtensionFactory` has already been drained /// and can be now removed safely. /// /// [`DrainStatus`]: enum.DrainStatus.html fn on_drain(&mut self) -> Result<DrainStatus> { Ok(DrainStatus::Complete) } } /// An interface for accessing extension config. pub(crate) trait ContextOps { /// Returns extension config. fn configuration(&self, start: usize, max_size: usize) -> host::Result<ByteString>; } impl dyn ContextOps { /// Returns the default implementation that interacts with `Envoy` /// through its [`ABI`]. /// /// [`ABI`]: https://github.com/proxy-wasm/spec pub fn default() -> &'static dyn ContextOps { &ops::Host } } /// An interface for operations available in the context of [`on_configure`] /// invocation. /// /// [`on_configure`]: trait.ExtensionFactory.html#method.on_configure pub trait ConfigureOps {} /// An interface for acknowledging `Envoy` that [`ExtensionFactory`] has been drained. /// /// [`ExtensionFactory`]: trait.ExtensionFactory.html pub trait DrainOps { /// Acknowledges `Envoy` that extension has been drained and can be safely removed now. fn done(&self) -> host::Result<()>; } #[doc(hidden)] pub trait Ops: ConfigureOps + DrainOps { fn as_configure_ops(&self) -> &dyn ConfigureOps; fn as_done_ops(&self) -> &dyn DrainOps; } impl<T> Ops for T where T: ConfigureOps + DrainOps, { fn as_configure_ops(&self) -> &dyn ConfigureOps { self } fn as_done_ops(&self) -> &dyn DrainOps { self } } impl dyn Ops { /// Returns the default implementation that interacts with `Envoy` /// through its [`ABI`]. /// /// [`ABI`]: https://github.com/proxy-wasm/spec pub fn default() -> &'static dyn Ops { &ops::Host } }