fix(auth): adopt upstream 0.7.3 retry and publish verified latest
This commit is contained in:
1 parent
b140fdb390
commit
406fcc2e52
8 files changed
+220
-45
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package whatsmeow_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/evolution-foundation/evolution-go/pkg/config"
|
||||
"go.mau.fi/whatsmeow/proto/waAdv"
|
||||
"go.mau.fi/whatsmeow/store/sqlstore"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestSharedAuthContainerRetryAndConcurrentReuse(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
w := whatsmeowService{config: &config.Config{}, exPath: root, authStore: &sharedSQLStore{}}
|
||||
t.Cleanup(func() {
|
||||
if w.authStore.sqliteDB != nil {
|
||||
if err := w.authStore.sqliteDB.Close(); err != nil {
|
||||
t.Errorf("close auth store: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
if _, err := w.getSharedSQLStoreContainer(); err == nil {
|
||||
t.Fatal("expected failure before dbdata exists")
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(root, "dbdata"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const callers = 16
|
||||
containers := make([]*sqlstore.Container, callers)
|
||||
errs := make([]error, callers)
|
||||
var wg sync.WaitGroup
|
||||
for i := range callers {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
containers[i], errs[i] = w.getSharedSQLStoreContainer()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
for i := range callers {
|
||||
if errs[i] != nil || containers[i] == nil {
|
||||
t.Fatalf("caller %d could not retry: %v", i, errs[i])
|
||||
}
|
||||
if containers[i] != containers[0] {
|
||||
t.Fatalf("caller %d opened a different container", i)
|
||||
}
|
||||
}
|
||||
if got := w.authStore.sqliteDB.Stats().MaxOpenConnections; got != 1 {
|
||||
t.Fatalf("SQLite connection limit = %d, want 1", got)
|
||||
}
|
||||
// Persist a paired-device record and reopen the existing schema as on restart.
|
||||
device := containers[0].NewDevice()
|
||||
jid := types.NewJID("5511999999999", types.DefaultUserServer)
|
||||
device.ID = &jid
|
||||
device.Account = &waAdv.ADVSignedDeviceIdentity{
|
||||
Details: []byte("test device"),
|
||||
AccountSignature: make([]byte, 64),
|
||||
AccountSignatureKey: make([]byte, 32),
|
||||
DeviceSignature: make([]byte, 64),
|
||||
}
|
||||
if err := device.Save(context.Background()); err != nil {
|
||||
t.Fatalf("save session: %v", err)
|
||||
}
|
||||
if err := w.authStore.sqliteDB.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.authStore = &sharedSQLStore{}
|
||||
reopened, err := w.getSharedSQLStoreContainer()
|
||||
if err != nil {
|
||||
t.Fatalf("reopen existing auth database: %v", err)
|
||||
}
|
||||
restored, err := reopened.GetDevice(context.Background(), jid)
|
||||
if err != nil || restored == nil {
|
||||
t.Fatalf("restore session: device=%v err=%v", restored, err)
|
||||
}
|
||||
if restored.RegistrationID != device.RegistrationID || *restored.NoiseKey.Pub != *device.NoiseKey.Pub {
|
||||
t.Fatal("session keys changed after reopening")
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,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"
|
||||
@@ -99,16 +98,15 @@ type whatsmeowService struct {
|
||||
natsProducer producer_interfaces.Producer
|
||||
loggerWrapper *logger_wrapper.LoggerManager
|
||||
passkeyCeremony *ceremony.Store
|
||||
// authStore is heap-allocated so sync.Once works with value-receiver methods like StartClient.
|
||||
// authStore is heap-allocated so its mutex is shared by value-receiver methods.
|
||||
authStore *sharedSQLStore
|
||||
}
|
||||
|
||||
// sharedSQLStore holds the process-wide whatsmeow sqlstore.Container for PostgresAuthDB
|
||||
// (or the sqlite fallback). One Upgrade per process; never Close on instance disconnect.
|
||||
// (or the sqlite fallback). Cache only success; never Close on instance disconnect.
|
||||
type sharedSQLStore struct {
|
||||
once sync.Once
|
||||
mu sync.Mutex
|
||||
container *sqlstore.Container
|
||||
err error
|
||||
sqliteDB *sql.DB // kept alive when not using PostgresAuthDB
|
||||
}
|
||||
type MyClient struct {
|
||||
@@ -328,46 +326,53 @@ func (w whatsmeowService) getSharedSQLStoreContainer() (*sqlstore.Container, err
|
||||
if w.authStore == nil {
|
||||
return nil, fmt.Errorf("shared sqlstore not initialized")
|
||||
}
|
||||
w.authStore.once.Do(func() {
|
||||
var dbLog waLog.Logger
|
||||
if w.config.WaDebug != "" {
|
||||
dbLog = waLog.Stdout("Database", w.config.WaDebug, true)
|
||||
w.authStore.mu.Lock()
|
||||
defer w.authStore.mu.Unlock()
|
||||
if w.authStore.container != nil {
|
||||
return w.authStore.container, nil
|
||||
}
|
||||
return w.createSharedSQLStoreContainer()
|
||||
}
|
||||
|
||||
// Called with authStore.mu held. A failed upgrade is retried by the next caller.
|
||||
func (w whatsmeowService) createSharedSQLStoreContainer() (*sqlstore.Container, error) {
|
||||
var dbLog waLog.Logger
|
||||
if w.config.WaDebug != "" {
|
||||
dbLog = waLog.Stdout("Database", w.config.WaDebug, true)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
if w.config.PostgresAuthDB != "" {
|
||||
if w.authDB == nil {
|
||||
return nil, fmt.Errorf("postgres auth DB handle is nil")
|
||||
}
|
||||
ctx := context.Background()
|
||||
if w.config.PostgresAuthDB != "" {
|
||||
if w.authDB == nil {
|
||||
w.authStore.err = fmt.Errorf("postgres auth DB handle is nil")
|
||||
return
|
||||
}
|
||||
container := sqlstore.NewWithDB(w.authDB, "postgres", dbLog)
|
||||
if err := container.Upgrade(ctx); err != nil {
|
||||
// Do not Close authDB — owned by main.
|
||||
w.authStore.err = fmt.Errorf("failed to upgrade database: %w", err)
|
||||
return
|
||||
}
|
||||
w.authStore.container = container
|
||||
return
|
||||
}
|
||||
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 {
|
||||
w.authStore.err = fmt.Errorf("failed to open sqlite auth store: %w", err)
|
||||
return
|
||||
}
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(5)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
db.SetConnMaxIdleTime(1 * time.Minute)
|
||||
container := sqlstore.NewWithDB(db, "sqlite", dbLog)
|
||||
container := sqlstore.NewWithDB(w.authDB, "postgres", dbLog)
|
||||
if err := container.Upgrade(ctx); err != nil {
|
||||
_ = db.Close()
|
||||
w.authStore.err = fmt.Errorf("failed to upgrade database: %w", err)
|
||||
return
|
||||
// Do not Close authDB — owned by main.
|
||||
return nil, fmt.Errorf("failed to upgrade database: %w", err)
|
||||
}
|
||||
w.authStore.sqliteDB = db
|
||||
w.authStore.container = container
|
||||
})
|
||||
return w.authStore.container, w.authStore.err
|
||||
return container, nil
|
||||
}
|
||||
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 sqlite auth store: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
db.SetConnMaxIdleTime(1 * time.Minute)
|
||||
container := sqlstore.NewWithDB(db, "sqlite", dbLog)
|
||||
if err := container.Upgrade(ctx); err != nil {
|
||||
if closeErr := db.Close(); closeErr != nil {
|
||||
return nil, fmt.Errorf("failed to upgrade database: %w; close sqlite auth store: %v", err, closeErr)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to upgrade database: %w", err)
|
||||
}
|
||||
w.authStore.sqliteDB = db
|
||||
w.authStore.container = container
|
||||
return container, nil
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
Reference in new issue
Block a user