]>
Commit | Line | Data |
---|---|---|
9db437c8 C |
1 | import { ACTOR_FOLLOW_SCORE } from '../initializers/constants' |
2 | import { logger } from '../helpers/logger' | |
2f5c6b2f C |
3 | |
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 | |
9db437c8 C |
6 | class ActorFollowHealthCache { |
7 | ||
8 | private static instance: ActorFollowHealthCache | |
2f5c6b2f | 9 | |
2f5c6b2f | 10 | private pendingFollowsScore: { [ url: string ]: number } = {} |
9db437c8 | 11 | |
6b9c966f C |
12 | private pendingBadServer = new Set<number>() |
13 | private pendingGoodServer = new Set<number>() | |
2f5c6b2f | 14 | |
1cf0df02 | 15 | private readonly badInboxes = new Set<string>() |
9db437c8 | 16 | |
2f5c6b2f C |
17 | private constructor () {} |
18 | ||
19 | static get Instance () { | |
20 | return this.instance || (this.instance = new this()) | |
21 | } | |
22 | ||
9db437c8 C |
23 | updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) { |
24 | this.badInboxes.clear() | |
25 | ||
2f5c6b2f C |
26 | if (goodInboxes.length === 0 && badInboxes.length === 0) return |
27 | ||
d41a3805 C |
28 | logger.info( |
29 | 'Updating %d good actor follows and %d bad actor follows scores in cache.', | |
30 | goodInboxes.length, badInboxes.length, { badInboxes } | |
31 | ) | |
2f5c6b2f C |
32 | |
33 | for (const goodInbox of goodInboxes) { | |
34 | if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0 | |
35 | ||
36 | this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS | |
37 | } | |
38 | ||
39 | for (const badInbox of badInboxes) { | |
40 | if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0 | |
41 | ||
42 | this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY | |
9db437c8 | 43 | this.badInboxes.add(badInbox) |
2f5c6b2f C |
44 | } |
45 | } | |
46 | ||
9db437c8 C |
47 | isBadInbox (inboxUrl: string) { |
48 | return this.badInboxes.has(inboxUrl) | |
49 | } | |
50 | ||
6b9c966f C |
51 | addBadServerId (serverId: number) { |
52 | this.pendingBadServer.add(serverId) | |
53 | } | |
54 | ||
55 | getBadFollowingServerIds () { | |
56 | return Array.from(this.pendingBadServer) | |
57 | } | |
58 | ||
59 | clearBadFollowingServerIds () { | |
60 | this.pendingBadServer = new Set<number>() | |
61 | } | |
62 | ||
63 | addGoodServerId (serverId: number) { | |
64 | this.pendingGoodServer.add(serverId) | |
65 | } | |
66 | ||
67 | getGoodFollowingServerIds () { | |
68 | return Array.from(this.pendingGoodServer) | |
69 | } | |
70 | ||
71 | clearGoodFollowingServerIds () { | |
72 | this.pendingGoodServer = new Set<number>() | |
73 | } | |
74 | ||
75 | getPendingFollowsScore () { | |
2f5c6b2f C |
76 | return this.pendingFollowsScore |
77 | } | |
78 | ||
79 | clearPendingFollowsScore () { | |
80 | this.pendingFollowsScore = {} | |
81 | } | |
82 | } | |
83 | ||
84 | export { | |
9db437c8 | 85 | ActorFollowHealthCache |
2f5c6b2f | 86 | } |