-
Notifications
You must be signed in to change notification settings - Fork 137
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
feat: different default and max idle period #1558
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ import { isArray, isNumber, isUndefined } from './utils/type-utils' | |
import { logger } from './utils/logger' | ||
|
||
import { clampToRange } from './utils/number-utils' | ||
import { PostHog } from './posthog-core' | ||
|
||
const MAX_SESSION_IDLE_TIMEOUT = 30 * 60 // 30 minutes | ||
const MIN_SESSION_IDLE_TIMEOUT = 60 // 1 minute | ||
const SESSION_LENGTH_LIMIT = 24 * 3600 * 1000 // 24 hours | ||
export const DEFAULT_SESSION_IDLE_TIMEOUT_SECONDS = 30 * 60 // 30 minutes | ||
export const MAX_SESSION_IDLE_TIMEOUT_SECONDS = 10 * 60 * 60 // 10 hours | ||
const MIN_SESSION_IDLE_TIMEOUT_SECONDS = 60 // 1 minute | ||
const SESSION_LENGTH_LIMIT_MILLISECONDS = 24 * 3600 * 1000 // 24 hours | ||
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed these since they have different units There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. picked ten hours as the new max but 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine w/ me, the sessions table queries assume 24 hours and some queries might break with sessions longer than that, but anything up to and including 24 hours is fine. |
||
|
||
export class SessionIdManager { | ||
private readonly _sessionIdGenerator: () => string | ||
|
@@ -29,32 +31,34 @@ export class SessionIdManager { | |
private _sessionIdChangedHandlers: SessionIdChangedCallback[] = [] | ||
private readonly _sessionTimeoutMs: number | ||
|
||
constructor( | ||
config: Partial<PostHogConfig>, | ||
persistence: PostHogPersistence, | ||
sessionIdGenerator?: () => string, | ||
windowIdGenerator?: () => string | ||
) { | ||
this.config = config | ||
this.persistence = persistence | ||
constructor(instance: PostHog, sessionIdGenerator?: () => string, windowIdGenerator?: () => string) { | ||
if (!instance.persistence) { | ||
throw new Error('SessionIdManager requires a PostHogPersistence instance') | ||
} | ||
|
||
this.config = instance.config | ||
this.persistence = instance.persistence | ||
this._windowId = undefined | ||
this._sessionId = undefined | ||
this._sessionStartTimestamp = null | ||
this._sessionActivityTimestamp = null | ||
this._sessionIdGenerator = sessionIdGenerator || uuidv7 | ||
this._windowIdGenerator = windowIdGenerator || uuidv7 | ||
|
||
const persistenceName = config['persistence_name'] || config['token'] | ||
const persistenceName = this.config['persistence_name'] || this.config['token'] | ||
|
||
const desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT | ||
const desiredTimeout = this.config['session_idle_timeout_seconds'] || DEFAULT_SESSION_IDLE_TIMEOUT_SECONDS | ||
this._sessionTimeoutMs = | ||
clampToRange( | ||
desiredTimeout, | ||
MIN_SESSION_IDLE_TIMEOUT, | ||
MAX_SESSION_IDLE_TIMEOUT, | ||
'session_idle_timeout_seconds' | ||
MIN_SESSION_IDLE_TIMEOUT_SECONDS, | ||
MAX_SESSION_IDLE_TIMEOUT_SECONDS, | ||
'session_idle_timeout_seconds', | ||
DEFAULT_SESSION_IDLE_TIMEOUT_SECONDS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now the so,
|
||
) * 1000 | ||
|
||
instance.register({ $configured_session_timeout_ms: this._sessionTimeoutMs }) | ||
|
||
this._window_id_storage_key = 'ph_' + persistenceName + '_window_id' | ||
this._primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists' | ||
|
||
|
@@ -218,7 +222,7 @@ export class SessionIdManager { | |
const sessionPastMaximumLength = | ||
isNumber(startTimestamp) && | ||
startTimestamp > 0 && | ||
Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT | ||
Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT_MILLISECONDS | ||
|
||
let valuesChanged = false | ||
const noSessionId = !sessionId | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chose to pass in the posthog instance rather than pass in
this.register