postgres-switch #113

Merged
lash merged 14 commits from postgres-switch into master 2024-10-22 16:04:56 +02:00
2 changed files with 47 additions and 88 deletions
Showing only changes of commit f13f5996c1 - Show all commits

View File

@ -8,27 +8,18 @@ import (
"git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/lang"
) )
var ( type PostgresDb struct {
pdbC map[string]chan db.Db
)
type ThreadPostgresDb struct {
db db.Db db db.Db
connStr string connStr string
} }
func NewThreadPostgresDb() *ThreadPostgresDb { func NewPostgresDb() *PostgresDb {
if pdbC == nil { return &PostgresDb{}
pdbC = make(map[string]chan db.Db)
}
return &ThreadPostgresDb{}
} }
func (tpdb *ThreadPostgresDb) Connect(ctx context.Context, connStr string) error { func (pdb *PostgresDb) Connect(ctx context.Context, connStr string) error {
var ok bool if pdb.db != nil {
_, ok = pdbC[connStr] logg.WarnCtxf(ctx, "already connected, skipping", "connStr", connStr)
if ok {
logg.WarnCtxf(ctx, "already registered thread postgres, skipping", "connStr", connStr)
return nil return nil
} }
postgresdb := postgres.NewPgDb().WithSchema("public") postgresdb := postgres.NewPgDb().WithSchema("public")
@ -36,80 +27,48 @@ func (tpdb *ThreadPostgresDb) Connect(ctx context.Context, connStr string) error
if err != nil { if err != nil {
return err return err
} }
pdbC[connStr] = make(chan db.Db, 1) pdb.db = postgresdb
pdbC[connStr] <- postgresdb pdb.connStr = connStr
tpdb.connStr = connStr
return nil return nil
} }
func (tpdb *ThreadPostgresDb) reserve() { func (pdb *PostgresDb) SetPrefix(pfx uint8) {
if tpdb.db == nil { pdb.db.SetPrefix(pfx)
tpdb.db = <-pdbC[tpdb.connStr]
}
} }
func (tpdb *ThreadPostgresDb) release() { func (pdb *PostgresDb) SetSession(sessionId string) {
if tpdb.db == nil { pdb.db.SetSession(sessionId)
return
}
pdbC[tpdb.connStr] <- tpdb.db
tpdb.db = nil
} }
func (tpdb *ThreadPostgresDb) SetPrefix(pfx uint8) { func (pdb *PostgresDb) SetLanguage(lng *lang.Language) {
tpdb.reserve() pdb.db.SetLanguage(lng)
tpdb.db.SetPrefix(pfx)
} }
func (tpdb *ThreadPostgresDb) SetSession(sessionId string) { func (pdb *PostgresDb) Safe() bool {
tpdb.reserve() return pdb.db.Safe()
tpdb.db.SetSession(sessionId)
} }
func (tpdb *ThreadPostgresDb) SetLanguage(lng *lang.Language) { func (pdb *PostgresDb) Prefix() uint8 {
tpdb.reserve() return pdb.db.Prefix()
tpdb.db.SetLanguage(lng)
} }
func (tpdb *ThreadPostgresDb) Safe() bool { func (pdb *PostgresDb) SetLock(typ uint8, locked bool) error {
tpdb.reserve() return pdb.db.SetLock(typ, locked)
v := tpdb.db.Safe()
tpdb.release()
return v
} }
func (tpdb *ThreadPostgresDb) Prefix() uint8 { func (pdb *PostgresDb) Put(ctx context.Context, key []byte, val []byte) error {
tpdb.reserve() return pdb.db.Put(ctx, key, val)
v := tpdb.db.Prefix()
tpdb.release()
return v
} }
func (tpdb *ThreadPostgresDb) SetLock(typ uint8, locked bool) error { func (pdb *PostgresDb) Get(ctx context.Context, key []byte) ([]byte, error) {
tpdb.reserve() return pdb.db.Get(ctx, key)
err := tpdb.db.SetLock(typ, locked) }
tpdb.release()
return err func (pdb *PostgresDb) Close() error {
} if pdb.db == nil {
return nil
func (tpdb *ThreadPostgresDb) Put(ctx context.Context, key []byte, val []byte) error { }
tpdb.reserve() err := pdb.db.Close()
err := tpdb.db.Put(ctx, key, val) pdb.db = nil
tpdb.release()
return err
}
func (tpdb *ThreadPostgresDb) Get(ctx context.Context, key []byte) ([]byte, error) {
tpdb.reserve()
v, err := tpdb.db.Get(ctx, key)
tpdb.release()
return v, err
}
func (tpdb *ThreadPostgresDb) Close() error {
tpdb.reserve()
close(pdbC[tpdb.connStr])
err := tpdb.db.Close()
tpdb.db = nil
return err return err
} }

View File

@ -72,7 +72,7 @@ func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error)
} }
if database == "postgres" { if database == "postgres" {
ms.userDataStore = NewThreadPostgresDb() ms.userDataStore = NewPostgresDb()
connStr := buildConnStr() connStr := buildConnStr()
err := ms.userDataStore.Connect(ctx, connStr) err := ms.userDataStore.Connect(ctx, connStr)
if err != nil { if err != nil {