]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/actor-follow-health-cache.ts
External auth can update user on login
[github/Chocobozzz/PeerTube.git] / server / lib / actor-follow-health-cache.ts
1 import { ACTOR_FOLLOW_SCORE } from '../initializers/constants'
2 import { 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
6 class 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 readonly 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
84 export {
85 ActorFollowHealthCache
86 }