fix(buffer): ensure we don't have duplicate events

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-27 12:48:43 +01:00
parent 9aa0ee485a
commit 44721f84da
4 changed files with 87 additions and 11 deletions

View File

@@ -41,11 +41,12 @@ export async function postEvent(
const isScreenView = request.body.name === 'screen_view';
// this will ensure that we don't have multiple events creating sessions
const LOCK_DURATION = 1000;
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'PX',
950, // a bit under the delay below
LOCK_DURATION,
'NX',
);
@@ -76,7 +77,7 @@ export async function postEvent(
// Prioritize 'screen_view' events by setting no delay
// This ensures that session starts are created from 'screen_view' events
// rather than other events, maintaining accurate session tracking
delay: request.body.name === 'screen_view' ? undefined : 1000,
delay: isScreenView ? undefined : LOCK_DURATION - 100,
},
);

View File

@@ -229,11 +229,12 @@ async function track({
}) {
const isScreenView = payload.name === 'screen_view';
// this will ensure that we don't have multiple events creating sessions
const LOCK_DURATION = 1000;
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'PX',
950, // a bit under the delay below
LOCK_DURATION,
'NX',
);
@@ -264,7 +265,7 @@ async function track({
// Prioritize 'screen_view' events by setting no delay
// This ensures that session starts are created from 'screen_view' events
// rather than other events, maintaining accurate session tracking
delay: isScreenView ? undefined : 1000,
delay: isScreenView ? undefined : LOCK_DURATION - 100,
},
);
}

View File

@@ -96,10 +96,10 @@ const startServer = async () => {
global: false,
});
fastify.addHook('preHandler', ipHook);
fastify.addHook('preHandler', timestampHook);
fastify.addHook('preHandler', fixHook);
fastify.addHook('onRequest', requestIdHook);
fastify.addHook('onRequest', timestampHook);
fastify.addHook('onRequest', ipHook);
fastify.addHook('onRequest', fixHook);
fastify.addHook('onResponse', requestLoggingHook);
fastify.register(compress, {