Skip to content

Commit

Permalink
fix: catch mutation throttling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Nov 26, 2024
1 parent f71a829 commit 62918fd
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/extensions/replay/mutation-rate-limiter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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'
import { logger } from '../../utils/logger'

export class MutationRateLimiter {
private bucketSize = 100
Expand Down Expand Up @@ -75,39 +76,44 @@ export class MutationRateLimiter {
return event
}

const data = event.data as Partial<mutationCallbackParam>
const initialMutationCount = this.numberOfChanges(data)
try {
const data = event.data as Partial<mutationCallbackParam>
const initialMutationCount = this.numberOfChanges(data)

if (data.attributes) {
// Most problematic mutations come from attrs where the style or minor properties are changed rapidly
data.attributes = data.attributes.filter((attr) => {
const [nodeId, node] = this.getNodeOrRelevantParent(attr.id)
if (data.attributes) {
// Most problematic mutations come from attrs where the style or minor properties are changed rapidly
data.attributes = data.attributes.filter((attr) => {
const [nodeId, node] = this.getNodeOrRelevantParent(attr.id)

if (this.mutationBuckets[nodeId] === 0) {
return false
}
if (this.mutationBuckets[nodeId] === 0) {
return false
}

this.mutationBuckets[nodeId] = this.mutationBuckets[nodeId] ?? this.bucketSize
this.mutationBuckets[nodeId] = Math.max(this.mutationBuckets[nodeId] - 1, 0)
this.mutationBuckets[nodeId] = this.mutationBuckets[nodeId] ?? this.bucketSize
this.mutationBuckets[nodeId] = Math.max(this.mutationBuckets[nodeId] - 1, 0)

if (this.mutationBuckets[nodeId] === 0) {
if (!this.loggedTracker[nodeId]) {
this.loggedTracker[nodeId] = true
this.options.onBlockedNode?.(nodeId, node)
if (this.mutationBuckets[nodeId] === 0) {
if (!this.loggedTracker[nodeId]) {
this.loggedTracker[nodeId] = true
this.options.onBlockedNode?.(nodeId, node)
}
}
}

return attr
})
}
return attr
})
}

// Check if every part of the mutation is empty in which case there is nothing to do
const mutationCount = this.numberOfChanges(data)
// Check if every part of the mutation is empty in which case there is nothing to do
const mutationCount = this.numberOfChanges(data)

if (mutationCount === 0 && initialMutationCount !== mutationCount) {
// If we have modified the mutation count and the remaining count is 0, then we don't need the event.
return
if (mutationCount === 0 && initialMutationCount !== mutationCount) {
// If we have modified the mutation count and the remaining count is 0, then we don't need the event.
return
}
return event
} catch (e) {
logger.warn('error throttling mutations', e)
return event
}
return event
}
}

0 comments on commit 62918fd

Please sign in to comment.