backfillqueue.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. case <-time.After(10 * time.Second):
  42. }
  43. }
  44. }
  45. }
  46. func (bq *BackfillQueue) deferredBackfillLoop(user *User) {
  47. for {
  48. // Finish all immediate backfills before doing the deferred ones.
  49. if immediate := bq.BackfillQuery.GetNext(user.MXID, database.BackfillImmediate); immediate != nil {
  50. time.Sleep(10 * time.Second)
  51. continue
  52. }
  53. if backfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillDeferred); backfill != nil {
  54. bq.DeferredBackfillRequests <- backfill
  55. backfill.MarkDone()
  56. } else {
  57. time.Sleep(10 * time.Second)
  58. }
  59. }
  60. }