Skip to content

Commit 5aac641

Browse files
add onTransactionConnection* events, docs
1 parent bb244e8 commit 5aac641

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Install a database `Session` on the domain `d`.
8181

8282
Sessions accept the following options:
8383

84+
<img align="right" src="https://cloud.githubusercontent.com/assets/37303/12259904/985ef78a-b8cd-11e5-8665-0535caa69334.png" alt="assets/event-flowchart.dot" />
85+
8486
* `maxConcurrency`: An integer specifying the maximum number of connections a
8587
given session will make at a time. `0` is treated as `Infinity`. Defaults to
8688
`Infinity`. *Note:* this number is implicitly bound by the size of the `pg`
@@ -99,21 +101,29 @@ Sessions accept the following options:
99101
an optional `err` parameter that will be called when a connection is released
100102
back to the session.
101103
* `onTransactionRequest(baton, operation, args)`: A function accepting a baton,
102-
function, and array of arguments representing the request for a transaction
104+
function, and array of arguments, representing the request for a transaction
103105
session. Called coincident with `onConnectionRequest`.
104106
* `onTransactionStart(baton, operation, args)`: A function accepting a baton,
105-
function, and array of arguments representing the fulfillment of a request
107+
function, and array of arguments, representing the fulfillment of a request
106108
for a transaction session. Called before `BEGIN`, coincident with
107109
`onConnectionStart`.
108110
* `onTransactionFinish(baton, operation, args, PromiseInspection)`:
109111
A function accepting a baton, function, array of arguments, and a
110112
[`PromiseInspection`][bluebird-inspection] representing the state of the
111113
transaction. Called coincident with `onConnectionFinish`.
114+
* `onTransactionConnectionRequest(baton)`: A function accepting a baton,
115+
representing the request for a connection within a transaction session.
116+
* `onTransactionConnectionStart(baton)`: A function accepting a baton,
117+
representing the fulfillment of a request for a connection within a
118+
transaction session.
119+
* `onTransactionConnectionFinish(baton, err)`: A function accepting a baton
120+
and an optional `err` argument, representing the completion of a transaction
121+
connection within a transaction session.
112122
* `onAtomicRequest(baton, operation, args)`: A function accepting a baton,
113-
function, and array of arguments representing the request for an atomic
123+
function, and array of arguments, representing the request for an atomic
114124
session.
115125
* `onAtomicStart(baton, operation, args)`: A function accepting a baton,
116-
function, and array of arguments representing the fulfillment of a request
126+
function, and array of arguments, representing the fulfillment of a request
117127
for an atomic session.
118128
* `onAtomicFinish(baton, operation, args, PromiseInspection)`:
119129
A function accepting a baton, function, array of arguments, and a

assets/event-flowchart.dot

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
digraph metrics {
2+
A [label="(beginning state)"];
3+
A -> onConnectionRequest;
4+
onConnectionRequest -> onConnectionStart;
5+
onConnectionStart -> onConnectionFinish;
6+
onConnectionFinish -> onSessionIdle;
7+
onConnectionFinish -> onConnectionStart;
8+
onSessionIdle -> onConnectionRequest;
9+
10+
onConnectionRequest -> onTransactionRequest;
11+
onTransactionRequest -> onConnectionStart;
12+
onConnectionStart -> onTransactionStart;
13+
onTransactionStart -> onTransactionFinish;
14+
onTransactionFinish -> onConnectionFinish;
15+
16+
onTransactionStart -> onTransactionConnectionRequest;
17+
onTransactionConnectionRequest -> onTransactionConnectionStart;
18+
onTransactionConnectionRequest -> onAtomicRequest;
19+
onAtomicRequest -> onTransactionConnectionStart;
20+
onTransactionConnectionStart -> onTransactionConnectionFinish;
21+
onTransactionConnectionFinish -> onTransactionConnectionStart;
22+
onTransactionConnectionFinish -> onTransactionFinish;
23+
24+
onTransactionConnectionStart -> onAtomicStart;
25+
onAtomicFinish -> onTransactionConnectionFinish;
26+
onAtomicStart -> onAtomicFinish;
27+
}
28+

db-session.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const api = module.exports = {
2828
onTransactionRequest: noop,
2929
onTransactionStart: noop,
3030
onTransactionFinish: noop,
31+
onTransactionConnectionRequest: noop,
32+
onTransactionConnectionStart: noop,
33+
onTransactionConnectionFinish: noop,
3134
onAtomicRequest: noop,
3235
onAtomicStart: noop,
3336
onAtomicFinish: noop
@@ -90,6 +93,9 @@ class Session {
9093
onTransactionRequest: opts.onTransactionRequest,
9194
onTransactionStart: opts.onTransactionStart,
9295
onTransactionFinish: opts.onTransactionFinish,
96+
onTransactionConnectionRequest: opts.onTransactionConnectionRequest,
97+
onTransactionConnectionStart: opts.onTransactionConnectionStart,
98+
onTransactionConnectionFinish: opts.onTransactionConnectionFinish,
9399
onAtomicRequest: opts.onAtomicRequest,
94100
onAtomicStart: opts.onAtomicStart,
95101
onAtomicFinish: opts.onAtomicFinish
@@ -167,10 +173,13 @@ class TransactionSession {
167173
reject(new NoSessionAvailable())
168174
})
169175
}
176+
177+
const baton = {}
178+
this.metrics.onTransactionConnectionRequest(baton)
170179
// NB(chrisdickinson): creating a TxConnPair implicitly
171180
// swaps out "this.operation", creating a linked list of
172181
// promises.
173-
return new TxSessionConnectionPair(this).onready
182+
return new TxSessionConnectionPair(this, baton).onready
174183
}
175184

176185
transaction (operation, args) {

lib/tx-session-connpair.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const REJECT_SYM = Symbol()
1616
// connection — see Session#transaction and TransactionSession#atomic
1717
// for more details (specifically, look for "release()".)
1818
module.exports = class TransactionSessionConnPair {
19-
constructor (session) {
19+
constructor (session, baton) {
20+
const metrics = session.metrics
2021
this.pair = session.connectionPair
21-
this.release = err => release(this, err)
22+
this.release = err => release(this, metrics, baton, err)
2223
this.completed = new Promise((resolve, reject) => {
2324
this[RESOLVE_SYM] = resolve
2425
this[REJECT_SYM] = reject
@@ -38,7 +39,10 @@ module.exports = class TransactionSessionConnPair {
3839
// | |
3940
// completed → onready → (work happens) → completed → onready
4041
//
41-
this.onready = session.operation.then(() => this)
42+
this.onready = session.operation.then(() => {
43+
metrics.onTransactionConnectionStart(baton)
44+
return this
45+
})
4246
session.operation = this.completed
4347
}
4448

@@ -47,6 +51,7 @@ module.exports = class TransactionSessionConnPair {
4751
}
4852
}
4953

50-
function release (conn, err) {
54+
function release (conn, metrics, baton, err) {
55+
metrics.onTransactionConnectionFinish(baton, err)
5156
return err ? conn[REJECT_SYM](err) : conn[RESOLVE_SYM]()
5257
}

0 commit comments

Comments
 (0)