-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayground.js
More file actions
134 lines (97 loc) · 3.28 KB
/
playground.js
File metadata and controls
134 lines (97 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const {Pool} = require("./node_postgres_ha.js");
const connection_cfg = {
autoRecover: true, // Enable our high availability feature!
autoCancel: true,
max: 4,
// application_name?: string, // The name of the application that created this Client instance
// connectionTimeoutMillis?: number, // number of milliseconds to wait for connection, default is no timeout
/////connectionTimeoutMillis: 1000,
// statement_timeout?: number, // number of milliseconds before a statement in query will time out, default is no timeout
////statement_timeout: 5000,
// query_timeout?: number, // number of milliseconds before a query call will timeout, default is no timeout
//// query_timeout: 5100,
// lock_timeout?: number, // number of milliseconds a query is allowed to be en lock state before it's cancelled due to lock timeout
////lock_timeout: 100, // ✅ No ens afecta.
// idle_in_transaction_session_timeout?: number // number of milliseconds before terminating any session with an open idle transaction, default is no timeout
};
const pool = new Pool(connection_cfg);
false &&
pool.on('ready', function(ev) {
console.log(` 🔔 Ready!!!: `, ev.message); //, client);
});
false &&
pool.on('allErrors', function(err, client) {
console.log(` 🔔${client ? "🔔" : "-"} Error: ${err.message}`); //, client);
});
false &&
pool.on('connect', function(client) {
console.log(` 🔔 Connect: `); //, client);
});
false &&
pool.on('acquire', function(client) {
console.log(` 🔔 Acquire: `); //, client);
});
false &&
pool.on('error', function(err, client) {
console.log(` 🔔 Error: ${err.message}`); //, client);
});
false &&
pool.on('release', function(err, client) {
console.log(` 🔔 Release: ${err? err.message : "No Errors"}`); //, client);
});
false &&
pool.on('remove', function(client) {
console.log(` 🔔 Remove: `); //, client);
});
const connections = [];
function wireConnection(c) {
connections.push(c);
///console.log(`🔗 Wired new connection. Total: ${connections.length}`);
};
function failConnection(err) {
///console.error(`🔥 Failed wiring new connection: ${err.message}`);
///console.error(err);
};
function addConnection() {
pool.connect().then(wireConnection).catch(failConnection);
};
// addConnection();
// addConnection();
// addConnection();
// addConnection();
// addConnection();
// addConnection();
function getPromiseState(promise) {
const pending = {};
return Promise.race([promise, pending])
.then(
value => (value === pending) ? 'pending' : 'fulfilled'
, () => 'rejected'
);
};
async function testConnection(c, label = "??", {secs = 5} = {}) {
try {
const {rows: [{t}]} = await c.query(
"select now() as t from (select pg_sleep($1)) as foo"
, [secs]
);
console.log(`✅ ${label}: ${t}`);
} catch (err) {
console.error(`❌ ${label}: Test Failed! ${err.message}`);
console.error(err);
};
};
function query(...args) {
pool.query(...args)
.then(({rows})=>console.log("✅", rows))
.catch(err=>console.error("❌", err.message))
;
};
module.exports = {
pool,
query,
connections,
getPromiseState,
testConnection,
addConnection,
};