Skip to content

Commit f629014

Browse files
committed
Add connections stats
1 parent 0428b30 commit f629014

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/index.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,34 @@ function Postgres(a, b) {
5252

5353
let ending = false
5454

55-
const queries = Queue()
56-
, connecting = Queue()
57-
, reserved = Queue()
58-
, closed = Queue()
59-
, ended = Queue()
60-
, open = Queue()
61-
, busy = Queue()
62-
, full = Queue()
55+
const queries = Queue('queries')
56+
, connecting = Queue('connecting')
57+
, reserved = Queue('reserved')
58+
, closed = Queue('closed')
59+
, ended = Queue('ended')
60+
, open = Queue('open')
61+
, busy = Queue('busy')
62+
, full = Queue('full')
6363
, queues = { connecting, reserved, closed, ended, open, busy, full }
6464

6565
const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))
6666

6767
const sql = Sql(handler)
6868

6969
Object.assign(sql, {
70+
queries,
71+
connections: {
72+
onchange: null,
73+
max: options.max,
74+
get open() { return reserved.length + open.length + busy.length + full.length },
75+
get connecting() { return connecting.length },
76+
get ended() { return ended.length },
77+
get closed() { return closed.length },
78+
get reserved() { return reserved.length },
79+
get idle() { return open.length },
80+
get busy() { return busy.length },
81+
get full() { return full.length }
82+
},
7083
get parameters() { return options.parameters },
7184
largeObject: largeObject.bind(null, sql),
7285
subscribe,
@@ -308,6 +321,8 @@ function Postgres(a, b) {
308321
queue === open
309322
? c.idleTimer.start()
310323
: c.idleTimer.cancel()
324+
325+
sql.connections.onchange && sql.connections.onchange(queue.name)
311326
return c
312327
}
313328

src/queue.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
export default Queue
22

3-
function Queue(initial = []) {
4-
let xs = initial.slice()
3+
function Queue(name) {
4+
let xs = []
55
let index = 0
66

77
return {
8+
name,
89
get length() {
910
return xs.length - index
1011
},

0 commit comments

Comments
 (0)