-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor frontend context #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8d5c59
♻️ refactor: implement service-oriented architecture for PostgreSQL f…
tobyhede aecdbeb
♻️ refactor: extract column processing logic into dedicated ColumnMap…
tobyhede b2b6ea0
♻️ refactor: extract Statement and Portal into dedicated modules
tobyhede 91d1740
♻️ refactor: extract encrypt config into dedicated module
tobyhede 2f3300b
♻️ refactor: add test config constructors and update Context instanti…
tobyhede a35b9f0
♻️ refactor: replace Proxy with Context in handler and use async_trait
tobyhede bc1aa6b
wip
tobyhede d44599f
fix: lint and cleanup
tobyhede 9c05593
chore: streamline config reload
tobyhede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,11 @@ | ||
| use cipherstash_proxy::config::TandemConfig; | ||
| use cipherstash_proxy::connect::{self, AsyncStream}; | ||
| use cipherstash_proxy::error::Error; | ||
| use cipherstash_proxy::error::{ConfigError, Error}; | ||
| use cipherstash_proxy::prometheus::CLIENTS_ACTIVE_CONNECTIONS; | ||
| use cipherstash_proxy::proxy::Proxy; | ||
| use cipherstash_proxy::{cli, log, postgresql as pg, prometheus, tls, Args}; | ||
| use clap::Parser; | ||
| use metrics::gauge; | ||
| use tokio::net::TcpListener; | ||
| use tokio::signal::unix::{signal, SignalKind}; | ||
| use tokio_util::task::TaskTracker; | ||
| use tracing::{error, info, warn}; | ||
|
|
@@ -55,7 +54,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |
|
|
||
| let mut proxy = init(config).await; | ||
|
|
||
| let mut listener = connect::bind_with_retry(&proxy.config.server).await; | ||
| let listener = connect::bind_with_retry(&proxy.config.server).await; | ||
| let tracker = TaskTracker::new(); | ||
|
|
||
| let mut client_id = 0; | ||
|
|
@@ -81,26 +80,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| break; | ||
| }, | ||
| _ = sighup() => { | ||
| info!(msg = "Received SIGHUP. Reloading configuration"); | ||
| (listener, proxy) = reload_config(listener, &args, proxy).await; | ||
| info!(msg = "Reloaded configuration"); | ||
| info!(msg = "Received SIGHUP. Reloading application configuration"); | ||
| proxy = reload_application_config(&proxy.config, &args).await.unwrap_or(proxy); | ||
| }, | ||
| _ = sigterm() => { | ||
| info!(msg = "Received SIGTERM"); | ||
| break; | ||
| }, | ||
| Ok(client_stream) = AsyncStream::accept(&listener) => { | ||
|
|
||
| let proxy = proxy.clone(); | ||
|
|
||
| client_id += 1; | ||
|
|
||
| let context = proxy.context(client_id); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proxy is the core orchestrator. We create a new context from the proxy for a particular client. |
||
|
|
||
| tracker.spawn(async move { | ||
| let proxy = proxy.clone(); | ||
|
|
||
| gauge!(CLIENTS_ACTIVE_CONNECTIONS).increment(1); | ||
|
|
||
| match pg::handler(client_stream, proxy, client_id).await { | ||
| match pg::handler(client_stream,context).await { | ||
tobyhede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Ok(_) => (), | ||
| Err(err) => { | ||
|
|
||
|
|
@@ -261,25 +258,35 @@ async fn sighup() -> std::io::Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| async fn reload_config(listener: TcpListener, args: &Args, proxy: Proxy) -> (TcpListener, Proxy) { | ||
| fn has_network_config_changed(current: &TandemConfig, new: &TandemConfig) -> bool { | ||
| current.server.host != new.server.host | ||
| || current.server.port != new.server.port | ||
| || current.server.require_tls != new.server.require_tls | ||
| || current.server.worker_threads != new.server.worker_threads | ||
| || current.tls != new.tls | ||
| } | ||
|
|
||
| async fn reload_application_config(config: &TandemConfig, args: &Args) -> Result<Proxy, Error> { | ||
| let new_config = match TandemConfig::load(args) { | ||
| Ok(config) => config, | ||
| Err(err) => { | ||
| warn!( | ||
| msg = "Configuration could not be reloaded: {}", | ||
| error = err.to_string() | ||
| ); | ||
| return (listener, proxy); | ||
| return Err(err); | ||
| } | ||
| }; | ||
|
|
||
| let new_proxy = init(new_config).await; | ||
| // Check for network config changes that require restart | ||
| if has_network_config_changed(config, &new_config) { | ||
| let err = ConfigError::NetworkConfigurationChangeRequiresRestart; | ||
| warn!(msg = err.to_string()); | ||
|
|
||
| // Explicit drop needed here to free the network resources before binding if using the same address & port | ||
| std::mem::drop(listener); | ||
| return Err(err.into()); | ||
| } | ||
|
|
||
| ( | ||
| connect::bind_with_retry(&new_proxy.config.server).await, | ||
| new_proxy, | ||
| ) | ||
| info!(msg = "Configuration reloaded"); | ||
| let proxy = init(new_config).await; | ||
| Ok(proxy) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started with
impl Defaultbut adefaultimplies that there is a sensible default, but there is not.Went with
for_testinginstead, but open to ideas.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this