backfillqueue.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan, Sumner Evans
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "time"
  19. log "maunium.net/go/maulogger/v2"
  20. "maunium.net/go/mautrix-whatsapp/database"
  21. )
  22. type BackfillQueue struct {
  23. BackfillQuery *database.BackfillQuery
  24. ImmediateBackfillRequests chan *database.Backfill
  25. DeferredBackfillRequests chan *database.Backfill
  26. ReCheckQueue chan bool
  27. log log.Logger
  28. }
  29. func (bq *BackfillQueue) RunLoops(user *User) {
  30. go bq.immediateBackfillLoop(user)
  31. bq.deferredBackfillLoop(user)
  32. }
  33. func (bq *BackfillQueue) immediateBackfillLoop(user *User) {
  34. for {
  35. if backfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillImmediate); backfill != nil {
  36. bq.ImmediateBackfillRequests <- backfill
  37. backfill.MarkDone()
  38. } else {
  39. select {
  40. case <-bq.ReCheckQueue:
  41. bq.log.Debugfln("Re-checking infinite backfill queue due to forced re-check")
  42. case <-time.After(10 * time.Second):
  43. bq.log.Debugfln("Re-checking infinite backfill queue due to timeout")
  44. }
  45. }
  46. }
  47. }
  48. func (bq *BackfillQueue) deferredBackfillLoop(user *User) {
  49. for {
  50. // Finish all immediate backfills before doing the deferred ones.
  51. if immediate := bq.BackfillQuery.GetNext(user.MXID, database.BackfillImmediate); immediate != nil {
  52. bq.log.Debugfln("Not doing any deferred or media backfill since there are immediate backfills to do")
  53. time.Sleep(10 * time.Second)
  54. } else if backfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillDeferred); backfill != nil {
  55. bq.DeferredBackfillRequests <- backfill
  56. backfill.MarkDone()
  57. } else if mediaBackfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillMedia); mediaBackfill != nil {
  58. bq.DeferredBackfillRequests <- mediaBackfill
  59. mediaBackfill.MarkDone()
  60. } else {
  61. select {
  62. case <-bq.ReCheckQueue:
  63. case <-time.After(time.Minute):
  64. }
  65. }
  66. }
  67. }