diff options
author | Chocobozzz <me@florianbigard.com> | 2021-10-13 11:47:32 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-10-13 11:47:32 +0200 |
commit | 9db437c8155f3563a33e22ed2896072a9f1fbdb0 (patch) | |
tree | 716078fbe1506e0b0d19936f4939e9c530b3b8ab /server/lib/actor-follow-health-cache.ts | |
parent | e81f6ccf989d4573b59ec7b2bf2812fe3e9fb534 (diff) | |
download | PeerTube-9db437c8155f3563a33e22ed2896072a9f1fbdb0.tar.gz PeerTube-9db437c8155f3563a33e22ed2896072a9f1fbdb0.tar.zst PeerTube-9db437c8155f3563a33e22ed2896072a9f1fbdb0.zip |
Process slow followers in unicast job queue
Diffstat (limited to 'server/lib/actor-follow-health-cache.ts')
-rw-r--r-- | server/lib/actor-follow-health-cache.ts | 86 |
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 @@ | |||
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 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 | } | ||