Skip to content

Commit

Permalink
Move config from constants to env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Jan 23, 2025
1 parent edbb5dd commit 41014e2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
16 changes: 16 additions & 0 deletions plugin-server/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ export function getDefaultConfig(): PluginsServerConfig {
// Cookieless
COOKIELESS_FORCE_STATELESS_MODE: false,
COOKIELESS_DISABLED: false,
COOKIELESS_DELETE_EXPIRED_LOCAL_SALTS_INTERVAL_MS: 60 * 60 * 1000, // 1 hour
COOKIELESS_SESSION_TTL_SECONDS: 60 * 60 * 24, // 24 hours
COOKIELESS_SALT_TTL_SECONDS: 60 * 60 * 24, // 24 hours
COOKIELESS_SESSION_INACTIVITY_MS: 30 * 60 * 1000, // 30 minutes
COOKIELESS_IDENTIFIES_TTL_SECONDS:
(24 + // max supported ingestion lag
12 + // max negative timezone in the world*/
14 + // max positive timezone in the world */
24) * // amount of time salt is valid in one timezone
60 *
60,
}
}

Expand Down Expand Up @@ -322,5 +333,10 @@ export const createCookielessConfig = (config: PluginsServerConfig): CookielessC
return {
disabled: config.COOKIELESS_DISABLED,
forceStatelessMode: config.COOKIELESS_FORCE_STATELESS_MODE,
deleteExpiredLocalSaltsIntervalMs: config.COOKIELESS_DELETE_EXPIRED_LOCAL_SALTS_INTERVAL_MS,
sessionTtlSeconds: config.COOKIELESS_SESSION_TTL_SECONDS,
saltTtlSeconds: config.COOKIELESS_SALT_TTL_SECONDS,
sessionInactivityMs: config.COOKIELESS_SESSION_INACTIVITY_MS,
identifiesTtlSeconds: config.COOKIELESS_IDENTIFIES_TTL_SECONDS,
}
}
10 changes: 10 additions & 0 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ export interface PluginsServerConfig extends CdpConfig, IngestionConsumerConfig
// cookieless
COOKIELESS_DISABLED: boolean
COOKIELESS_FORCE_STATELESS_MODE: boolean
COOKIELESS_DELETE_EXPIRED_LOCAL_SALTS_INTERVAL_MS: number
COOKIELESS_SESSION_TTL_SECONDS: number
COOKIELESS_SALT_TTL_SECONDS: number
COOKIELESS_SESSION_INACTIVITY_MS: number
COOKIELESS_IDENTIFIES_TTL_SECONDS: number
}

export interface Hub extends PluginsServerConfig {
Expand Down Expand Up @@ -1360,4 +1365,9 @@ export interface ModelRow {
export interface CookielessConfig {
disabled: boolean
forceStatelessMode: boolean
deleteExpiredLocalSaltsIntervalMs: number
identifiesTtlSeconds: number
sessionTtlSeconds: number
saltTtlSeconds: number
sessionInactivityMs: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ const TIMEZONE_FALLBACK = 'UTC'
export const COOKIELESS_SENTINEL_VALUE = '$posthog_cookieless'
export const COOKIELESS_MODE_FLAG_PROPERTY = '$cookieless_mode'
export const COOKIELESS_EXTRA_HASH_CONTENTS_PROPERTY = '$cookieless_extra'
const MAX_NEGATIVE_TIMEZONE_HOURS = 12
const MAX_POSITIVE_TIMEZONE_HOURS = 14
const MAX_INGESTION_LAG_HOURS = 24
const SALT_TTL_SECONDS =
(MAX_POSITIVE_TIMEZONE_HOURS + MAX_NEGATIVE_TIMEZONE_HOURS + MAX_INGESTION_LAG_HOURS + 24) * 60 * 60
const SESSION_TTL_SECONDS = 60 * 60 * 24
const IDENTIFIES_TTL_SECONDS = 60 * 60 * 24
const DELETE_EXPIRED_SALTS_INTERVAL_MS = 60 * 60 * 1000

export class CookielessSaltManager {
private readonly db: DB
Expand All @@ -90,7 +82,7 @@ export class CookielessSaltManager {
// Periodically delete expired salts from the local cache. Note that this doesn't delete them from redis, but
// that's handled by using redis TTLs. Deleting these salts is what allows us to use the hash of PII data in a
// non PII way. Of course, these are also deleted when the node process restarts.
this.cleanupInterval = setInterval(this.deleteExpiredLocalSalts, DELETE_EXPIRED_SALTS_INTERVAL_MS)
this.cleanupInterval = setInterval(this.deleteExpiredLocalSalts, config.deleteExpiredLocalSaltsIntervalMs)
// Call unref on the timer object, so that it doesn't prevent node from exiting.
this.cleanupInterval.unref()
}
Expand Down Expand Up @@ -131,7 +123,7 @@ export class CookielessSaltManager {
`cookieless_salt:${yyyymmdd}`,
uint32ArrayLEToBase64String(newSaltParts),
'cookielessServerHashStep',
SALT_TTL_SECONDS
this.config.saltTtlSeconds
)
if (setResult === 'OK') {
this.localSaltMap[yyyymmdd] = newSaltParts
Expand Down Expand Up @@ -228,6 +220,8 @@ async function cookielessServerHashStepInner(
hub: Hub,
event: PluginEvent & { properties: Properties }
): Promise<[PluginEvent | undefined]> {
const config = hub.cookielessConfig

// if the team isn't allowed to use this mode, drop the event
const team = await hub.teamManager.getTeamForEvent(event)
if (!team?.cookieless_server_hash_mode) {
Expand Down Expand Up @@ -372,7 +366,7 @@ async function cookielessServerHashStepInner(
// identify event, so the anon_distinct_id must be the sentinel and needs to be replaced

// add this identify event id to redis
const numIdentifies = await hub.db.redisSAddAndSCard(identifiesRedisKey, event.uuid, IDENTIFIES_TTL_SECONDS)
const numIdentifies = await hub.db.redisSAddAndSCard(identifiesRedisKey, event.uuid)

// we want the number of identifies that happened before this one
hashValue = await doHash(hub, {
Expand Down Expand Up @@ -427,22 +421,22 @@ async function cookielessServerHashStepInner(
let sessionState = sessionInfoBuffer ? bufferToSessionState(sessionInfoBuffer) : undefined

// if not, or the TTL has expired, create a new one. Don't rely on redis TTL, as ingestion lag could approach the 30-minute session inactivity timeout
if (!sessionState || timestampMs - sessionState.lastActivityTimestamp > 60 * 30 * 1000) {
if (!sessionState || timestampMs - sessionState.lastActivityTimestamp > config.sessionInactivityMs) {
const sessionId = new UUID7(timestampMs)
sessionState = { sessionId: sessionId, lastActivityTimestamp: timestampMs }
await hub.db.redisSetBuffer(
sessionRedisKey,
sessionStateToBuffer(sessionState),
'cookielessServerHashStep',
SESSION_TTL_SECONDS
config.sessionTtlSeconds
)
} else {
// otherwise, update the timestamp
await hub.db.redisSetBuffer(
sessionRedisKey,
sessionStateToBuffer({ sessionId: sessionState.sessionId, lastActivityTimestamp: timestampMs }),
'cookielessServerHashStep',
SESSION_TTL_SECONDS
config.sessionTtlSeconds
)
}

Expand Down

0 comments on commit 41014e2

Please sign in to comment.