Skip to content
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

DB max connection env #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ext Cardano DBSync

The approach of this project is to allow a CRD to DBSync on the K8S cluster and an operator will enable the required resources to expose an DBSync port.
The approach of this project is to allow a CRD to DBSync on the K8S cluster and an operator will enable the required resources to expose a DBSync port.

## Folder structure

Expand Down
19 changes: 10 additions & 9 deletions operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ This project is a Kubernetes custom controller to create users on dbsync's Postg
```
shared_preload_libraries = 'pg_stat_statements'
```
- create the extension on postgres
- create the extension on Postgres
```
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
```

## Environment

| Key | Value |
| -------------- | --------------------------------------------------------------------------------------- |
| ADDR | 0.0.0.0:5000 |
| DB_URLS | postgres://postgres:[email protected]:5432,postgres://postgres:[email protected]:5433 |
| DB_NAMES | preview=dbsync-preview,preprod=dbsync-preprod,mainnet=dbsync-mainnet |
| DCU_PER_SECOND | preview=5,preprod=5,mainnet=5 |
| METRICS_DELAY | 30 |
| Key | Value |
| ------------------ | --------------------------------------------------------------------------------------- |
| ADDR | 0.0.0.0:5000 |
| DB_URLS | postgres://postgres:[email protected]:5432,postgres://postgres:[email protected]:5433 |
| DB_NAMES | preview=dbsync-preview,preprod=dbsync-preprod,mainnet=dbsync-mainnet |
| DB_MAX_CONNECTIONS | 2 |
| DCU_PER_SECOND | preview=5,preprod=5,mainnet=5 |
| METRICS_DELAY | 30 |


## Commands
Expand All @@ -41,7 +42,7 @@ cargo run

## Metrics

to collect metrics for Prometheus, an http api will enable with the route /metrics.
to collect metrics for Prometheus, an HTTP API will enable the route /metrics.

```
/metrics
Expand Down
9 changes: 9 additions & 0 deletions operator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn get_config() -> &'static Config {
pub struct Config {
pub db_urls: Vec<String>,
pub db_names: HashMap<String, String>,
pub db_max_connections: usize,
pub dcu_per_second: HashMap<String, f64>,

pub metrics_delay: Duration,
Expand All @@ -35,6 +36,13 @@ impl Config {
})
.collect();

let db_max_connections = env::var("DB_MAX_CONNECTIONS")
.map(|v| {
v.parse::<usize>()
.expect("DB_MAX_CONNECTIONS must be number usize")
})
.unwrap_or(2);

let dcu_per_second = env::var("DCU_PER_SECOND")
.expect("DCU_PER_SECOND must be set")
.split(',')
Expand All @@ -58,6 +66,7 @@ impl Config {
Self {
db_urls,
db_names,
db_max_connections,
dcu_per_second,
metrics_delay,
}
Expand Down
4 changes: 3 additions & 1 deletion operator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ impl State {
for (network, db_name) in config.db_names.iter() {
let mut connections: Vec<Postgres> = Vec::new();
for url in config.db_urls.iter() {
let connection = Postgres::try_new(&format!("{}/{}", url, db_name)).await?;
let connection =
Postgres::try_new(&format!("{}/{}", url, db_name), &config.db_max_connections)
.await?;
connections.push(connection);
}

Expand Down
4 changes: 2 additions & 2 deletions operator/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pub struct Postgres {
}

impl Postgres {
pub async fn try_new(url: &str) -> Result<Self, Error> {
pub async fn try_new(url: &str, max_size: &usize) -> Result<Self, Error> {
let mgr_config = ManagerConfig {
recycling_method: RecyclingMethod::Fast,
};

let config = tokio_postgres::Config::from_str(url)?;

let mgr = Manager::from_config(config, NoTls, mgr_config);
let pool = Pool::builder(mgr).build()?;
let pool = Pool::builder(mgr).max_size(*max_size).build()?;

Ok(Self { pool })
}
Expand Down
Loading