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

♻️ [RUMF-1083] rework session cookie cache #1180

Merged
merged 7 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 0 additions & 40 deletions packages/core/src/browser/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,6 @@ export interface CookieOptions {
domain?: string
}

export interface CookieCache {
get: () => string | undefined
set: (value: string, expireDelay: number) => void
clearCache: () => void
}

export function cacheCookieAccess(name: string, options: CookieOptions): CookieCache {
let timeout: number
let cache: string | undefined
let hasCache = false

const cacheAccess = () => {
hasCache = true
clearTimeout(timeout)
timeout = setTimeout(() => {
hasCache = false
}, COOKIE_ACCESS_DELAY)
}

return {
get: () => {
if (hasCache) {
return cache
}
cache = getCookie(name)
cacheAccess()
return cache
},
set: (value: string, expireDelay: number) => {
setCookie(name, value, expireDelay, options)
cache = value
cacheAccess()
},
clearCache: () => {
clearTimeout(timeout)
hasCache = false
},
}
}

export function setCookie(name: string, value: string, expireDelay: number, options?: CookieOptions) {
const date = new Date()
date.setTime(date.getTime() + expireDelay)
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/domain/session/oldCookiesMigration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCookie, CookieOptions, cacheCookieAccess } from '../../browser/cookie'
import { persistSession, SessionState, SESSION_COOKIE_NAME } from './sessionStore'
import { getCookie, CookieOptions } from '../../browser/cookie'
import { SessionState, SESSION_COOKIE_NAME, persistSession } from './sessionStore'

export const OLD_SESSION_COOKIE_NAME = '_dd'
export const OLD_RUM_COOKIE_NAME = '_dd_r'
Expand All @@ -14,8 +14,7 @@ export const LOGS_SESSION_KEY = 'logs'
* to allow older sdk versions to be upgraded to newer versions without compatibility issues.
*/
export function tryOldCookiesMigration(options: CookieOptions) {
const sessionCookie = cacheCookieAccess(SESSION_COOKIE_NAME, options)
const sessionString = sessionCookie.get()
const sessionString = getCookie(SESSION_COOKIE_NAME)
const oldSessionId = getCookie(OLD_SESSION_COOKIE_NAME)
const oldRumType = getCookie(OLD_RUM_COOKIE_NAME)
const oldLogsType = getCookie(OLD_LOGS_COOKIE_NAME)
Expand All @@ -30,6 +29,6 @@ export function tryOldCookiesMigration(options: CookieOptions) {
if (oldRumType && /^[012]$/.test(oldRumType)) {
session[RUM_SESSION_KEY] = oldRumType
}
persistSession(session, sessionCookie)
persistSession(session, options)
}
}
44 changes: 1 addition & 43 deletions packages/core/src/domain/session/sessionManagement.spec.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,10 @@
import {
cacheCookieAccess,
COOKIE_ACCESS_DELAY,
CookieCache,
CookieOptions,
getCookie,
setCookie,
} from '../../browser/cookie'
import { COOKIE_ACCESS_DELAY, CookieOptions, getCookie, setCookie } from '../../browser/cookie'
import { Clock, mockClock, restorePageVisibility, setPageVisibility, createNewEvent } from '../../../test/specHelper'
import { ONE_HOUR, DOM_EVENT } from '../../tools/utils'
import { isIE } from '../../tools/browserDetection'
import { Session, startSessionManagement, stopSessionManagement, VISIBILITY_CHECK_DELAY } from './sessionManagement'
import { SESSION_COOKIE_NAME, SESSION_TIME_OUT_DELAY, SESSION_EXPIRATION_DELAY } from './sessionStore'

describe('cacheCookieAccess', () => {
const TEST_COOKIE = 'test'
const TEST_DELAY = 1000
const options: CookieOptions = {}
const DURATION = 123456
let cookieCache: CookieCache
let clock: Clock

beforeEach(() => {
clock = mockClock()
cookieCache = cacheCookieAccess(TEST_COOKIE, options)
})

afterEach(() => clock.cleanup())

it('should keep cookie value in cache', () => {
setCookie(TEST_COOKIE, 'foo', DURATION)
expect(cookieCache.get()).toEqual('foo')

setCookie(TEST_COOKIE, '', DURATION)
expect(cookieCache.get()).toEqual('foo')

clock.tick(TEST_DELAY)
expect(cookieCache.get()).toBeUndefined()
})

it('should invalidate cache when updating the cookie', () => {
setCookie(TEST_COOKIE, 'foo', DURATION)
expect(cookieCache.get()).toEqual('foo')

cookieCache.set('bar', DURATION)
expect(cookieCache.get()).toEqual('bar')
})
})

enum FakeTrackingType {
NOT_TRACKED = 'not-tracked',
TRACKED = 'tracked',
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/domain/session/sessionManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ export interface Session<T> {
getTrackingType: () => T | undefined
}

let stopCallbacks: Array<() => void> = []

export function startSessionManagement<TrackingType extends string>(
options: CookieOptions,
productKey: string,
computeSessionState: (rawTrackingType?: string) => { trackingType: TrackingType; isTracked: boolean }
): Session<TrackingType> {
tryOldCookiesMigration(options)
const sessionStore = startSessionStore(options, productKey, computeSessionState)
stopCallbacks.push(() => sessionStore.stop())
bcaudan marked this conversation as resolved.
Show resolved Hide resolved

sessionStore.expandOrRenewSession()
trackActivity(() => sessionStore.expandOrRenewSession())
trackVisibility(() => sessionStore.expandSession())

return {
getId: () => sessionStore.retrieveSession().id,
getTrackingType: () => sessionStore.retrieveSession()[productKey] as TrackingType | undefined,
getId: () => sessionStore.getSession().id,
getTrackingType: () => sessionStore.getSession()[productKey] as TrackingType | undefined,
renewObservable: sessionStore.renewObservable,
}
}
Expand All @@ -37,8 +40,6 @@ export function stopSessionManagement() {
stopCallbacks = []
}

let stopCallbacks: Array<() => void> = []

function trackActivity(expandOrRenewSession: () => void) {
const { stop } = utils.addEventListeners(
window,
Expand Down
Loading