-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
712 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
proto/quilkin/extensions/filters/debug/v1alpha1/debug.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
|
||
package quilkin.extensions.filters.debug.v1alpha1; | ||
|
||
import "google/protobuf/wrappers.proto"; | ||
|
||
option go_package = "example"; | ||
|
||
message Debug { | ||
google.protobuf.StringValue id = 1; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright 2020 Google LLC All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// TODO: Allow unused variables since this module is WIP. | ||
#![allow(unused)] | ||
|
||
use crate::extensions::{FilterChain, FilterRegistry}; | ||
|
||
use std::sync::Arc; | ||
|
||
use parking_lot::RwLock; | ||
use slog::{debug, o, warn, Logger}; | ||
use tokio::sync::mpsc; | ||
use tokio::sync::watch; | ||
|
||
/// The max size of queue that provides updates from the XDS layer to the [`ClusterManager`]. | ||
const FILTER_CHAIN_UPDATE_QUEUE_SIZE: usize = 1000; | ||
|
||
pub type SharedFilterManager = Arc<RwLock<FilterManager>>; | ||
|
||
/// FilterManager creates and updates the filter chain. | ||
pub struct FilterManager { | ||
/// A lookup table for filter factories. | ||
filter_registry: FilterRegistry, | ||
/// The current filter chain. | ||
filter_chain: Option<FilterChain>, | ||
log: Logger, | ||
} | ||
|
||
/// ListenerManagerArgs contains arguments when invoking the LDS resource manager. | ||
pub(crate) struct ListenerManagerArgs { | ||
pub filter_registry: Arc<FilterRegistry>, | ||
pub filter_chain_updates_tx: mpsc::Sender<FilterChain>, | ||
} | ||
|
||
impl ListenerManagerArgs { | ||
pub fn new( | ||
filter_registry: Arc<FilterRegistry>, | ||
filter_chain_updates_tx: mpsc::Sender<FilterChain>, | ||
) -> ListenerManagerArgs { | ||
ListenerManagerArgs { | ||
filter_registry, | ||
filter_chain_updates_tx, | ||
} | ||
} | ||
} | ||
|
||
impl FilterManager { | ||
fn update(&mut self, filter_chain: Option<FilterChain>) { | ||
self.filter_chain = filter_chain; | ||
} | ||
|
||
/// Returns the current filter chain. | ||
pub fn get_filter_chain(&self) -> &Option<FilterChain> { | ||
&self.filter_chain | ||
} | ||
|
||
/// Returns a new instance backed only by the provided filter chain. | ||
pub fn fixed( | ||
base_logger: Logger, | ||
filter_registry: FilterRegistry, | ||
filter_chain: FilterChain, | ||
) -> SharedFilterManager { | ||
Arc::new(RwLock::new(FilterManager { | ||
filter_registry, | ||
filter_chain: Some(filter_chain), | ||
log: Self::create_logger(base_logger), | ||
})) | ||
} | ||
|
||
/// Returns a new instance backed by a stream of filter chain updates. | ||
/// Updates from the provided stream will be reflected in the current filter chain. | ||
pub async fn dynamic( | ||
base_logger: Logger, | ||
filter_registry: FilterRegistry, | ||
filter_chain_updates_rx: mpsc::Receiver<Option<FilterChain>>, | ||
shutdown_rx: watch::Receiver<()>, | ||
) -> SharedFilterManager { | ||
let log = Self::create_logger(base_logger); | ||
let filter_manager = Arc::new(RwLock::new(FilterManager { | ||
filter_registry, | ||
filter_chain: None, | ||
log: log.clone(), | ||
})); | ||
|
||
Self::spawn_updater( | ||
log, | ||
filter_manager.clone(), | ||
filter_chain_updates_rx, | ||
shutdown_rx, | ||
); | ||
|
||
filter_manager | ||
} | ||
|
||
/// Spawns a task in the background that listens for filter chain updates and | ||
/// updates the filter manager's current filter in turn. | ||
fn spawn_updater( | ||
log: Logger, | ||
filter_manager: SharedFilterManager, | ||
mut filter_chain_updates_rx: mpsc::Receiver<Option<FilterChain>>, | ||
mut shutdown_rx: watch::Receiver<()>, | ||
) { | ||
tokio::spawn(async move { | ||
loop { | ||
tokio::select! { | ||
update = filter_chain_updates_rx.recv() => { | ||
match update { | ||
Some(filter_chain) => { | ||
debug!(log, "Received a filter chain update."); | ||
filter_manager.write().update(filter_chain); | ||
} | ||
None => { | ||
warn!(log, "Exiting filter chain update receive loop because the sender dropped the channel."); | ||
return; | ||
} | ||
} | ||
} | ||
_ = shutdown_rx.recv() => { | ||
debug!(log, "Exiting filter chain update receive loop because a shutdown signal was received."); | ||
return; | ||
}, | ||
} | ||
} | ||
}); | ||
} | ||
|
||
fn create_logger(base_logger: Logger) -> Logger { | ||
base_logger.new(o!("source" => "FilterManager")) | ||
} | ||
} |
Oops, something went wrong.