database.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "database/sql"
  19. "errors"
  20. "fmt"
  21. "net"
  22. "time"
  23. "github.com/lib/pq"
  24. _ "github.com/mattn/go-sqlite3"
  25. log "maunium.net/go/maulogger/v2"
  26. "go.mau.fi/whatsmeow/store"
  27. "go.mau.fi/whatsmeow/store/sqlstore"
  28. "maunium.net/go/mautrix-whatsapp/config"
  29. "maunium.net/go/mautrix-whatsapp/database/upgrades"
  30. )
  31. func init() {
  32. sqlstore.PostgresArrayWrapper = pq.Array
  33. }
  34. type Database struct {
  35. *sql.DB
  36. log log.Logger
  37. dialect string
  38. User *UserQuery
  39. Portal *PortalQuery
  40. Puppet *PuppetQuery
  41. Message *MessageQuery
  42. Reaction *ReactionQuery
  43. DisappearingMessage *DisappearingMessageQuery
  44. BackfillQuery *BackfillQuery
  45. HistorySyncQuery *HistorySyncQuery
  46. }
  47. func New(cfg config.DatabaseConfig, baseLog log.Logger) (*Database, error) {
  48. conn, err := sql.Open(cfg.Type, cfg.URI)
  49. if err != nil {
  50. return nil, err
  51. }
  52. db := &Database{
  53. DB: conn,
  54. log: baseLog.Sub("Database"),
  55. dialect: cfg.Type,
  56. }
  57. db.User = &UserQuery{
  58. db: db,
  59. log: db.log.Sub("User"),
  60. }
  61. db.Portal = &PortalQuery{
  62. db: db,
  63. log: db.log.Sub("Portal"),
  64. }
  65. db.Puppet = &PuppetQuery{
  66. db: db,
  67. log: db.log.Sub("Puppet"),
  68. }
  69. db.Message = &MessageQuery{
  70. db: db,
  71. log: db.log.Sub("Message"),
  72. }
  73. db.Reaction = &ReactionQuery{
  74. db: db,
  75. log: db.log.Sub("Reaction"),
  76. }
  77. db.DisappearingMessage = &DisappearingMessageQuery{
  78. db: db,
  79. log: db.log.Sub("DisappearingMessage"),
  80. }
  81. db.BackfillQuery = &BackfillQuery{
  82. db: db,
  83. log: db.log.Sub("Backfill"),
  84. }
  85. db.HistorySyncQuery = &HistorySyncQuery{
  86. db: db,
  87. log: db.log.Sub("HistorySync"),
  88. }
  89. db.SetMaxOpenConns(cfg.MaxOpenConns)
  90. db.SetMaxIdleConns(cfg.MaxIdleConns)
  91. if len(cfg.ConnMaxIdleTime) > 0 {
  92. maxIdleTimeDuration, err := time.ParseDuration(cfg.ConnMaxIdleTime)
  93. if err != nil {
  94. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  95. }
  96. db.SetConnMaxIdleTime(maxIdleTimeDuration)
  97. }
  98. if len(cfg.ConnMaxLifetime) > 0 {
  99. maxLifetimeDuration, err := time.ParseDuration(cfg.ConnMaxLifetime)
  100. if err != nil {
  101. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  102. }
  103. db.SetConnMaxLifetime(maxLifetimeDuration)
  104. }
  105. return db, nil
  106. }
  107. func (db *Database) Init() error {
  108. return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
  109. }
  110. type Scannable interface {
  111. Scan(...interface{}) error
  112. }
  113. func isRetryableError(err error) bool {
  114. if pqError := (&pq.Error{}); errors.As(err, &pqError) {
  115. switch pqError.Code.Class() {
  116. case "08", // Connection Exception
  117. "53", // Insufficient Resources (e.g. too many connections)
  118. "57": // Operator Intervention (e.g. server restart)
  119. return true
  120. }
  121. } else if netError := (&net.OpError{}); errors.As(err, &netError) {
  122. return true
  123. }
  124. return false
  125. }
  126. func (db *Database) HandleSignalStoreError(device *store.Device, action string, attemptIndex int, err error) (retry bool) {
  127. if db.dialect != "sqlite" && isRetryableError(err) {
  128. sleepTime := time.Duration(attemptIndex*2) * time.Second
  129. device.Log.Warnf("Failed to %s (attempt #%d): %v - retrying in %v", action, attemptIndex+1, err, sleepTime)
  130. time.Sleep(sleepTime)
  131. return true
  132. }
  133. device.Log.Errorf("Failed to %s: %v", action, err)
  134. return false
  135. }