From f1569117f9bc468bc38b5b8d32df1bce40cc42ad Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Dec 2021 11:36:51 +0100 Subject: Cleanup unavailable remote AP resource --- server/lib/redis.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'server/lib/redis.ts') diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 9ef9d7702..4dcbcddb5 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -1,9 +1,11 @@ import { createClient } from 'redis' import { exists } from '@server/helpers/custom-validators/misc' +import { sha256 } from '@shared/extra-utils' import { logger } from '../helpers/logger' import { generateRandomString } from '../helpers/utils' import { CONFIG } from '../initializers/config' import { + AP_CLEANER, CONTACT_FORM_LIFETIME, RESUMABLE_UPLOAD_SESSION_LIFETIME, TRACKER_RATE_LIMITS, @@ -260,6 +262,17 @@ class Redis { return this.deleteKey('resumable-upload-' + uploadId) } + /* ************ AP ressource unavailability ************ */ + + async addAPUnavailability (url: string) { + const key = this.generateAPUnavailabilityKey(url) + + const value = await this.increment(key) + await this.setExpiration(key, AP_CLEANER.PERIOD * 2) + + return value + } + /* ************ Keys generation ************ */ private generateLocalVideoViewsKeys (videoId?: Number) { @@ -298,6 +311,10 @@ class Redis { return 'contact-form-' + ip } + private generateAPUnavailabilityKey (url: string) { + return 'ap-unavailability-' + sha256(url) + } + /* ************ Redis helpers ************ */ private getValue (key: string) { @@ -330,10 +347,6 @@ class Redis { return this.client.del(this.prefix + key) } - private getObject (key: string) { - return this.client.hGetAll(this.prefix + key) - } - private increment (key: string) { return this.client.incr(this.prefix + key) } @@ -342,6 +355,10 @@ class Redis { return this.client.exists(this.prefix + key) } + private setExpiration (key: string, ms: number) { + return this.client.expire(this.prefix + key, ms / 1000) + } + static get Instance () { return this.instance || (this.instance = new this()) } -- cgit v1.2.3