Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:cache): propagate method errors without retrying
johaven6c07c421c06a
diff --git a/backend/src/infrastructure/cache/cache.decorator.spec.ts b/backend/src/infrastructure/cache/cache.decorator.spec.tsnew file mode 100644index 00000000..9069b2c1--- /dev/null+++ b/backend/src/infrastructure/cache/cache.decorator.spec.ts@@ -0,0 +1,139 @@+import 'reflect-metadata'+import { Logger } from '@nestjs/common'+import { CacheDecorator } from './cache.decorator'+import { Cache } from './cache.service'++interface CacheMock {+ genSlugKey: ReturnType<typeof vi.fn>+ get: ReturnType<typeof vi.fn>+ set: ReturnType<typeof vi.fn>+}++class CachedService {+ readonly original = vi.fn(async (left: number, right: number) => left + right)++ constructor(readonly cache: Cache) {}++ @CacheDecorator(60)+ async compute(left: number, right: number): Promise<number> {+ return this.original(left, right)+ }+}++class RefreshingCachedService {+ readonly original = vi.fn(async (value: number) => value * 2)++ constructor(readonly cache: Cache) {}++ @CacheDecorator(90, true)+ async compute(value: number): Promise<number> {+ return this.original(value)+ }+}++describe(CacheDecorator.name, () => {+ let cache: CacheMock++ beforeEach(() => {+ cache = {+ genSlugKey: vi.fn(() => 'cache-key'),+ get: vi.fn(),+ set: vi.fn().mockResolvedValue(true)+ }+ })++ afterEach(() => {+ vi.restoreAllMocks()+ })++ it('should return a cached value without calling the original method', async () => {+ cache.get.mockResolvedValue(42)+ const service = new CachedService(cache as unknown as Cache)++ await expect(service.compute(1, 2)).resolves.toBe(42)++ expect(cache.genSlugKey).toHaveBeenCalledWith(CachedService.name, 'compute', 1, 2)+ expect(service.original).not.toHaveBeenCalled()+ expect(cache.set).not.toHaveBeenCalled()+ })++ it('should call the original method and cache its result on a cache miss', async () => {+ cache.get.mockResolvedValue(undefined)+ const service = new CachedService(cache as unknown as Cache)++ await expect(service.compute(2, 3)).resolves.toBe(5)++ expect(service.original).toHaveBeenCalledOnce()+ expect(service.original).toHaveBeenCalledWith(2, 3)+ expect(cache.set).toHaveBeenCalledWith('cache-key', 5, 60)+ })++ it('should preserve the context and arguments when reading from the cache fails', async () => {+ const cacheError = new Error('cache read failed')+ const loggerError = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined)+ cache.get.mockRejectedValue(cacheError)+ const service = new CachedService(cache as unknown as Cache)++ await expect(service.compute(4, 5)).resolves.toBe(9)++ expect(loggerError).toHaveBeenCalledWith({ tag: 'compute', msg: `${cacheError}` })+ expect(service.original).toHaveBeenCalledOnce()+ expect(service.original).toHaveBeenCalledWith(4, 5)+ expect(cache.set).toHaveBeenCalledWith('cache-key', 9, 60)+ })++ it('should propagate an original method error without retrying it', async () => {+ const methodError = new Error('method failed')+ cache.get.mockResolvedValue(undefined)+ const service = new CachedService(cache as unknown as Cache)+ service.original.mockRejectedValue(methodError)++ await expect(service.compute(6, 7)).rejects.toBe(methodError)++ expect(service.original).toHaveBeenCalledOnce()+ expect(service.original).toHaveBeenCalledWith(6, 7)+ expect(cache.set).not.toHaveBeenCalled()+ })++ it('should log a cache write error without altering the original result', async () => {+ const cacheError = new Error('cache write failed')+ const loggerError = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined)+ cache.get.mockResolvedValue(undefined)+ cache.set.mockRejectedValue(cacheError)+ const service = new CachedService(cache as unknown as Cache)++ await expect(service.compute(2, 3)).resolves.toBe(5)++ expect(service.original).toHaveBeenCalledOnce()+ await vi.waitFor(() => {+ expect(loggerError).toHaveBeenCalledWith({ tag: 'compute', msg: `${cacheError}` })+ })+ })++ it('should return the cached value and refresh it in the background', async () => {+ cache.get.mockResolvedValue(10)+ const service = new RefreshingCachedService(cache as unknown as Cache)++ await expect(service.compute(8)).resolves.toBe(10)++ await vi.waitFor(() => {+ expect(service.original).toHaveBeenCalledOnce()+ expect(cache.set).toHaveBeenCalledWith('cache-key', 16, 90)+ })+ })++ it('should log a background refresh error while returning the cached value', async () => {+ const refreshError = new Error('refresh failed')+ const loggerError = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined)+ cache.get.mockResolvedValue(10)+ const service = new RefreshingCachedService(cache as unknown as Cache)+ service.original.mockRejectedValue(refreshError)++ await expect(service.compute(8)).resolves.toBe(10)++ await vi.waitFor(() => {+ expect(loggerError).toHaveBeenCalledWith({ tag: 'compute', msg: `${refreshError}` })+ })+ expect(cache.set).not.toHaveBeenCalled()+ })+})diff --git a/backend/src/infrastructure/cache/cache.decorator.ts b/backend/src/infrastructure/cache/cache.decorator.tsindex c13b8dfa..697210ad 100644--- a/backend/src/infrastructure/cache/cache.decorator.ts+++ b/backend/src/infrastructure/cache/cache.decorator.ts@@ -1,33 +1,39 @@-import { Inject } from '@nestjs/common'+import { Inject, Logger } from '@nestjs/common'import { Cache } from './cache.service'export function CacheDecorator(TTL = 120, updateCache: boolean = false) {// if updateCache is true, we update the value in the cache on each callconst injector = Inject(Cache)+ const logger = new Logger(CacheDecorator.name)return (target: any, _key?: string | symbol, descriptor?: TypedPropertyDescriptor<any>) => {injector(target, 'cache')const originalMethod = descriptor.valueconst memoize = async function (...args: any[]): Promise<any> {+ let cacheKey: string | undefinedtry {- const cacheKey = this.cache.genSlugKey(this.constructor.name, originalMethod.name, ...args)+ cacheKey = this.cache.genSlugKey(this.constructor.name, originalMethod.name, ...args)const cacheResult = await this.cache.get(cacheKey)if (cacheResult !== undefined) {if (updateCache) {- originalMethod.apply(this, args).then((r: any) => {- this.cache.set(cacheKey, r, TTL).catch((e: Error) => this.logger.error({ tag: memoize.name, msg: `${e}` }))- })+ originalMethod+ .apply(this, args)+ .then((r: any) => {+ this.cache.set(cacheKey, r, TTL).catch((e: Error) => logger.error({ tag: memoize.name, msg: `${e}` }))+ })+ .catch((e: Error) => logger.error({ tag: memoize.name, msg: `${e}` }))}return cacheResult}- const r: any = await originalMethod.apply(this, args)- this.cache.set(cacheKey, r, TTL).catch((e: Error) => this.logger.error({ tag: memoize.name, msg: `${e}` }))- return r} catch (e) {- console.error(e)- return originalMethod(args)+ logger.error({ tag: memoize.name, msg: `${e}` })}+ const r: any = await originalMethod.apply(this, args)+ if (cacheKey !== undefined) {+ this.cache.set(cacheKey, r, TTL).catch((e: Error) => logger.error({ tag: memoize.name, msg: `${e}` }))+ }+ return r}// keep the original function nameObject.defineProperty(memoize, 'name', {diff --git a/backend/src/infrastructure/cache/cache.e2e-spec.ts b/backend/src/infrastructure/cache/cache.e2e-spec.tsindex 6083b27b..726e1b02 100644--- a/backend/src/infrastructure/cache/cache.e2e-spec.ts+++ b/backend/src/infrastructure/cache/cache.e2e-spec.ts@@ -83,17 +83,4 @@ describe(Cache.name, () => {it('should create a slug key from parameters', () => {expect(cache.genSlugKey('foo', 'BAR', 12341)).toBe('foo-bar-12341')})-- // it('should exit if maxConnectRetry is reached', () => {- // const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {- // throw new Error('process.exit')- // })- // expect(() => RedisCache.redisReconnectStrategy(RedisCache.redisReconnectOptions.maxAttempts + 1)).toThrow()- // expect(mockExit).toHaveBeenCalledTimes(1)- // mockExit.mockRestore()- // })- //- // it('should not exit if maxConnectRetry is not reached', () => {- // expect(RedisCache.redisReconnectStrategy(1)).toBeGreaterThan(0)- // })})