@@ -14,12 +14,27 @@ class NoSessionAvailable extends Error {
14
14
}
15
15
}
16
16
17
+ function noop ( ) {
18
+ }
19
+
17
20
const api = module . exports = {
18
21
install ( domain , getConnection , opts ) {
19
- opts = opts || { }
22
+ opts = Object . assign ( {
23
+ maxConcurrency : Infinity ,
24
+ onSessionIdle : noop ,
25
+ onConnectionRequest : noop ,
26
+ onConnectionStart : noop ,
27
+ onConnectionFinish : noop ,
28
+ onTransactionRequest : noop ,
29
+ onTransactionStart : noop ,
30
+ onTransactionFinish : noop ,
31
+ onAtomicRequest : noop ,
32
+ onAtomicStart : noop ,
33
+ onAtomicFinish : noop
34
+ } , opts || { } )
20
35
DOMAIN_TO_SESSION . set ( domain , new Session (
21
36
getConnection ,
22
- opts . maxConcurrency
37
+ opts
23
38
) )
24
39
} ,
25
40
@@ -63,15 +78,29 @@ const api = module.exports = {
63
78
// 3. atomic — grouped set of operations — parent transaction treats all connections performed
64
79
// as a single operation
65
80
class Session {
66
- constructor ( getConnection , maxConcurrency ) {
81
+ constructor ( getConnection , opts ) {
67
82
this . _getConnection = getConnection
68
- this . _activeConnections = 0
69
- this . _maxConcurrency = maxConcurrency || Infinity
83
+ this . activeConnections = 0
84
+ this . maxConcurrency = opts . maxConcurrency || Infinity
85
+ this . metrics = {
86
+ onSessionIdle : opts . onSessionIdle ,
87
+ onConnectionRequest : opts . onConnectionRequest ,
88
+ onConnectionStart : opts . onConnectionStart ,
89
+ onConnectionFinish : opts . onConnectionFinish ,
90
+ onTransactionRequest : opts . onTransactionRequest ,
91
+ onTransactionStart : opts . onTransactionStart ,
92
+ onTransactionFinish : opts . onTransactionFinish ,
93
+ onAtomicRequest : opts . onAtomicRequest ,
94
+ onAtomicStart : opts . onAtomicStart ,
95
+ onAtomicFinish : opts . onAtomicFinish
96
+ }
70
97
this . pending = [ ]
71
98
}
72
99
73
100
getConnection ( ) {
74
- if ( this . _activeConnections === this . _maxConcurrency ) {
101
+ const baton = { }
102
+ this . metrics . onConnectionRequest ( baton )
103
+ if ( this . activeConnections === this . maxConcurrency ) {
75
104
// not using Promise.defer() here in case it gets deprecated by
76
105
// bluebird.
77
106
const pending = _defer ( )
@@ -80,24 +109,29 @@ class Session {
80
109
}
81
110
82
111
const connPair = Promise . resolve ( this . _getConnection ( ) )
83
- ++ this . _activeConnections
112
+ ++ this . activeConnections
84
113
85
- return connPair . then (
86
- pair => new SessionConnectionPair ( pair , this )
87
- )
114
+ return connPair . then ( pair => {
115
+ this . metrics . onConnectionStart ( baton )
116
+ return new SessionConnectionPair ( pair , this , baton )
117
+ } )
88
118
}
89
119
90
120
transaction ( operation , args ) {
121
+ const baton = { }
91
122
const getConnPair = this . getConnection ( )
92
- const getResult = Session$RunWrapped ( connPair => {
93
- return new TransactionSession ( connPair )
123
+ this . metrics . onTransactionRequest ( baton , operation , args )
124
+ const getResult = Session$RunWrapped ( this , connPair => {
125
+ this . metrics . onTransactionStart ( baton , operation , args )
126
+ return new TransactionSession ( connPair , this . metrics )
94
127
} , getConnPair , `BEGIN` , {
95
128
success : `COMMIT` ,
96
129
failure : `ROLLBACK`
97
130
} , operation , args )
98
131
99
132
const releasePair = getConnPair . then ( pair => {
100
133
return getResult . reflect ( ) . then ( result => {
134
+ this . metrics . onTransactionFinish ( baton , operation , args , result )
101
135
return result . isFulfilled ( )
102
136
? pair . release ( )
103
137
: pair . release ( result . reason ( ) )
@@ -114,16 +148,17 @@ class Session {
114
148
}
115
149
116
150
releasePair ( pair , err ) {
117
- -- this . _activeConnections
151
+ -- this . activeConnections
118
152
pair . release ( err )
119
153
}
120
154
}
121
155
122
156
class TransactionSession {
123
- constructor ( connPair ) {
157
+ constructor ( connPair , metrics ) {
124
158
this . connectionPair = connPair
125
159
this . inactive = false
126
160
this . operation = Promise . resolve ( true )
161
+ this . metrics = metrics
127
162
}
128
163
129
164
getConnection ( ) {
@@ -148,17 +183,21 @@ class TransactionSession {
148
183
}
149
184
150
185
atomic ( operation , args ) {
186
+ const baton = { }
151
187
const atomicConnPair = this . getConnection ( )
152
188
const savepointName = getSavepointName ( operation )
153
- const getResult = Session$RunWrapped ( connPair => {
154
- return new AtomicSession ( connPair , savepointName )
189
+ this . metrics . onAtomicRequest ( baton , operation , args )
190
+ const getResult = Session$RunWrapped ( this , connPair => {
191
+ this . metrics . onAtomicStart ( baton , operation , args )
192
+ return new AtomicSession ( connPair , this . metrics , savepointName )
155
193
} , atomicConnPair , `SAVEPOINT ${ savepointName } ` , {
156
194
success : `RELEASE SAVEPOINT ${ savepointName } ` ,
157
195
failure : `ROLLBACK TO SAVEPOINT ${ savepointName } `
158
196
} , operation , args )
159
197
160
198
const releasePair = atomicConnPair . then ( pair => {
161
199
return getResult . reflect ( ) . then ( result => {
200
+ this . metrics . onAtomicFinish ( baton , operation , args , result )
162
201
return result . isFulfilled ( )
163
202
? pair . release ( )
164
203
: pair . release ( result . reason ( ) )
@@ -170,13 +209,14 @@ class TransactionSession {
170
209
}
171
210
172
211
class AtomicSession extends TransactionSession {
173
- constructor ( connection , name ) {
174
- super ( connection )
212
+ constructor ( connection , metrics , name ) {
213
+ super ( connection , metrics )
175
214
this . name = name
176
215
}
177
216
}
178
217
179
- function Session$RunWrapped ( createSession ,
218
+ function Session$RunWrapped ( parent ,
219
+ createSession ,
180
220
getConnPair ,
181
221
before ,
182
222
after ,
0 commit comments