aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/actor-follow-health-cache.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/actor-follow-health-cache.ts')
-rw-r--r--server/lib/actor-follow-health-cache.ts86
1 files changed, 86 insertions, 0 deletions
diff --git a/server/lib/actor-follow-health-cache.ts b/server/lib/actor-follow-health-cache.ts
new file mode 100644
index 000000000..ab8cc98df
--- /dev/null
+++ b/server/lib/actor-follow-health-cache.ts
@@ -0,0 +1,86 @@
1import { ACTOR_FOLLOW_SCORE } from '../initializers/constants'
2import { logger } from '../helpers/logger'
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
6class ActorFollowHealthCache {
7
8 private static instance: ActorFollowHealthCache
9
10 private pendingFollowsScore: { [ url: string ]: number } = {}
11
12 private pendingBadServer = new Set<number>()
13 private pendingGoodServer = new Set<number>()
14
15 private badInboxes = new Set<string>()
16
17 private constructor () {}
18
19 static get Instance () {
20 return this.instance || (this.instance = new this())
21 }
22
23 updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) {
24 this.badInboxes.clear()
25
26 if (goodInboxes.length === 0 && badInboxes.length === 0) return
27
28 logger.info(
29 'Updating %d good actor follows and %d bad actor follows scores in cache.',
30 goodInboxes.length, badInboxes.length, { badInboxes }
31 )
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
43 this.badInboxes.add(badInbox)
44 }
45 }
46
47 isBadInbox (inboxUrl: string) {
48 return this.badInboxes.has(inboxUrl)
49 }
50
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 () {
76 return this.pendingFollowsScore
77 }
78
79 clearPendingFollowsScore () {
80 this.pendingFollowsScore = {}
81 }
82}
83
84export {
85 ActorFollowHealthCache
86}