database.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  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 database
  17. import (
  18. "errors"
  19. "net"
  20. "time"
  21. "github.com/lib/pq"
  22. _ "github.com/mattn/go-sqlite3"
  23. "go.mau.fi/whatsmeow/store"
  24. "go.mau.fi/whatsmeow/store/sqlstore"
  25. "maunium.net/go/mautrix-whatsapp/database/upgrades"
  26. "maunium.net/go/mautrix/util/dbutil"
  27. )
  28. func init() {
  29. sqlstore.PostgresArrayWrapper = pq.Array
  30. }
  31. type Database struct {
  32. *dbutil.Database
  33. User *UserQuery
  34. Portal *PortalQuery
  35. Puppet *PuppetQuery
  36. Message *MessageQuery
  37. Reaction *ReactionQuery
  38. DisappearingMessage *DisappearingMessageQuery
  39. Backfill *BackfillQuery
  40. HistorySync *HistorySyncQuery
  41. MediaBackfillRequest *MediaBackfillRequestQuery
  42. }
  43. func New(baseDB *dbutil.Database) *Database {
  44. db := &Database{Database: baseDB}
  45. db.UpgradeTable = upgrades.Table
  46. db.User = &UserQuery{
  47. db: db,
  48. log: db.Log.Sub("User"),
  49. }
  50. db.Portal = &PortalQuery{
  51. db: db,
  52. log: db.Log.Sub("Portal"),
  53. }
  54. db.Puppet = &PuppetQuery{
  55. db: db,
  56. log: db.Log.Sub("Puppet"),
  57. }
  58. db.Message = &MessageQuery{
  59. db: db,
  60. log: db.Log.Sub("Message"),
  61. }
  62. db.Reaction = &ReactionQuery{
  63. db: db,
  64. log: db.Log.Sub("Reaction"),
  65. }
  66. db.DisappearingMessage = &DisappearingMessageQuery{
  67. db: db,
  68. log: db.Log.Sub("DisappearingMessage"),
  69. }
  70. db.Backfill = &BackfillQuery{
  71. db: db,
  72. log: db.Log.Sub("Backfill"),
  73. }
  74. db.HistorySync = &HistorySyncQuery{
  75. db: db,
  76. log: db.Log.Sub("HistorySync"),
  77. }
  78. db.MediaBackfillRequest = &MediaBackfillRequestQuery{
  79. db: db,
  80. log: db.Log.Sub("MediaBackfillRequest"),
  81. }
  82. return db
  83. }
  84. func isRetryableError(err error) bool {
  85. if pqError := (&pq.Error{}); errors.As(err, &pqError) {
  86. switch pqError.Code.Class() {
  87. case "08", // Connection Exception
  88. "53", // Insufficient Resources (e.g. too many connections)
  89. "57": // Operator Intervention (e.g. server restart)
  90. return true
  91. }
  92. } else if netError := (&net.OpError{}); errors.As(err, &netError) {
  93. return true
  94. }
  95. return false
  96. }
  97. func (db *Database) HandleSignalStoreError(device *store.Device, action string, attemptIndex int, err error) (retry bool) {
  98. if db.Dialect != dbutil.SQLite && isRetryableError(err) {
  99. sleepTime := time.Duration(attemptIndex*2) * time.Second
  100. device.Log.Warnf("Failed to %s (attempt #%d): %v - retrying in %v", action, attemptIndex+1, err, sleepTime)
  101. time.Sleep(sleepTime)
  102. return true
  103. }
  104. device.Log.Errorf("Failed to %s: %v", action, err)
  105. return false
  106. }