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"
)
var (
pdbC map[string]chan db.Db
)
type ThreadPostgresDb struct {
type PostgresDb struct {
db db.Db
connStr string
}
Alfred-mk marked this conversation as resolved Outdated
Outdated
Review

i dont think you need the thread abstraction here, because the pg implementation used is already a managed connection pool. did you try it without?

i dont think you need the thread abstraction here, because the pg implementation used is already a managed connection pool. did you try it without?
func NewThreadPostgresDb() *ThreadPostgresDb {
if pdbC == nil {
pdbC = make(map[string]chan db.Db)
}
return &ThreadPostgresDb{}
func NewPostgresDb() *PostgresDb {
return &PostgresDb{}
}
func (tpdb *ThreadPostgresDb) Connect(ctx context.Context, connStr string) error {
var ok bool
_, ok = pdbC[connStr]
if ok {
logg.WarnCtxf(ctx, "already registered thread postgres, skipping", "connStr", connStr)
func (pdb *PostgresDb) Connect(ctx context.Context, connStr string) error {
if pdb.db != nil {
logg.WarnCtxf(ctx, "already connected, skipping", "connStr", connStr)
return nil
}
postgresdb := postgres.NewPgDb().WithSchema("public")
@ -36,80 +27,48 @@ func (tpdb *ThreadPostgresDb) Connect(ctx context.Context, connStr string) error
if err != nil {
return err
}
pdbC[connStr] = make(chan db.Db, 1)
pdbC[connStr] <- postgresdb
tpdb.connStr = connStr
pdb.db = postgresdb
pdb.connStr = connStr
return nil
}
func (tpdb *ThreadPostgresDb) reserve() {
if tpdb.db == nil {
tpdb.db = <-pdbC[tpdb.connStr]
}
func (pdb *PostgresDb) SetPrefix(pfx uint8) {
pdb.db.SetPrefix(pfx)
}
func (tpdb *ThreadPostgresDb) release() {
if tpdb.db == nil {
return
}
pdbC[tpdb.connStr] <- tpdb.db
tpdb.db = nil
func (pdb *PostgresDb) SetSession(sessionId string) {
pdb.db.SetSession(sessionId)
}
func (tpdb *ThreadPostgresDb) SetPrefix(pfx uint8) {
tpdb.reserve()
tpdb.db.SetPrefix(pfx)
func (pdb *PostgresDb) SetLanguage(lng *lang.Language) {
pdb.db.SetLanguage(lng)
}
func (tpdb *ThreadPostgresDb) SetSession(sessionId string) {
tpdb.reserve()
tpdb.db.SetSession(sessionId)
func (pdb *PostgresDb) Safe() bool {
return pdb.db.Safe()
}
func (tpdb *ThreadPostgresDb) SetLanguage(lng *lang.Language) {
tpdb.reserve()
tpdb.db.SetLanguage(lng)
func (pdb *PostgresDb) Prefix() uint8 {
return pdb.db.Prefix()
}
func (tpdb *ThreadPostgresDb) Safe() bool {
tpdb.reserve()
v := tpdb.db.Safe()
tpdb.release()
return v
func (pdb *PostgresDb) SetLock(typ uint8, locked bool) error {
return pdb.db.SetLock(typ, locked)
}
func (tpdb *ThreadPostgresDb) Prefix() uint8 {
tpdb.reserve()
v := tpdb.db.Prefix()
tpdb.release()
return v
func (pdb *PostgresDb) Put(ctx context.Context, key []byte, val []byte) error {
return pdb.db.Put(ctx, key, val)
}
func (tpdb *ThreadPostgresDb) SetLock(typ uint8, locked bool) error {
tpdb.reserve()
err := tpdb.db.SetLock(typ, locked)
tpdb.release()
return err
}
func (tpdb *ThreadPostgresDb) Put(ctx context.Context, key []byte, val []byte) error {
tpdb.reserve()
err := tpdb.db.Put(ctx, key, val)
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
func (pdb *PostgresDb) Get(ctx context.Context, key []byte) ([]byte, error) {
return pdb.db.Get(ctx, key)
}
func (pdb *PostgresDb) Close() error {
if pdb.db == nil {
return nil
}
err := pdb.db.Close()
pdb.db = nil
return err
}

View File

@ -72,7 +72,7 @@ func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error)
}
if database == "postgres" {
Alfred-mk marked this conversation as resolved Outdated
Outdated
Review

postgres shouold be implemented for both userdatatstore and state store

postgres shouold be implemented for both userdatatstore and state store
ms.userDataStore = NewThreadPostgresDb()
ms.userDataStore = NewPostgresDb()
connStr := buildConnStr()
err := ms.userDataStore.Connect(ctx, connStr)
if err != nil {