sync: 0.7.3 from main (15b0dbdde03c6b89733ca07d36eefa25fd301107)
This commit is contained in:
1 parent
9337afc47e
commit
80d7c72c07
12 files changed
+484
-315
No files matched your search
@@ -10,6 +10,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gomessguii/logger"
|
||||
// The binary's only registration of the "postgres" driver. Without it the
|
||||
// build still passes and sql.Open("postgres") fails at boot.
|
||||
_ "github.com/lib/pq"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The only guard for the lib/pq import: build and vet pass without it.
|
||||
func TestPostgresDriverRegistered(t *testing.T) {
|
||||
if !slices.Contains(sql.Drivers(), "postgres") {
|
||||
t.Fatalf("driver \"postgres\" not registered; sql.Drivers() = %v", sql.Drivers())
|
||||
}
|
||||
}
|
||||
+248
-248
File diff suppressed because it is too large.
Load diff
@@ -444,7 +444,7 @@ func (u *userService) BlockContact(data *BlockStruct, instance *instance_model.I
|
||||
return nil, errors.New("invalid phone number")
|
||||
}
|
||||
|
||||
resp, err := client.UpdateBlocklist(context.Background(), jid, events.BlocklistChangeActionBlock)
|
||||
resp, err := client.UpdateBlocklist(context.Background(), jid, events.BlocklistChangeActionBlock, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -463,7 +463,7 @@ func (u *userService) UnlockContact(data *BlockStruct, instance *instance_model.
|
||||
return nil, errors.New("invalid phone number")
|
||||
}
|
||||
|
||||
resp, err := client.UpdateBlocklist(context.Background(), jid, events.BlocklistChangeActionUnblock)
|
||||
resp, err := client.UpdateBlocklist(context.Background(), jid, events.BlocklistChangeActionUnblock, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -477,7 +477,7 @@ func (u *userService) GetBlockList(instance *instance_model.Instance) (*types.Bl
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := client.GetBlocklist(context.Background())
|
||||
resp, err := client.GetBlocklist(context.Background(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -532,7 +532,7 @@ func (u *userService) SetProfileStatus(data *SetProfileStatusStruct, instance *i
|
||||
return false, err
|
||||
}
|
||||
|
||||
err = client.SetStatusMessage(context.Background(), data.Status)
|
||||
err = client.SetStatusMessage(context.Background(), types.SetStatusInput{Text: &data.Status})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package whatsmeow_service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/evolution-foundation/evolution-go/pkg/config"
|
||||
)
|
||||
|
||||
// Exercised through SQLite only. Closing ownDB on the Upgrade error path is not
|
||||
// covered: the handle is local to getAuthContainer.
|
||||
func TestGetAuthContainerRetryAndReuse(t *testing.T) {
|
||||
// TempDirs first: cleanups run LIFO, so reset closes the *sql.DB before
|
||||
// RemoveAll, which fails on Windows while the file is still open.
|
||||
badPath := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
goodPath := t.TempDir()
|
||||
|
||||
reset := func() {
|
||||
sharedAuthContainerMu.Lock()
|
||||
if sharedAuthContainer != nil {
|
||||
_ = sharedAuthContainer.Close()
|
||||
sharedAuthContainer = nil
|
||||
}
|
||||
sharedAuthContainerMu.Unlock()
|
||||
}
|
||||
reset()
|
||||
t.Cleanup(reset)
|
||||
|
||||
// SQLite does not create missing directories, so Upgrade fails here.
|
||||
bad := whatsmeowService{
|
||||
config: &config.Config{},
|
||||
exPath: badPath,
|
||||
}
|
||||
if _, err := bad.getAuthContainer(); err == nil {
|
||||
t.Fatal("expected an error for a missing directory, got nil")
|
||||
}
|
||||
sharedAuthContainerMu.Lock()
|
||||
if sharedAuthContainer != nil {
|
||||
sharedAuthContainerMu.Unlock()
|
||||
t.Fatalf("failed container should not be memoized (sharedAuthContainer = %#v)", sharedAuthContainer)
|
||||
}
|
||||
sharedAuthContainerMu.Unlock()
|
||||
|
||||
// The DSN expects a dbdata subdirectory.
|
||||
if err := os.MkdirAll(filepath.Join(goodPath, "dbdata"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
good := whatsmeowService{
|
||||
config: &config.Config{},
|
||||
exPath: goodPath,
|
||||
}
|
||||
c1, err := good.getAuthContainer()
|
||||
if err != nil {
|
||||
t.Fatalf("retry after a failure should succeed instead of returning a cached error: %v", err)
|
||||
}
|
||||
if c1 == nil {
|
||||
t.Fatal("nil container after success")
|
||||
}
|
||||
|
||||
c2, err := good.getAuthContainer()
|
||||
if err != nil {
|
||||
t.Fatalf("second call failed: %v", err)
|
||||
}
|
||||
if c1 != c2 {
|
||||
t.Fatal("container was not reused; expected the shared instance")
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"golang.org/x/image/webp"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/skip2/go-qrcode"
|
||||
"go.mau.fi/whatsmeow"
|
||||
@@ -301,6 +300,65 @@ func (w whatsmeowService) ForceUpdateJid(instanceId string, number string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shared by every StartClient: a container per call opens a pool that is never
|
||||
// closed. Only success is memoized, so a failed creation is retried next call.
|
||||
var (
|
||||
sharedAuthContainer *sqlstore.Container
|
||||
sharedAuthContainerMu sync.Mutex
|
||||
)
|
||||
|
||||
func (w whatsmeowService) getAuthContainer() (*sqlstore.Container, error) {
|
||||
sharedAuthContainerMu.Lock()
|
||||
defer sharedAuthContainerMu.Unlock()
|
||||
|
||||
if sharedAuthContainer != nil {
|
||||
return sharedAuthContainer, nil
|
||||
}
|
||||
|
||||
var dbLog waLog.Logger
|
||||
if w.config.WaDebug != "" {
|
||||
dbLog = waLog.Stdout("Database", w.config.WaDebug, true)
|
||||
}
|
||||
|
||||
var container *sqlstore.Container
|
||||
var ownDB *sql.DB
|
||||
|
||||
if w.config.PostgresAuthDB != "" {
|
||||
// Reuse the pool main.go opened and capped; a second pool on the same
|
||||
// DSN would raise the connection ceiling.
|
||||
if w.authDB == nil {
|
||||
return nil, fmt.Errorf("POSTGRES_AUTH_DB is set but the auth database pool was not initialized")
|
||||
}
|
||||
container = sqlstore.NewWithDB(w.authDB, "postgres", dbLog)
|
||||
} else {
|
||||
dsn := fmt.Sprintf("file:%s/dbdata/main.db?_pragma=foreign_keys(1)&_busy_timeout=5000&cache=shared&mode=rwc&_journal_mode=WAL", w.exPath)
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open auth database: %w", err)
|
||||
}
|
||||
// Process-wide cap shared by all instances. Fix the DSN before raising it:
|
||||
// modernc ignores _journal_mode and _busy_timeout, so WAL is never enabled.
|
||||
db.SetMaxOpenConns(1)
|
||||
ownDB = db
|
||||
container = sqlstore.NewWithDB(db, "sqlite", dbLog)
|
||||
}
|
||||
|
||||
// Upgrade runs under the mutex: without a deadline, a server that accepts TCP
|
||||
// but never answers would stall every StartClient behind it.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
if err := container.Upgrade(ctx); err != nil {
|
||||
// Close only what this call opened; the Postgres pool belongs to main.
|
||||
if ownDB != nil {
|
||||
_ = ownDB.Close()
|
||||
}
|
||||
return nil, fmt.Errorf("failed to upgrade auth database: %w", err)
|
||||
}
|
||||
|
||||
sharedAuthContainer = container
|
||||
return container, nil
|
||||
}
|
||||
|
||||
func (w whatsmeowService) StartClient(cd *ClientData) {
|
||||
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogInfo("Starting websocket connection to Whatsapp for user '%s'", cd.Instance.Id)
|
||||
@@ -314,27 +372,9 @@ func (w whatsmeowService) StartClient(cd *ClientData) {
|
||||
}
|
||||
}
|
||||
|
||||
var container *sqlstore.Container
|
||||
|
||||
if w.config.WaDebug != "" {
|
||||
dbLog := waLog.Stdout("Database", w.config.WaDebug, true)
|
||||
if w.config.PostgresAuthDB != "" {
|
||||
container, err = sqlstore.New(context.Background(), "postgres", w.config.PostgresAuthDB, dbLog)
|
||||
} else {
|
||||
dsn := fmt.Sprintf("file:%s/dbdata/main.db?_pragma=foreign_keys(1)&_busy_timeout=5000&cache=shared&mode=rwc&_journal_mode=WAL", w.exPath)
|
||||
container, err = sqlstore.New(context.Background(), "sqlite", dsn, dbLog)
|
||||
}
|
||||
} else {
|
||||
if w.config.PostgresAuthDB != "" {
|
||||
container, err = sqlstore.New(context.Background(), "postgres", w.config.PostgresAuthDB, nil)
|
||||
} else {
|
||||
dsn := fmt.Sprintf("file:%s/dbdata/main.db?_pragma=foreign_keys(1)&_busy_timeout=5000&cache=shared&mode=rwc&_journal_mode=WAL", w.exPath)
|
||||
container, err = sqlstore.New(context.Background(), "sqlite", dsn, nil)
|
||||
}
|
||||
}
|
||||
|
||||
container, err := w.getAuthContainer()
|
||||
if err != nil {
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogError("[%s] Failed to create container: %v", cd.Instance.Id, err)
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogError("[%s] Failed to get auth container: %v", cd.Instance.Id, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user