chore(buffer): iron out the buffer issues

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-19 21:38:40 +02:00
parent e18ac4d79e
commit 25f329a4cd
11 changed files with 164 additions and 54 deletions

View File

@@ -43,8 +43,8 @@ export async function postEvent(
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'EX',
5,
'PX',
950, // a bit under the delay below
'NX',
);
@@ -72,7 +72,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' ? 0 : 1000,
delay: request.body.name === 'screen_view' ? undefined : 1000,
},
);

View File

@@ -201,8 +201,8 @@ async function track({
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'EX',
5,
'PX',
950, // a bit under the delay below
'NX',
);
@@ -228,7 +228,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: payload.name === 'screen_view' ? 0 : 1000,
delay: payload.name === 'screen_view' ? undefined : 1000,
},
);
}

View File

@@ -39,17 +39,22 @@ export async function parseIp(ip?: string): Promise<GeoLocation> {
}
try {
const geo = await fetch(`${process.env.GEO_IP_HOST}/${ip}`, {
const res = await fetch(`${process.env.GEO_IP_HOST}/${ip}`, {
signal: AbortSignal.timeout(2000),
});
const res = (await geo.json()) as RemoteIpLookupResponse;
if (!res.ok) {
return geo;
}
const json = (await res.json()) as RemoteIpLookupResponse;
return {
country: res.country,
city: res.city,
region: res.stateprov,
longitude: res.longitude,
latitude: res.latitude,
country: json.country,
city: json.city,
region: json.stateprov,
longitude: json.longitude,
latitude: json.latitude,
};
} catch (error) {
logger.error('Failed to fetch geo location for ip', { error });

View File

@@ -9,7 +9,9 @@ export function parseUserAgent(ua?: string | null) {
if (!ua) return parsedServerUa;
const res = new UAParser(ua).getResult();
if (isServer(ua)) return parsedServerUa;
if (isServer(ua)) {
return parsedServerUa;
}
return {
os: res.os.name,
@@ -77,7 +79,9 @@ function isServer(userAgent: string) {
return true;
}
return !!userAgent.match(/^([^\s]+\/[\d.]+\s*)+$/);
// Matches user agents like "Go-http-client/1.0" or "Go Http Client/1.0"
// It should just match the first name (with optional spaces) and version
return !!userAgent.match(/^[^\/]+\/[\d.]+$/);
}
export function getDevice(ua: string) {