chore: Implement short-lived redis cache for slots (#22787)
* chore: Implement short-lived redis cache for slots * chore: adapt apiv2 redis service to match with upstash redis * chore: safer redis service and ms ttl * fixup! chore: safer redis service and ms ttl * Wrap with timeout, currently doesn't work yet * Updated @upstash/redis for better signal support * Fix type errors, remove ts value * Inject NoopRedisService for NODE_ENV test * chore: bump platform libs * chore: bump platform libs * Upstash Redis upgrade no longer resulted in expected hard crash on init, so updated factory and our Upstash Redis Adapter to mimick old behaviour * Add SLOTS_CACHE_TTL variable for configurable ttl on slots cache * Update parseInt to use right types * chore: bump platform libs * chore: bump platform libs * chore: bump platform libs * update e2e api v2 action * set SLOTS_CACHE_TTL env var api v2 e2e --------- Co-authored-by: cal.com <[email protected]>
This commit is contained in:
1 parent
a71d949a0c
commit
b71d8baccc
19 files changed
+276
-41
No files matched your search
@@ -8,6 +8,7 @@ import { orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains";
|
||||
import { checkForConflicts } from "@calcom/features/bookings/lib/conflictChecker/checkForConflicts";
|
||||
import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled";
|
||||
import type { CacheService } from "@calcom/features/calendar-cache/lib/getShouldServeCache";
|
||||
import type { IRedisService } from "@calcom/features/redis/IRedisService";
|
||||
import { findQualifiedHostsWithDelegationCredentials } from "@calcom/lib/bookings/findQualifiedHostsWithDelegationCredentials";
|
||||
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
|
||||
import { RESERVED_SUBDOMAINS } from "@calcom/lib/constants";
|
||||
@@ -64,6 +65,7 @@ import { handleNotificationWhenNoSlots } from "./handleNotificationWhenNoSlots";
|
||||
import type { GetScheduleOptions } from "./types";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[slots/util]"] });
|
||||
const DEFAULT_SLOTS_CACHE_TTL = 2000;
|
||||
|
||||
type GetAvailabilityUserWithDelegationCredentials = Omit<GetAvailabilityUser, "credentials"> & {
|
||||
credentials: CredentialForCalendarService[];
|
||||
@@ -101,7 +103,47 @@ export interface IAvailableSlotsService {
|
||||
routingFormResponseRepo: RoutingFormResponseRepository;
|
||||
cacheService: CacheService;
|
||||
checkBookingLimitsService: CheckBookingLimitsService;
|
||||
userAvailabilityService: UserAvailabilityService
|
||||
userAvailabilityService: UserAvailabilityService;
|
||||
redisClient: IRedisService;
|
||||
}
|
||||
|
||||
function withSlotsCache(
|
||||
redisClient: IRedisService,
|
||||
func: (args: GetScheduleOptions) => Promise<IGetAvailableSlots>
|
||||
) {
|
||||
return async (args: GetScheduleOptions): Promise<IGetAvailableSlots> => {
|
||||
const cacheKey = `${JSON.stringify(args.input)}`;
|
||||
let success = false;
|
||||
let cachedResult: IGetAvailableSlots | null = null;
|
||||
const startTime = process.hrtime();
|
||||
try {
|
||||
cachedResult = await redisClient.get(cacheKey);
|
||||
success = true;
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof Error && err.name === "TimeoutError") {
|
||||
const endTime = process.hrtime(startTime);
|
||||
log.error(`Redis request timed out after ${endTime[0]}${endTime[1] / 1e6}ms`);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
// If the cache request fails, we proceed to call the function directly
|
||||
return await func(args);
|
||||
}
|
||||
if (cachedResult) {
|
||||
log.info("[CACHE HIT] Available slots", { cacheKey });
|
||||
return cachedResult;
|
||||
}
|
||||
const result = await func(args);
|
||||
const ttl = parseInt(process.env.SLOTS_CACHE_TTL ?? "", 10) || DEFAULT_SLOTS_CACHE_TTL;
|
||||
// we do not wait for the cache to complete setting; we fire and forget, and hope it'll finish.
|
||||
// this is to already start responding to the client.
|
||||
redisClient.set(cacheKey, result, { ttl });
|
||||
log.info("[CACHE MISS] Available slots", { cacheKey, ttl });
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
export class AvailableSlotsService {
|
||||
@@ -884,7 +926,10 @@ export class AvailableSlotsService {
|
||||
"getRegularOrDynamicEventType"
|
||||
);
|
||||
|
||||
getAvailableSlots = withReporting(this._getAvailableSlots.bind(this), "getAvailableSlots");
|
||||
getAvailableSlots = withReporting(
|
||||
withSlotsCache(this.dependencies.redisClient, this._getAvailableSlots.bind(this)),
|
||||
"getAvailableSlots"
|
||||
);
|
||||
|
||||
async _getAvailableSlots({ input, ctx }: GetScheduleOptions): Promise<IGetAvailableSlots> {
|
||||
const {
|
||||
|
||||
Reference in new issue
Block a user