Skip to content

Commit

Permalink
feat: allow mutation rate limiter configuration (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Oct 31, 2024
1 parent 97acfec commit 58d2e00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/extensions/replay/mutation-rate-limiter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { eventWithTime, mutationCallbackParam } from '@rrweb/types'
import { INCREMENTAL_SNAPSHOT_EVENT_TYPE, MUTATION_SOURCE_TYPE, rrwebRecord } from './sessionrecording-utils'
import { clampToRange } from '../../utils/number-utils'

export class MutationRateLimiter {
private bucketSize = 100
Expand All @@ -15,8 +16,18 @@ export class MutationRateLimiter {
onBlockedNode?: (id: number, node: Node | null) => void
} = {}
) {
this.refillRate = this.options.refillRate ?? this.refillRate
this.bucketSize = this.options.bucketSize ?? this.bucketSize
this.refillRate = clampToRange(
this.options.refillRate ?? this.refillRate,
0,
100,
'mutation throttling refill rate'
)
this.bucketSize = clampToRange(
this.options.bucketSize ?? this.bucketSize,
0,
100,
'mutation throttling bucket size'
)
setInterval(() => {
this.refillBuckets()
}, 1000)
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,8 @@ export class SessionRecording {
this.mutationRateLimiter =
this.mutationRateLimiter ??
new MutationRateLimiter(this.rrwebRecord, {
refillRate: this.instance.config.session_recording.__mutationRateLimiterRefillRate,
bucketSize: this.instance.config.session_recording.__mutationRateLimiterBucketSize,
onBlockedNode: (id, node) => {
const message = `Too many mutations on node '${id}'. Rate limiting. This could be due to SVG animations or something similar`
logger.info(message, {
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ export interface SessionRecordingOptions {
Default is 5 minutes.
*/
session_idle_threshold_ms?: number
/*
ADVANCED: alters the refill rate for the token bucket mutation throttling
Normally only altered alongside posthog support guidance.
Accepts values between 0 and 100
Default is 10.
*/
__mutationRateLimiterRefillRate?: number
/*
ADVANCED: alters the bucket size for the token bucket mutation throttling
Normally only altered alongside posthog support guidance.
Accepts values between 0 and 100
Default is 100.
*/
__mutationRateLimiterBucketSize?: number
}

export type SessionIdChangedCallback = (
Expand Down

0 comments on commit 58d2e00

Please sign in to comment.