-
Notifications
You must be signed in to change notification settings - Fork 40
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
Qorb integration as connection pool for database #5876
Changes from 1 commit
a18e659
4695244
0b1e300
91375c4
7d06d29
2c44ecf
aa544fc
d7fd531
6e1dfd9
e008c8c
47b0932
5244907
88f67aa
72369bb
ec1f78b
dce0276
e4d6c78
b5026cb
a362e5d
fbe4126
8293be5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ use diesel::PgConnection; | |
use diesel_dtrace::DTraceConnection; | ||
use qorb::backend::{self, Backend, Error}; | ||
use slog::Logger; | ||
use url::Url; | ||
|
||
pub type DbConnection = DTraceConnection<PgConnection>; | ||
|
||
|
@@ -22,14 +23,15 @@ pub const DISALLOW_FULL_TABLE_SCAN_SQL: &str = | |
/// A [backend::Connector] which provides access to [PgConnection]. | ||
pub(crate) struct DieselPgConnector { | ||
log: Logger, | ||
prefix: String, | ||
suffix: String, | ||
user: String, | ||
db: String, | ||
args: Vec<(String, String)>, | ||
} | ||
|
||
pub(crate) struct DieselPgConnectorArgs<'a> { | ||
pub(crate) user: &'a str, | ||
pub(crate) db: &'a str, | ||
pub(crate) args: Option<&'a str>, | ||
pub(crate) args: Vec<(&'a str, &'a str)>, | ||
} | ||
|
||
impl DieselPgConnector { | ||
|
@@ -47,20 +49,29 @@ impl DieselPgConnector { | |
let DieselPgConnectorArgs { user, db, args } = args; | ||
Self { | ||
log: log.clone(), | ||
prefix: format!("postgresql://{user}@"), | ||
suffix: format!( | ||
"/{db}{}", | ||
args.map(|args| format!("?{args}")).unwrap_or("".to_string()) | ||
), | ||
user: user.to_string(), | ||
db: db.to_string(), | ||
args: args | ||
.into_iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect(), | ||
} | ||
} | ||
|
||
fn to_url(&self, address: std::net::SocketAddr) -> String { | ||
format!( | ||
"{prefix}{address}{suffix}", | ||
prefix = self.prefix, | ||
suffix = self.suffix, | ||
) | ||
fn to_url( | ||
&self, | ||
address: std::net::SocketAddr, | ||
) -> Result<String, anyhow::Error> { | ||
let user = &self.user; | ||
let db = &self.db; | ||
let mut url = | ||
Url::parse(&format!("postgresql://{user}@{address}/{db}"))?; | ||
Comment on lines
+67
to
+68
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. i really thought there was a builder-style interface for constructing URLs from parts like this, but looking at the |
||
|
||
for (k, v) in &self.args { | ||
url.query_pairs_mut().append_pair(k, v); | ||
} | ||
|
||
Ok(url.as_str().to_string()) | ||
} | ||
} | ||
|
||
|
@@ -72,7 +83,7 @@ impl backend::Connector for DieselPgConnector { | |
&self, | ||
backend: &Backend, | ||
) -> Result<Self::Connection, Error> { | ||
let url = self.to_url(backend.address); | ||
let url = self.to_url(backend.address).map_err(Error::Other)?; | ||
|
||
let conn = tokio::task::spawn_blocking(move || { | ||
let pg_conn = DbConnection::establish(&url) | ||
|
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.
This whole implementation is taken from https://github.com/oxidecomputer/qorb/blob/master/src/connectors/diesel_pg.rs -- I originally made the implementation there as an example, and to make sure I didn't break anything, but IMO it makes sense for Omicron to have total control over this (e.g., the layering, the control of full table scans, etc).