database.go 4.0 KB

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