Skip to content

Commit 4850fef

Browse files
committed
performance - read only pool POC
Signed-off-by: Amit Prinz Setter <[email protected]>
1 parent 4986d5d commit 4850fef

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/util/postgres_client.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ class PostgresTable {
626626
}
627627

628628

629-
get_pool() {
630-
const pool = this.client.get_pool(this.pool_key);
629+
get_pool(key = this.pool_key) {
630+
const pool = this.client.get_pool(key);
631631
if (!pool) {
632-
throw new Error(`The postgres clients pool ${this.pool_key} disconnected`);
632+
throw new Error(`The postgres clients pool ${key} disconnected`);
633633
}
634634
return pool;
635635
}
@@ -1494,6 +1494,10 @@ class PostgresClient extends EventEmitter {
14941494
md: {
14951495
instance: null,
14961496
size: config.POSTGRES_MD_MAX_CLIENTS
1497+
},
1498+
read_only: {
1499+
instance: null,
1500+
size: config.POSTGRES_DEFAULT_MAX_CLIENTS
14971501
}
14981502
};
14991503

@@ -1507,6 +1511,8 @@ class PostgresClient extends EventEmitter {
15071511
} else {
15081512
// get the connection configuration. first from env, then from file, then default
15091513
const host = process.env.POSTGRES_HOST || fs_utils.try_read_file_sync(process.env.POSTGRES_HOST_PATH) || '127.0.0.1';
1514+
//optional read-only host. if not present defaults to general pg host
1515+
const host_ro = process.env.POSTGRES_HOST_RO || fs_utils.try_read_file_sync(process.env.POSTGRES_HOST_RO_PATH) || host;
15101516
const user = process.env.POSTGRES_USER || fs_utils.try_read_file_sync(process.env.POSTGRES_USER_PATH) || 'postgres';
15111517
const password = process.env.POSTGRES_PASSWORD || fs_utils.try_read_file_sync(process.env.POSTGRES_PASSWORD_PATH) || 'noobaa';
15121518
const database = process.env.POSTGRES_DBNAME || fs_utils.try_read_file_sync(process.env.POSTGRES_DBNAME_PATH) || 'nbcore';
@@ -1520,6 +1526,7 @@ class PostgresClient extends EventEmitter {
15201526
port,
15211527
...params,
15221528
};
1529+
this.pools.read_only.host = host_ro;
15231530
}
15241531
// As we now also support external DB we don't want to print secret user data
15251532
// so this code will mask out passwords from the printed pool params
@@ -1718,8 +1725,12 @@ class PostgresClient extends EventEmitter {
17181725
if (!pool) {
17191726
throw new Error(`create_pool: the pool ${name} is not defined in pools object`);
17201727
}
1728+
const new_pool_params = _.clone(this.new_pool_params);
1729+
if (pool.host) {
1730+
new_pool_params.host = pool.host;
1731+
}
17211732
if (!pool.instance) {
1722-
pool.instance = new Pool({ ...this.new_pool_params, max: pool.size });
1733+
pool.instance = new Pool({ ...new_pool_params, max: pool.size });
17231734
if (!pool._error_listener) {
17241735
pool.error_listener = err => {
17251736
dbg.error(`got error on postgres pool ${name}`, err);

0 commit comments

Comments
 (0)