1 import { ACTOR_FOLLOW_SCORE } from '../initializers/constants'
2 import { logger } from '../helpers/logger'
4 // Cache follows scores, instead of writing them too often in database
5 // Keep data in memory, we don't really need Redis here as we don't really care to loose some scores
6 class ActorFollowHealthCache {
8 private static instance: ActorFollowHealthCache
10 private pendingFollowsScore: { [ url: string ]: number } = {}
12 private pendingBadServer = new Set<number>()
13 private pendingGoodServer = new Set<number>()
15 private readonly badInboxes = new Set<string>()
17 private constructor () {}
19 static get Instance () {
20 return this.instance || (this.instance = new this())
23 updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) {
24 this.badInboxes.clear()
26 if (goodInboxes.length === 0 && badInboxes.length === 0) return
29 'Updating %d good actor follows and %d bad actor follows scores in cache.',
30 goodInboxes.length, badInboxes.length, { badInboxes }
33 for (const goodInbox of goodInboxes) {
34 if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
36 this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
39 for (const badInbox of badInboxes) {
40 if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
42 this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
43 this.badInboxes.add(badInbox)
47 isBadInbox (inboxUrl: string) {
48 return this.badInboxes.has(inboxUrl)
51 addBadServerId (serverId: number) {
52 this.pendingBadServer.add(serverId)
55 getBadFollowingServerIds () {
56 return Array.from(this.pendingBadServer)
59 clearBadFollowingServerIds () {
60 this.pendingBadServer = new Set<number>()
63 addGoodServerId (serverId: number) {
64 this.pendingGoodServer.add(serverId)
67 getGoodFollowingServerIds () {
68 return Array.from(this.pendingGoodServer)
71 clearGoodFollowingServerIds () {
72 this.pendingGoodServer = new Set<number>()
75 getPendingFollowsScore () {
76 return this.pendingFollowsScore
79 clearPendingFollowsScore () {
80 this.pendingFollowsScore = {}
85 ActorFollowHealthCache