|
1 | 1 | use crate::buffer::EnvoyBuffer; |
2 | 2 | use crate::{ |
3 | | - abi, drop_wrapped_c_void_ptr, str_to_module_buffer, strs_to_module_buffers, wrap_into_c_void_ptr, |
4 | | - CompletionCallback, EnvoyCounterId, EnvoyCounterVecId, EnvoyGaugeId, EnvoyGaugeVecId, |
5 | | - EnvoyHistogramId, EnvoyHistogramVecId, NEW_CLUSTER_CONFIG_FUNCTION, |
| 3 | + abi, |
| 4 | + bytes_to_module_buffer, |
| 5 | + drop_wrapped_c_void_ptr, |
| 6 | + str_to_module_buffer, |
| 7 | + strs_to_module_buffers, |
| 8 | + wrap_into_c_void_ptr, |
| 9 | + CompletionCallback, |
| 10 | + EnvoyCounterId, |
| 11 | + EnvoyCounterVecId, |
| 12 | + EnvoyGaugeId, |
| 13 | + EnvoyGaugeVecId, |
| 14 | + EnvoyHistogramId, |
| 15 | + EnvoyHistogramVecId, |
| 16 | + NEW_CLUSTER_CONFIG_FUNCTION, |
6 | 17 | }; |
7 | 18 | use mockall::*; |
8 | 19 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
@@ -226,6 +237,29 @@ pub trait ClusterLbContext { |
226 | 237 | /// |
227 | 238 | /// Returns `None` if the downstream connection or SNI is not available. |
228 | 239 | fn get_downstream_connection_sni(&self) -> Option<String>; |
| 240 | + |
| 241 | + /// Returns the bytes value of a `Router::StringAccessor` filter state stored on the request. |
| 242 | + /// |
| 243 | + /// This lets a cluster consume filter state that an upstream HTTP filter set via |
| 244 | + /// `EnvoyHttpFilter::set_filter_state_bytes` (or anything else that stores a `StringAccessor`) |
| 245 | + /// to make a host-selection decision. |
| 246 | + /// |
| 247 | + /// Returns `None` if the request has no stream info, the key is not present, or the stored |
| 248 | + /// value is not a `StringAccessor`. The returned buffer borrows from Envoy and is valid for |
| 249 | + /// the duration of the current host-selection callback. |
| 250 | + fn get_filter_state_bytes<'a>(&'a self, key: &[u8]) -> Option<EnvoyBuffer<'a>>; |
| 251 | + |
| 252 | + /// Returns the serialized bytes of a typed filter state object stored on the request. |
| 253 | + /// |
| 254 | + /// This is the read side of `EnvoyHttpFilter::set_filter_state_typed` and works for any |
| 255 | + /// filter state object whose registered `ObjectFactory` produces an object that supports |
| 256 | + /// `serializeAsString`. |
| 257 | + /// |
| 258 | + /// Returns `None` if the request has no stream info, the key is not present, or the object |
| 259 | + /// does not support serialization. The returned buffer borrows from Envoy and is valid until |
| 260 | + /// the next call to `get_filter_state_typed` on the same worker thread, or until the end of |
| 261 | + /// the current host-selection callback, whichever comes first. |
| 262 | + fn get_filter_state_typed<'a>(&'a self, key: &[u8]) -> Option<EnvoyBuffer<'a>>; |
229 | 263 | } |
230 | 264 |
|
231 | 265 | /// Envoy-side cluster operations available to the module. |
@@ -1830,6 +1864,44 @@ impl ClusterLbContext for ClusterLbContextImpl { |
1830 | 1864 | }; |
1831 | 1865 | Some(sni.into_owned()) |
1832 | 1866 | } |
| 1867 | + |
| 1868 | + fn get_filter_state_bytes(&self, key: &[u8]) -> Option<EnvoyBuffer<'_>> { |
| 1869 | + let mut result = abi::envoy_dynamic_module_type_envoy_buffer { |
| 1870 | + ptr: std::ptr::null_mut(), |
| 1871 | + length: 0, |
| 1872 | + }; |
| 1873 | + let success = unsafe { |
| 1874 | + abi::envoy_dynamic_module_callback_cluster_lb_context_get_filter_state_bytes( |
| 1875 | + self.raw_context, |
| 1876 | + bytes_to_module_buffer(key), |
| 1877 | + &mut result, |
| 1878 | + ) |
| 1879 | + }; |
| 1880 | + if success { |
| 1881 | + Some(unsafe { EnvoyBuffer::new_from_raw(result.ptr as *const _, result.length) }) |
| 1882 | + } else { |
| 1883 | + None |
| 1884 | + } |
| 1885 | + } |
| 1886 | + |
| 1887 | + fn get_filter_state_typed(&self, key: &[u8]) -> Option<EnvoyBuffer<'_>> { |
| 1888 | + let mut result = abi::envoy_dynamic_module_type_envoy_buffer { |
| 1889 | + ptr: std::ptr::null_mut(), |
| 1890 | + length: 0, |
| 1891 | + }; |
| 1892 | + let success = unsafe { |
| 1893 | + abi::envoy_dynamic_module_callback_cluster_lb_context_get_filter_state_typed( |
| 1894 | + self.raw_context, |
| 1895 | + bytes_to_module_buffer(key), |
| 1896 | + &mut result, |
| 1897 | + ) |
| 1898 | + }; |
| 1899 | + if success { |
| 1900 | + Some(unsafe { EnvoyBuffer::new_from_raw(result.ptr as *const _, result.length) }) |
| 1901 | + } else { |
| 1902 | + None |
| 1903 | + } |
| 1904 | + } |
1833 | 1905 | } |
1834 | 1906 |
|
1835 | 1907 | // Cluster Event Hook Implementations |
|
0 commit comments