Skip to content

Commit cdbd4c8

Browse files
committed
Internal code improvements
1 parent ac0aa2f commit cdbd4c8

File tree

5 files changed

+46
-44
lines changed

5 files changed

+46
-44
lines changed

lib/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,9 +1584,9 @@ class Connection extends EventEmitter {
15841584
// that cannot take place concurrently
15851585
// NOTE: breakExecution() should not be serialized
15861586
Connection.prototype.break =
1587-
nodbUtil.callbackify(Connection.prototype.breakExecution);
1587+
nodbUtil.callbackify(nodbUtil.wrapFn(Connection.prototype.breakExecution));
15881588
Connection.prototype.tpcRecover =
1589-
nodbUtil.callbackify(Connection.prototype.tpcRecover);
1589+
nodbUtil.callbackify(nodbUtil.wrapFn(Connection.prototype.tpcRecover));
15901590
nodbUtil.wrapFns(Connection.prototype,
15911591
"changePassword",
15921592
"close",

lib/lob.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ nodbUtil.wrapFns(Lob.prototype, errors.ERR_BUSY_LOB,
300300
"close",
301301
"fileExists",
302302
"getData");
303-
Lob.prototype._serializedRead = nodbUtil.serialize(Lob.prototype._readData);
304-
Lob.prototype._serializedWrite = nodbUtil.serialize(Lob.prototype._writeData);
303+
Lob.prototype._serializedRead = nodbUtil.wrapFn(Lob.prototype._readData, true);
304+
Lob.prototype._serializedWrite =
305+
nodbUtil.wrapFn(Lob.prototype._writeData, true);
305306

306307
module.exports = Lob;

lib/oracledb.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,12 +879,12 @@ module.exports = {
879879
SodaOperation,
880880

881881
// top-level functions
882-
getConnection: nodbUtil.callbackify(getConnection),
883-
createPool: nodbUtil.callbackify(createPool),
882+
getConnection: nodbUtil.callbackify(nodbUtil.wrapFn(getConnection)),
883+
createPool: nodbUtil.callbackify(nodbUtil.wrapFn(createPool)),
884884
getPool,
885885
initOracleClient,
886-
shutdown: nodbUtil.callbackify(shutdown),
887-
startup: nodbUtil.callbackify(startup),
886+
shutdown: nodbUtil.callbackify(nodbUtil.wrapFn(shutdown)),
887+
startup: nodbUtil.callbackify(nodbUtil.wrapFn(startup)),
888888

889889
// CQN operation codes
890890
CQN_OPCODE_ALL_OPS: constants.CQN_OPCODE_ALL_OPS,

lib/pool.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,11 @@ class Pool extends EventEmitter {
800800

801801
}
802802

803-
Pool.prototype.close = nodbUtil.callbackify(Pool.prototype.close);
804-
Pool.prototype.getConnection = nodbUtil.callbackify(Pool.prototype.getConnection);
805-
Pool.prototype.reconfigure = nodbUtil.callbackify(Pool.prototype.reconfigure);
806-
Pool.prototype.setAccessToken = nodbUtil.callbackify(Pool.prototype.setAccessToken);
803+
nodbUtil.wrapFns(Pool.prototype, false,
804+
"close",
805+
"getConnection",
806+
"reconfigure",
807+
"setAccessToken");
807808

808809
// DEPRECATED aliases
809810
Pool.prototype.terminate = Pool.prototype.close;

lib/util.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,37 +124,41 @@ function callbackify(func) {
124124
// if last argument is not a function, simply invoke the function as usual
125125
// and a promise will be returned
126126
if (typeof arguments[arguments.length - 1] !== 'function') {
127-
return func.apply(this, arguments).catch(function stackCapture(err) {
128-
throw errors.transformErr(err, stackCapture);
129-
});
127+
return func.apply(this, arguments);
130128
}
131129

132130
// otherwise, resolve or reject the promise and invoke the callback
133131
const args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
134132
const cb = arguments[arguments.length - 1];
135133
func.apply(this, args).then(function(result) {
136134
cb(null, result);
137-
}, function stackCapture(err) {
138-
cb(errors.transformErr(err, stackCapture));
139-
});
135+
}, cb);
140136
};
141137
if (func.name) {
142138
Object.defineProperty(wrapper, 'name', { value: func.name });
143139
}
144140
return wrapper;
145141
}
146142

147-
// The serialize function is used to wrap methods to ensure that the connection
148-
// is not used concurrently by multiple threads
149-
function serialize(func) {
150-
return async function() {
143+
// the wrapFn() function is used to wrap a single method to ensure that calls
144+
// are serialized and that concurrent calls are prevented where that is needed;
145+
// this method also ensures that error information is captured correctly
146+
function wrapFn(func, serialize, preventConcurrentErrorCode) {
147+
const wrapper = async function wrapper() {
151148

152149
let connImpl;
153150

151+
// if concurrent operations are to be prevented, check for that now
152+
if (preventConcurrentErrorCode) {
153+
if (this._isActive)
154+
errors.throwErr(preventConcurrentErrorCode);
155+
this._isActive = true;
156+
}
157+
154158
// determine the connection implementation associated with the object, if
155159
// one currently exists and acquire the "lock"; this simply checks to see
156160
// if another operation is in progress, and if so, waits for it to complete
157-
if (this._impl) {
161+
if (serialize && this._impl) {
158162
connImpl = this._impl._getConnImpl();
159163
await connImpl._acquireLock();
160164
}
@@ -164,46 +168,43 @@ function serialize(func) {
164168
// if a connection implementation is currently associated with this object
165169
try {
166170
return await func.apply(this, arguments);
171+
} catch (err) {
172+
throw errors.transformErr(err, wrapper);
167173
} finally {
168174
if (connImpl)
169175
connImpl._releaseLock();
176+
if (preventConcurrentErrorCode) {
177+
this._isActive = false;
178+
}
170179
}
171-
};
172-
}
173180

174-
function preventConcurrent(func, errorCode) {
175-
return async function() {
176-
if (this._isActive)
177-
errors.throwErr(errorCode);
178-
this._isActive = true;
179-
try {
180-
return await func.apply(this, arguments);
181-
} finally {
182-
this._isActive = false;
183-
}
184181
};
182+
if (func.name) {
183+
Object.defineProperty(wrapper, 'name', { value: func.name });
184+
}
185+
return wrapper;
185186
}
186187

187188
// The wrapFns() function is used to wrap the named methods on the prototype
188-
// in multiple ways (serialize, preventConcurrent and callbackify); the
189+
// so that a number of common tasks can be done in a single place; the
189190
// arguments following the formal arguments contain the names of methods to
190191
// wrap on the prototype; if the first extra argument is an error code, it is
191192
// used to wrap to prevent concurrent access
192193
function wrapFns(proto) {
193194
let nameIndex = 1;
195+
let serialize = true;
194196
let preventConcurrentErrorCode;
195197
if (typeof arguments[1] === 'number') {
196-
nameIndex = 2;
198+
nameIndex++;
197199
preventConcurrentErrorCode = arguments[1];
200+
} else if (typeof arguments[1] === 'boolean') {
201+
nameIndex++;
202+
serialize = arguments[1];
198203
}
199204
for (let i = nameIndex; i < arguments.length; i++) {
200205
const name = arguments[i];
201206
const f = proto[name];
202-
if (preventConcurrentErrorCode) {
203-
proto[name] = callbackify(preventConcurrent(serialize(f),
204-
preventConcurrentErrorCode));
205-
} else
206-
proto[name] = callbackify(serialize(f));
207+
proto[name] = callbackify(wrapFn(f, serialize, preventConcurrentErrorCode));
207208
}
208209
}
209210

@@ -436,8 +437,7 @@ module.exports = {
436437
isXid,
437438
normalizeXid,
438439
makeDate,
439-
preventConcurrent,
440-
serialize,
441440
verifySodaDoc,
441+
wrapFn,
442442
wrapFns
443443
};

0 commit comments

Comments
 (0)