From 4e35498be7cda07932d28ab27af827f04c2806e7 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Sat, 6 Dec 2025 09:22:24 -0800 Subject: [PATCH] Switch to using in-memory shuttle and ETA repositories This is to improve performance until Redis is migrated to the same memory space as the server. --- src/entities/InterchangeSystem.ts | 37 ++++++++++++------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/entities/InterchangeSystem.ts b/src/entities/InterchangeSystem.ts index c0f8383..3bd7e3f 100644 --- a/src/entities/InterchangeSystem.ts +++ b/src/entities/InterchangeSystem.ts @@ -14,16 +14,11 @@ import { ParkingRepositoryLoaderBuilderArguments } from "../loaders/parking/buildParkingRepositoryLoaderIfExists"; import { RedisParkingRepository } from "../repositories/parking/RedisParkingRepository"; -import { RedisShuttleRepository } from "../repositories/shuttle/RedisShuttleRepository"; import { ShuttleGetterRepository } from "../repositories/shuttle/ShuttleGetterRepository"; import { InMemoryExternalSourceETARepository } from "../repositories/shuttle/eta/InMemoryExternalSourceETARepository"; import { ETAGetterRepository } from "../repositories/shuttle/eta/ETAGetterRepository"; -import { RedisSelfUpdatingETARepository } from "../repositories/shuttle/eta/RedisSelfUpdatingETARepository"; -import { RedisExternalSourceETARepository } from "../repositories/shuttle/eta/RedisExternalSourceETARepository"; import { InMemorySelfUpdatingETARepository } from "../repositories/shuttle/eta/InMemorySelfUpdatingETARepository"; -import { BaseRedisETARepository } from "../repositories/shuttle/eta/BaseRedisETARepository"; import { BaseInMemoryETARepository } from "../repositories/shuttle/eta/BaseInMemoryETARepository"; -import createRedisClientForRepository from "../helpers/createRedisClientForRepository"; export interface InterchangeSystemBuilderArguments { name: string; @@ -78,7 +73,7 @@ export class InterchangeSystem { static async build( args: InterchangeSystemBuilderArguments, ) { - const { shuttleRepository, timedShuttleDataLoader, etaRepository } = await InterchangeSystem.buildRedisShuttleLoaderAndRepositories(args); + const { shuttleRepository, timedShuttleDataLoader, etaRepository } = await InterchangeSystem.buildProductionShuttleLoaderAndRepositories(args); timedShuttleDataLoader.start(); const { notificationScheduler, notificationRepository } = await InterchangeSystem.buildNotificationSchedulerAndRepository( @@ -104,34 +99,30 @@ export class InterchangeSystem { ); } - private static async buildRedisShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { - const shuttleRepository = new RedisShuttleRepository( - createRedisClientForRepository(), + private static async buildProductionShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { + const shuttleRepository = new UnoptimizedInMemoryShuttleRepository( args.shuttleStopArrivalDegreeDelta, ); - await shuttleRepository.connect(); - let etaRepository: BaseRedisETARepository; + let etaRepository: BaseInMemoryETARepository; let shuttleDataLoader: ApiBasedShuttleRepositoryLoader; if (args.useSelfUpdatingEtas) { - etaRepository = new RedisSelfUpdatingETARepository(shuttleRepository); - (etaRepository as RedisSelfUpdatingETARepository).startListeningForUpdates(); + etaRepository = new InMemorySelfUpdatingETARepository(shuttleRepository); + (etaRepository as InMemorySelfUpdatingETARepository).startListeningForUpdates(); shuttleDataLoader = new ApiBasedShuttleRepositoryLoader( args.passioSystemId, args.id, shuttleRepository, ); } else { - etaRepository = new RedisExternalSourceETARepository(); + etaRepository = new InMemoryExternalSourceETARepository(); shuttleDataLoader = new ApiBasedShuttleRepositoryLoader( args.passioSystemId, args.id, shuttleRepository, - etaRepository as RedisExternalSourceETARepository, + etaRepository as InMemoryExternalSourceETARepository, ); } - await etaRepository.connect(); - const timedShuttleDataLoader = new TimedApiBasedRepositoryLoader( shuttleDataLoader, ); @@ -191,17 +182,17 @@ export class InterchangeSystem { static buildForTesting( args: InterchangeSystemBuilderArguments, ) { - const { shuttleRepository, timedShuttleLoader, etaRepository } = InterchangeSystem.buildInMemoryShuttleLoaderAndRepositories(args); + const { shuttleRepository, timedShuttleLoader, etaRepository } = InterchangeSystem.buildTestingShuttleLoaderAndRepositories(args); // Timed shuttle loader is not started here - const { notificationScheduler, notificationRepository } = InterchangeSystem.buildInMemoryNotificationSchedulerAndRepository( + const { notificationScheduler, notificationRepository } = InterchangeSystem.buildTestingNotificationSchedulerAndRepository( etaRepository, shuttleRepository, args ); notificationScheduler.startListeningForUpdates(); - let { parkingRepository, timedParkingLoader } = this.buildInMemoryParkingLoaderAndRepository(args.parkingSystemId); + let { parkingRepository, timedParkingLoader } = this.buildTestingParkingLoaderAndRepository(args.parkingSystemId); // Timed parking loader is not started here return new InterchangeSystem( @@ -217,7 +208,7 @@ export class InterchangeSystem { ); } - private static buildInMemoryNotificationSchedulerAndRepository( + private static buildTestingNotificationSchedulerAndRepository( etaRepository: ETAGetterRepository, shuttleRepository: UnoptimizedInMemoryShuttleRepository, args: InterchangeSystemBuilderArguments @@ -233,7 +224,7 @@ export class InterchangeSystem { return { notificationScheduler, notificationRepository }; } - private static buildInMemoryParkingLoaderAndRepository(id?: string) { + private static buildTestingParkingLoaderAndRepository(id?: string) { if (id === undefined) { return { parkingRepository: null, timedParkingLoader: null }; } @@ -256,7 +247,7 @@ export class InterchangeSystem { return { parkingRepository, timedParkingLoader }; } - private static buildInMemoryShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { + private static buildTestingShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { const shuttleRepository = new UnoptimizedInMemoryShuttleRepository( args.shuttleStopArrivalDegreeDelta, );