Skip to content

Commit

Permalink
Make PG pool size configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Jul 29, 2019
1 parent c6c82e2 commit f0bc664
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
22 changes: 15 additions & 7 deletions t-rex-core/src/datasource/postgis_ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub struct SqlQuery {
#[derive(Clone)]
pub struct PostgisDatasource {
pub connection_url: String,
pub pool_size: Option<u16>,
conn_pool: Option<r2d2::Pool<PostgresConnectionManager>>,
// Queries for all layers and zoom levels
queries: BTreeMap<String, BTreeMap<u8, SqlQuery>>,
Expand Down Expand Up @@ -247,9 +248,10 @@ impl SqlQuery {
}

impl PostgisDatasource {
pub fn new(connection_url: &str) -> PostgisDatasource {
pub fn new(connection_url: &str, pool_size: Option<u16>) -> PostgisDatasource {
PostgisDatasource {
connection_url: connection_url.to_string(),
pool_size,
conn_pool: None,
queries: BTreeMap::new(),
}
Expand Down Expand Up @@ -611,12 +613,12 @@ impl PostgisDatasource {
impl DatasourceType for PostgisDatasource {
/// New instance with connected pool
fn connected(&self) -> PostgisDatasource {
let pool_size = 10; //FIXME: make configurable
// Emulate TlsMode::Allow (https://github.com/sfackler/rust-postgres/issues/278)
// Emulate TlsMode::Allow (https://github.com/sfackler/rust-postgres/issues/278)
let manager =
PostgresConnectionManager::new(self.connection_url.as_ref(), TlsMode::None).unwrap();
let pool_size = self.pool_size.unwrap_or(8); // TODO: use number of workers as default pool size
let pool = r2d2::Pool::builder()
.max_size(pool_size)
.max_size(pool_size as u32)
.build(manager)
.or_else(|e| match e.description() {
"unable to initialize connections" => {
Expand All @@ -627,13 +629,16 @@ impl DatasourceType for PostgisDatasource {
TlsMode::Require(Box::new(negotiator)),
)
.unwrap();
r2d2::Pool::builder().max_size(pool_size).build(manager)
r2d2::Pool::builder()
.max_size(pool_size as u32)
.build(manager)
}
_ => Err(e),
})
.unwrap();
PostgisDatasource {
connection_url: self.connection_url.clone(),
pool_size: Some(pool_size),
conn_pool: Some(pool),
queries: BTreeMap::new(),
}
Expand Down Expand Up @@ -868,9 +873,12 @@ impl<'a> Config<'a, DatasourceCfg> for PostgisDatasource {
fn from_config(ds_cfg: &DatasourceCfg) -> Result<Self, String> {
if let Ok(url) = env::var("TREX_DATASOURCE_URL") {
// FIXME: this overwrites *all* PostGIS connections instead of a specific one
Ok(PostgisDatasource::new(url.as_str()))
Ok(PostgisDatasource::new(url.as_str(), None))
} else {
Ok(PostgisDatasource::new(ds_cfg.dbconn.as_ref().unwrap()))
Ok(PostgisDatasource::new(
ds_cfg.dbconn.as_ref().unwrap(),
ds_cfg.pool,
))
}
}

Expand Down
14 changes: 7 additions & 7 deletions t-rex-core/src/datasource/postgis_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_from_geom_fields() {
#[ignore]
fn test_detect_layers() {
let pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand All @@ -75,7 +75,7 @@ fn test_detect_layers() {
#[ignore]
fn test_detect_columns() {
let pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand All @@ -99,7 +99,7 @@ fn test_detect_columns() {
#[ignore]
fn test_extent_query() {
let pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand All @@ -121,7 +121,7 @@ fn test_extent_query() {

#[test]
fn test_feature_query() {
let pg = PostgisDatasource::new("postgresql://pi@localhost/osm2vectortiles");
let pg = PostgisDatasource::new("postgresql://pi@localhost/osm2vectortiles", Some(1));
let mut layer = Layer::new("points");
layer.table_name = Some(String::from("osm_place_point"));
layer.geometry_field = Some(String::from("geometry"));
Expand Down Expand Up @@ -236,7 +236,7 @@ fn test_feature_query() {

#[test]
fn test_query_params() {
let pg = PostgisDatasource::new("postgresql://pi@localhost/osm2vectortiles");
let pg = PostgisDatasource::new("postgresql://pi@localhost/osm2vectortiles", Some(1));
let mut layer = Layer::new("buildings");
layer.geometry_field = Some(String::from("way"));

Expand Down Expand Up @@ -281,7 +281,7 @@ fn test_query_params() {
#[ignore]
fn test_retrieve_features() {
let mut pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand Down Expand Up @@ -340,7 +340,7 @@ fn test_retrieve_features() {
#[should_panic(expected = "geometry_field undefined")]
fn test_no_geom_field() {
let mut pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion t-rex-service/src/datasources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Datasources {
if let Some(dbconn) = args.value_of("dbconn") {
datasources.add(
&"dbconn".to_string(),
Datasource::Postgis(PostgisDatasource::new(dbconn)),
Datasource::Postgis(PostgisDatasource::new(dbconn, None)),
);
}
if let Some(datasource) = args.value_of("datasource") {
Expand Down
2 changes: 1 addition & 1 deletion t-rex-service/src/mvt_service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn mvt_service() -> MvtService {
use std::env;

let pg: PostgisDatasource = match env::var("DBCONN") {
Result::Ok(val) => Some(PostgisDatasource::new(&val).connected()),
Result::Ok(val) => Some(PostgisDatasource::new(&val, Some(1)).connected()),
Result::Err(_) => panic!("DBCONN undefined"),
}
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion t-rex-service/src/qgs_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub fn read_qgs(fname: &str) -> (Datasources, Tileset) {
layer.geometry_field = Some(info.geometry_field);
layer.geometry_type = Some(info.geometry_type);
layer.srid = Some(info.srid);
Datasource::Postgis(PostgisDatasource::new(&info.dbconn))
Datasource::Postgis(PostgisDatasource::new(&info.dbconn, None))
}
_ => continue,
};
Expand Down

0 comments on commit f0bc664

Please sign in to comment.