ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actor-follow-health-cache.ts 2.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { ACTOR_FOLLOW_SCORE } from '../initializers/constants.js'
  2. import { logger } from '../helpers/logger.js'
  3. // Cache follows scores, instead of writing them too often in database
  4. // Keep data in memory, we don't really need Redis here as we don't really care to loose some scores
  5. class ActorFollowHealthCache {
  6. private static instance: ActorFollowHealthCache
  7. private pendingFollowsScore: { [ url: string ]: number } = {}
  8. private pendingBadServer = new Set<number>()
  9. private pendingGoodServer = new Set<number>()
  10. private readonly badInboxes = new Set<string>()
  11. private constructor () {}
  12. static get Instance () {
  13. return this.instance || (this.instance = new this())
  14. }
  15. updateActorFollowsHealth (goodInboxes: string[], badInboxes: string[]) {
  16. this.badInboxes.clear()
  17. if (goodInboxes.length === 0 && badInboxes.length === 0) return
  18. logger.info(
  19. 'Updating %d good actor follows and %d bad actor follows scores in cache.',
  20. goodInboxes.length, badInboxes.length, { badInboxes }
  21. )
  22. for (const goodInbox of goodInboxes) {
  23. if (this.pendingFollowsScore[goodInbox] === undefined) this.pendingFollowsScore[goodInbox] = 0
  24. this.pendingFollowsScore[goodInbox] += ACTOR_FOLLOW_SCORE.BONUS
  25. }
  26. for (const badInbox of badInboxes) {
  27. if (this.pendingFollowsScore[badInbox] === undefined) this.pendingFollowsScore[badInbox] = 0
  28. this.pendingFollowsScore[badInbox] += ACTOR_FOLLOW_SCORE.PENALTY
  29. this.badInboxes.add(badInbox)
  30. }
  31. }
  32. isBadInbox (inboxUrl: string) {
  33. return this.badInboxes.has(inboxUrl)
  34. }
  35. addBadServerId (serverId: number) {
  36. this.pendingBadServer.add(serverId)
  37. }
  38. getBadFollowingServerIds () {
  39. return Array.from(this.pendingBadServer)
  40. }
  41. clearBadFollowingServerIds () {
  42. this.pendingBadServer = new Set<number>()
  43. }
  44. addGoodServerId (serverId: number) {
  45. this.pendingGoodServer.add(serverId)
  46. }
  47. getGoodFollowingServerIds () {
  48. return Array.from(this.pendingGoodServer)
  49. }
  50. clearGoodFollowingServerIds () {
  51. this.pendingGoodServer = new Set<number>()
  52. }
  53. getPendingFollowsScore () {
  54. return this.pendingFollowsScore
  55. }
  56. clearPendingFollowsScore () {
  57. this.pendingFollowsScore = {}
  58. }
  59. }
  60. export {
  61. ActorFollowHealthCache
  62. }