Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REMOVE] Bluebird #9

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4ec8285
empty commit
jpg619 Aug 20, 2024
1a7ac42
remove unused Promise dependency from integrate-pg-pool-sequence-test.js
jpg619 Jan 30, 2025
b377da7
remove unused Promise dependency from tx-session-connpair.js
jpg619 Jan 30, 2025
613e522
remove unused Promise dependency from integrate-pummel-leak-test.js
jpg619 Jan 30, 2025
24001e3
replace bluebird Promise.delay with native delay function in basic-at…
jpg619 Jan 30, 2025
7ff89de
replace bluebird Promise.delay with native delay function in basic-se…
jpg619 Jan 30, 2025
bf37d4a
refactor basic-session-error-test.js to use Promise.all instead of Pr…
jpg619 Jan 30, 2025
7cf7882
refactor basic-transaction-error-test.js to use util.promisify and Pr…
jpg619 Jan 30, 2025
c1296b1
replace bluebird Promise.delay with native delay function in basic-tr…
jpg619 Feb 12, 2025
ab845ba
refactor basic-atomic-error-test.js to replace bluebird with native P…
jpg619 Feb 12, 2025
3a8f5a5
refactor db-session.js to replace bluebird with native Promise and im…
jpg619 Feb 12, 2025
fa1ee11
refactor basic-rollback-test.js and basic-transaction-error-test.js t…
jpg619 Feb 12, 2025
31cfea3
refactor integrate-pg-pool-sequence-test.js and integrate-pummel-leak…
jpg619 Feb 13, 2025
75c9b45
refactor db-session.js to replace Promise.try with async/await for im…
jpg619 Feb 21, 2025
4f6cad7
refactor db-session.js to remove bluebird dependency for improved per…
jpg619 Feb 24, 2025
7d806b2
refactor package.json to remove bluebird dependency for improved perf…
jpg619 Feb 24, 2025
c550efc
bump version to 1.5.0 for release
jpg619 Feb 24, 2025
ad70e37
refactor db-session.js to use async functions for atomic and transact…
jpg619 Mar 5, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 63 additions & 57 deletions db-session.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const DOMAIN_TO_SESSION = new WeakMap()
const Promise = require('bluebird')

const TxSessionConnectionPair = require('./lib/tx-session-connpair.js')
const SessionConnectionPair = require('./lib/session-connpair.js')
Expand Down Expand Up @@ -45,19 +44,19 @@ const api = module.exports = {

atomic (operation) {
return function atomic$operation () {
return Promise.try(() => {
const args = [].slice.call(arguments)
return api.session.atomic(operation.bind(this), args)
})
const args = [].slice.call(arguments)
return (async () => {
return await api.session.atomic(operation.bind(this), args)
})()
}
},

transaction (operation) {
return function transaction$operation () {
return Promise.try(() => {
const args = [].slice.call(arguments)
return api.session.transaction(operation.bind(this), args)
})
const args = [].slice.call(arguments)
return (async () => {
return await api.session.transaction(operation.bind(this), args)
})()
}
},

Expand Down Expand Up @@ -140,15 +139,16 @@ class Session {
}, operation, args)

const releasePair = getConnPair.then(pair => {
return getResult.reflect().then(result => {
return getResult.then(result => {
this.metrics.onTransactionFinish(baton, operation, args, result)
return result.isFulfilled()
? pair.release()
: pair.release(result.reason())
return pair.release()
}).catch(reason => {
this.metrics.onTransactionFinish(baton, operation, args, reason)
return pair.release(reason)
})
})

return releasePair.return(getResult)
return releasePair.then(() => getResult)
}

atomic (operation, args) {
Expand Down Expand Up @@ -209,15 +209,16 @@ class TransactionSession {
}, operation, args)

const releasePair = atomicConnPair.then(pair => {
return getResult.reflect().then(result => {
return getResult.then(result => {
this.metrics.onAtomicFinish(baton, operation, args, result)
return result.isFulfilled()
? pair.release()
: pair.release(result.reason())
return pair.release()
}).catch(reason => {
this.metrics.onAtomicFinish(baton, operation, args, reason)
return pair.release(reason)
})
})

return releasePair.return(getResult)
return releasePair.then(() => getResult)
}

// NB: for use in tests _only_!)
Expand All @@ -233,59 +234,64 @@ class AtomicSession extends TransactionSession {
}
}

function Session$RunWrapped (parent,
createSession,
getConnPair,
before,
after,
operation,
args) {
return getConnPair.then(pair => {
function Session$RunWrapped(
parent,
createSession,
getConnPair,
before,
after,
operation,
args
) {
return getConnPair.then((pair) => {
const subdomain = domain.create()
const session = createSession(pair)
parent.metrics.onSubsessionStart(parent, session)
DOMAIN_TO_SESSION.set(subdomain, session)

const runBefore = new Promise((resolve, reject) => {
return pair.connection.query(
before,
err => err ? reject(err) : resolve()
return pair.connection.query(before, (err) =>
err ? reject(err) : resolve()
)
})

return runBefore.then(() => {
const getResult = Promise.resolve(
subdomain.run(() => Promise.try(() => {
return operation.apply(null, args)
}))
subdomain.run(() => {
return Promise.resolve().then(() => {
return operation.apply(null, args)
})
})
)

const reflectedResult = getResult.reflect()
const waitOperation = getResult
.then((result) => {
return Promise.all([
Promise.resolve(result),
Promise.resolve(session.operation),
])
})
.finally(() => {
markInactive(subdomain)
})

const waitOperation = Promise.join(
reflectedResult,
reflectedResult.then(() => session.operation)
)
.finally(markInactive(subdomain))
.return(reflectedResult)

const runCommitStep = waitOperation.then(result => {
return new Promise((resolve, reject) => {
return pair.connection.query(
result.isFulfilled()
? after.success
: after.failure,
err => err ? reject(err) : resolve()
)
const runCommitStep = waitOperation
.then(([result]) => {
return new Promise((resolve, reject) => {
return pair.connection.query(
result ? after.success : after.failure,
(err) => (err ? reject(err) : resolve())
)
})
})
}).then(
() => parent.metrics.onSubsessionFinish(parent, session),
err => {
parent.metrics.onSubsessionFinish(parent, session)
throw err
}
)
return runCommitStep.return(getResult)
.then(
() => parent.metrics.onSubsessionFinish(parent, session),
(err) => {
parent.metrics.onSubsessionFinish(parent, session)
throw err
}
)
return runCommitStep.then(() => getResult)
})
})
}
Expand Down
2 changes: 0 additions & 2 deletions lib/tx-session-connpair.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const Promise = require('bluebird')

const RESOLVE_SYM = Symbol('resolve')
const REJECT_SYM = Symbol('reject')

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@npm/pg-db-session",
"version": "1.4.1",
"version": "1.5.0",
"description": "domain-attached database sessions",
"main": "db-session.js",
"scripts": {
Expand Down Expand Up @@ -29,8 +29,5 @@
"pg": "6.1.6",
"standard": "10.0.2",
"tap": "10.3.2"
},
"dependencies": {
"bluebird": "3.5.0"
}
}
9 changes: 5 additions & 4 deletions test/basic-atomic-concurrency-test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict'

const Promise = require('bluebird')
const test = require('tap').test

const domain = require('../lib/domain.js')
const db = require('../db-session.js')

const LOGS = []

const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

const runOperations = db.transaction(function runOperations (inner) {
return Promise.all(Array.from(Array(4)).map((_, idx) => {
return idx % 2 === 0 ? inner(idx) : db.getConnection().then(connPair => {
LOGS.push(`load ${idx}`)
return Promise.delay(5).then(() => {
return delay(5).then(() => {
LOGS.push(`release ${idx}`)
connPair.release()
})
Expand All @@ -22,10 +23,10 @@ const runOperations = db.transaction(function runOperations (inner) {

function runSubOperation (rootIdx) {
return Promise.all(Array.from(Array(4)).map((_, idx) => {
return Promise.delay(5).then(() => {
return delay(5).then(() => {
return db.getConnection().then(connPair => {
LOGS.push(`load ${rootIdx} ${idx}`)
return Promise.delay(5).then(() => {
return delay(5).then(() => {
LOGS.push(`release ${rootIdx} ${idx}`)
connPair.release()
})
Expand Down
23 changes: 15 additions & 8 deletions test/basic-atomic-error-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const Promise = require('bluebird')
const test = require('tap').test

const domain = require('../lib/domain.js')
Expand All @@ -17,16 +16,24 @@ test('test error in previous query', assert => {
domain1.run(() => {
return db.atomic(() => {
const first = db.getConnection().then(conn => {
return Promise.promisify(conn.connection.query)('ONE')
.then(() => conn.release())
return new Promise((resolve, reject) => {
conn.connection.query('ONE', err => {
if (err) return reject(err)
resolve()
})
}).then(() => conn.release())
.catch(err => conn.release(err))
})

const second = first.then(() => {
return db.getConnection()
}).then(conn => {
return Promise.promisify(conn.connection.query)('TWO')
.then(() => conn.release())
return new Promise((resolve, reject) => {
conn.connection.query('TWO', err => {
if (err) return reject(err)
resolve()
})
}).then(() => conn.release())
.catch(err => conn.release(err))
})

Expand Down Expand Up @@ -141,7 +148,7 @@ test('test error in ROLLBACK: does not reuse connection', assert => {
pair.release()
throw new Error('any kind of error, really')
})
})().reflect()
})().catch(() => {})

const second = db.getConnection().then(pair => {
// with concurrency=1, we will try to re-use
Expand All @@ -151,7 +158,7 @@ test('test error in ROLLBACK: does not reuse connection', assert => {
pair.release()
})

return Promise.join(first, second)
return Promise.all([first, second])
})
.catch(err => assert.fail(err))
.finally(() => domain1.exit())
Expand All @@ -169,4 +176,4 @@ test('test error in ROLLBACK: does not reuse connection', assert => {
}
}
}
})
})
10 changes: 8 additions & 2 deletions test/basic-rollback-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ test('rolling back transaction calls ROLLBACK', assert => {
domain1.run(() => {
return db.transaction(() => {
throw new Error('no thanks')
})().reflect()
})().then(
() => null,
() => null
)
})
.then(() => assert.equal(LOGS.join(' '), 'BEGIN ROLLBACK'))
.catch(err => assert.fail(err))
Expand Down Expand Up @@ -44,7 +47,10 @@ test('rolling back atomic calls ROLLBACK', assert => {
domain1.run(() => {
return db.atomic(() => {
throw new Error('no thanks')
})().reflect()
})().then(
() => null,
() => null
)
})
.then(() => {
assert.equal(LOGS.join('\n').replace(/_[\d_]+$/gm, '_TS'), `
Expand Down
5 changes: 3 additions & 2 deletions test/basic-session-concurrency-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const Promise = require('bluebird')
const test = require('tap').test

const domain = require('../lib/domain.js')
const db = require('../db-session.js')

const LOGS = []

const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

test('test root session concurrency=0', assert => {
const start = process.domain
const domain1 = domain.create()
Expand Down Expand Up @@ -166,7 +167,7 @@ function runOperations () {
return Promise.all(Array.from(Array(8)).map((_, idx) => {
return db.getConnection().then(connPair => {
LOGS.push(`load ${idx}`)
return Promise.delay(5).then(() => {
return delay(5).then(() => {
LOGS.push(`release ${idx}`)
connPair.release()
})
Expand Down
Loading