Skip to content

Commit

Permalink
Avoid starting a session if one already exists
Browse files Browse the repository at this point in the history
This change prevents multiple sessions from being started if separate
plugins both call 'startSession' by making 'resumeSession' do nothing
if a session already exists
  • Loading branch information
imjoehaines committed Mar 4, 2021
1 parent fdf1583 commit bb14fad
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
12 changes: 10 additions & 2 deletions packages/plugin-browser-session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ const sessionDelegate = {
return sessionClient
},
resumeSession: (client) => {
// Do nothing if there's already an active session
if (client._session) {
return client
}

// If we have a paused session then make it the active session
if (client._pausedSession) {
client._session = client._pausedSession
client._pausedSession = null

return client
} else {
return client.startSession()
}

// Otherwise start a new session
return client.startSession()
},
pauseSession: (client) => {
client._pausedSession = client._session
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-express/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
const dom = domain.create()

// Get a client to be scoped to this request. If sessions are enabled, use the
// startSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.startSession() : clone(client)
// resumeSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.resumeSession() : clone(client)

// attach it to the request
req.bugsnag = requestClient
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-koa/src/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = {
load: client => {
const requestHandler = async (ctx, next) => {
// Get a client to be scoped to this request. If sessions are enabled, use the
// startSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.startSession() : clone(client)
// resumeSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.resumeSession() : clone(client)

ctx.bugsnag = requestClient

Expand Down Expand Up @@ -47,8 +47,8 @@ module.exports = {

requestHandler.v1 = function * (next) {
// Get a client to be scoped to this request. If sessions are enabled, use the
// startSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.startSession() : clone(client)
// resumeSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.resumeSession() : clone(client)

this.bugsnag = requestClient

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('plugin: koa', () => {
c._sessionDelegate = {
startSession: () => c,
pauseSession: () => {},
resumeSession: () => {}
resumeSession: () => c
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const middleware = c.getPlugin('koa')!
Expand All @@ -23,7 +23,7 @@ describe('plugin: koa', () => {
c._sessionDelegate = {
startSession: () => c,
pauseSession: () => {},
resumeSession: () => {}
resumeSession: () => c
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-restify/src/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = {
const dom = domain.create()

// Get a client to be scoped to this request. If sessions are enabled, use the
// startSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.startSession() : clone(client)
// resumeSession() call to get a session client, otherwise, clone the existing client.
const requestClient = client._config.autoTrackSessions ? client.resumeSession() : clone(client)

// attach it to the request
req.bugsnag = requestClient
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-server-session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ module.exports = {
client._session = null
},
resumeSession: (client) => {
// Do nothing if there's already an active session
if (client._session) {
return client
}

// If we have a paused session then make it the active session
if (client._pausedSession) {
client._session = client._pausedSession
client._pausedSession = null

return client
} else {
return client.startSession()
}

// Otherwise start a new session
return client.startSession()
}
}
},
Expand Down

0 comments on commit bb14fad

Please sign in to comment.