-
Notifications
You must be signed in to change notification settings - Fork 21
Enhanced Server Stability and Configuration Control #150
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ use pgwire::tokio::process_socket; | |
| use rustls_pemfile::{certs, pkcs8_private_keys}; | ||
| use rustls_pki_types::{CertificateDer, PrivateKeyDer}; | ||
| use tokio::net::TcpListener; | ||
| use tokio::sync::Semaphore; | ||
| use tokio_rustls::rustls::{self, ServerConfig}; | ||
| use tokio_rustls::TlsAcceptor; | ||
|
|
||
|
|
@@ -34,12 +35,24 @@ pub struct ServerOptions { | |
| port: u16, | ||
| tls_cert_path: Option<String>, | ||
| tls_key_path: Option<String>, | ||
| max_connections: usize, | ||
| query_timeout: Option<std::time::Duration>, | ||
| } | ||
|
|
||
| impl ServerOptions { | ||
| pub fn new() -> ServerOptions { | ||
| ServerOptions::default() | ||
| } | ||
|
|
||
| /// Set query timeout from seconds. Use 0 for no timeout. | ||
| pub fn with_query_timeout_secs(mut self, timeout_secs: u64) -> Self { | ||
| self.query_timeout = if timeout_secs == 0 { | ||
| None | ||
| } else { | ||
| Some(std::time::Duration::from_secs(timeout_secs)) | ||
| }; | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Default for ServerOptions { | ||
|
|
@@ -49,6 +62,8 @@ impl Default for ServerOptions { | |
| port: 5432, | ||
| tls_cert_path: None, | ||
| tls_key_path: None, | ||
| max_connections: 1000, | ||
|
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. default to 0, which means no restriction on this. |
||
| query_timeout: Some(std::time::Duration::from_secs(30)), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -85,7 +100,11 @@ pub async fn serve( | |
| let auth_manager = Arc::new(AuthManager::new()); | ||
|
|
||
| // Create the handler factory with authentication | ||
| let factory = Arc::new(HandlerFactory::new(session_context, auth_manager)); | ||
| let factory = Arc::new(HandlerFactory::new( | ||
| session_context, | ||
| auth_manager, | ||
| opts.query_timeout, | ||
| )); | ||
|
|
||
| serve_with_handlers(factory, opts).await | ||
| } | ||
|
|
@@ -126,17 +145,31 @@ pub async fn serve_with_handlers( | |
| info!("Listening on {server_addr} (unencrypted)"); | ||
| } | ||
|
|
||
| // Connection limiter to prevent resource exhaustion | ||
| let connection_semaphore = Arc::new(Semaphore::new(opts.max_connections)); | ||
|
|
||
| // Accept incoming connections | ||
| loop { | ||
| match listener.accept().await { | ||
| Ok((socket, _addr)) => { | ||
| Ok((socket, addr)) => { | ||
| let factory_ref = handlers.clone(); | ||
| let tls_acceptor_ref = tls_acceptor.clone(); | ||
| let semaphore_ref = connection_semaphore.clone(); | ||
|
|
||
| tokio::spawn(async move { | ||
| // Acquire connection permit to limit concurrency | ||
| let _permit = match semaphore_ref.try_acquire() { | ||
| Ok(permit) => permit, | ||
| Err(_) => { | ||
| warn!("Connection rejected from {addr}: max connections reached"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| if let Err(e) = process_socket(socket, tls_acceptor_ref, factory_ref).await { | ||
| warn!("Error processing socket: {e}"); | ||
| warn!("Error processing socket from {addr}: {e}"); | ||
| } | ||
| // Permit is automatically released when _permit is dropped | ||
| }); | ||
| } | ||
| Err(e) => { | ||
|
|
||
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
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.
I suggest you to put this into a new pull request. Also it should be configured at per-session level using
SET statement_timeout