Skip to content

Commit

Permalink
Add connections stats
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Oct 10, 2023
1 parent 31f9856 commit 9315136
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,34 @@ function Postgres(a, b) {

let ending = false

const queries = Queue()
, connecting = Queue()
, reserved = Queue()
, closed = Queue()
, ended = Queue()
, open = Queue()
, busy = Queue()
, full = Queue()
const queries = Queue('queries')
, connecting = Queue('connecting')
, reserved = Queue('reserved')
, closed = Queue('closed')
, ended = Queue('ended')
, open = Queue('open')
, busy = Queue('busy')
, full = Queue('full')
, queues = { connecting, reserved, closed, ended, open, busy, full }

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

const sql = Sql(handler)

Object.assign(sql, {
queries,
connections: {
onchange: null,
max: options.max,
get open() { return reserved.length + open.length + busy.length + full.length },
get connecting() { return connecting.length },
get ended() { return ended.length },
get closed() { return closed.length },
get reserved() { return reserved.length },
get idle() { return open.length },
get busy() { return busy.length },
get full() { return full.length }
},
get parameters() { return options.parameters },
largeObject: largeObject.bind(null, sql),
subscribe,
Expand Down Expand Up @@ -308,6 +321,8 @@ function Postgres(a, b) {
queue === open
? c.idleTimer.start()
: c.idleTimer.cancel()

sql.connections.onchange && sql.connections.onchange(queue.name)
return c
}

Expand Down
5 changes: 3 additions & 2 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export default Queue

function Queue(initial = []) {
let xs = initial.slice()
function Queue(name) {
let xs = []
let index = 0

return {
name,
get length() {
return xs.length - index
},
Expand Down

0 comments on commit 9315136

Please sign in to comment.