Fix timeout in ReadSchema/GetMeta etc method (fixes /health call taking 2 seconds)

This commit is contained in:
Mike Schwörer 2022-11-30 22:59:33 +01:00
parent 7eab74e65c
commit fd182f0abb
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 85 additions and 24 deletions

View File

@ -6,12 +6,20 @@ import (
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
) )
func (db *Database) ReadSchema(ctx context.Context) (int, error) { func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) {
r1, err := db.db.QueryContext(ctx, "SELECT name FROM sqlite_master WHERE type='table' AND name='meta'") r1, err := db.db.QueryContext(ctx, "SELECT name FROM sqlite_master WHERE type='table' AND name='meta'")
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func() {
err = r1.Close()
if err != nil {
// overwrite return values
retval = 0
reterr = err
}
}()
if !r1.Next() { if !r1.Next() {
return 0, nil return 0, nil
@ -21,6 +29,15 @@ func (db *Database) ReadSchema(ctx context.Context) (int, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer func() {
err = r2.Close()
if err != nil {
// overwrite return values
retval = 0
reterr = err
}
}()
if !r2.Next() { if !r2.Next() {
return 0, errors.New("no schema entry in meta table") return 0, errors.New("no schema entry in meta table")
} }
@ -78,11 +95,19 @@ func (db *Database) WriteMetaBlob(ctx context.Context, key string, value []byte)
return nil return nil
} }
func (db *Database) ReadMetaString(ctx context.Context, key string) (*string, error) { func (db *Database) ReadMetaString(ctx context.Context, key string) (retval *string, reterr error) {
r2, err := db.db.QueryContext(ctx, "SELECT value_txt FROM meta WHERE meta_key=?", key) r2, err := db.db.QueryContext(ctx, "SELECT value_txt FROM meta WHERE meta_key=?", key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() {
err = r2.Close()
if err != nil {
// overwrite return values
retval = nil
reterr = err
}
}()
if !r2.Next() { if !r2.Next() {
return nil, errors.New("no matching entry in meta table") return nil, errors.New("no matching entry in meta table")
} }
@ -96,11 +121,20 @@ func (db *Database) ReadMetaString(ctx context.Context, key string) (*string, er
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
func (db *Database) ReadMetaInt(ctx context.Context, key string) (*int64, error) { func (db *Database) ReadMetaInt(ctx context.Context, key string) (retval *int64, reterr error) {
r2, err := db.db.QueryContext(ctx, "SELECT value_int FROM meta WHERE meta_key=?", key) r2, err := db.db.QueryContext(ctx, "SELECT value_int FROM meta WHERE meta_key=?", key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() {
err = r2.Close()
if err != nil {
// overwrite return values
retval = nil
reterr = err
}
}()
if !r2.Next() { if !r2.Next() {
return nil, errors.New("no matching entry in meta table") return nil, errors.New("no matching entry in meta table")
} }
@ -114,11 +148,20 @@ func (db *Database) ReadMetaInt(ctx context.Context, key string) (*int64, error)
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
func (db *Database) ReadMetaReal(ctx context.Context, key string) (*float64, error) { func (db *Database) ReadMetaReal(ctx context.Context, key string) (retval *float64, reterr error) {
r2, err := db.db.QueryContext(ctx, "SELECT value_real FROM meta WHERE meta_key=?", key) r2, err := db.db.QueryContext(ctx, "SELECT value_real FROM meta WHERE meta_key=?", key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() {
err = r2.Close()
if err != nil {
// overwrite return values
retval = nil
reterr = err
}
}()
if !r2.Next() { if !r2.Next() {
return nil, errors.New("no matching entry in meta table") return nil, errors.New("no matching entry in meta table")
} }
@ -132,11 +175,20 @@ func (db *Database) ReadMetaReal(ctx context.Context, key string) (*float64, err
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
func (db *Database) ReadMetaBlob(ctx context.Context, key string) (*[]byte, error) { func (db *Database) ReadMetaBlob(ctx context.Context, key string) (retval *[]byte, reterr error) {
r2, err := db.db.QueryContext(ctx, "SELECT value_blob FROM meta WHERE meta_key=?", key) r2, err := db.db.QueryContext(ctx, "SELECT value_blob FROM meta WHERE meta_key=?", key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() {
err = r2.Close()
if err != nil {
// overwrite return values
retval = nil
reterr = err
}
}()
if !r2.Next() { if !r2.Next() {
return nil, errors.New("no matching entry in meta table") return nil, errors.New("no matching entry in meta table")
} }

27
server/test/util/init.go Normal file
View File

@ -0,0 +1,27 @@
package util
import (
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
)
func InitTests() {
cw := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05.000 Z07:00",
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
multi := zerolog.MultiLevelWriter(cw)
logger := zerolog.New(multi).With().
Timestamp().
Caller().
Logger()
log.Logger = logger
gin.SetMode(gin.TestMode)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

View File

@ -10,9 +10,6 @@ import (
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/push" "blackforestbytes.com/simplecloudnotifier/push"
"fmt" "fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"os" "os"
"path/filepath" "path/filepath"
@ -23,22 +20,7 @@ import (
type Void = struct{} type Void = struct{}
func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) { func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) {
cw := zerolog.ConsoleWriter{ InitTests()
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05 Z07:00",
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
multi := zerolog.MultiLevelWriter(cw)
logger := zerolog.New(multi).With().
Timestamp().
Caller().
Logger()
log.Logger = logger
gin.SetMode(gin.TestMode)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
uuid2, _ := langext.NewHexUUID() uuid2, _ := langext.NewHexUUID()
dbdir := t.TempDir() dbdir := t.TempDir()