Refactor link repository and improve logging

This commit is contained in:
Andrey Chervyakov 2021-03-18 20:53:59 +06:00
parent ddaf0dcbfc
commit 458ae28901
7 changed files with 63 additions and 19 deletions

View file

@ -14,8 +14,7 @@ func main() {
conf := cgnolink.NewConfig() conf := cgnolink.NewConfig()
pool := database.Pool(conf) pool := database.InitPool(conf)
database.Migrate(pool)
server := appserver.NewServer(conf, pool) server := appserver.NewServer(conf, pool)
@ -23,5 +22,5 @@ func main() {
} }
func configureLogger() { func configureLogger() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02 15:04:05"})
} }

View file

@ -6,16 +6,34 @@ import (
"fmt" "fmt"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf" "github.com/knadh/koanf"
"github.com/rs/zerolog/log"
"time"
) )
func Pool(config *koanf.Koanf) *pgxpool.Pool { func InitPool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config) pool := NewPool(config)
if err != nil {
if err := runMigrations(pool); err != nil {
log.Fatal().Err(err).Msg("Couldn't apply migrations")
panic(err) panic(err)
} }
pool, err := pgxpool.Connect(context.Background(), connStr) return pool
}
func NewPool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Couldn't construct DB connection string")
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
pool, err := pgxpool.Connect(ctx, connStr)
if err != nil {
log.Fatal().Err(err).Msg("Couldn't connect to DB")
panic(err) panic(err)
} }

View file

@ -4,24 +4,30 @@ import (
"context" "context"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/tern/migrate" "github.com/jackc/tern/migrate"
"time"
) )
func Migrate(pool *pgxpool.Pool) { func runMigrations(pool *pgxpool.Pool) (err error) {
conn, err := pool.Acquire(context.Background()) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
conn, err := pool.Acquire(ctx)
if err != nil { if err != nil {
panic(err) return
} }
migrator, err := migrate.NewMigrator(context.Background(), conn.Conn(), "schema_version") migrator, err := migrate.NewMigrator(ctx, conn.Conn(), "schema_version")
if err != nil { if err != nil {
panic(err) return
} }
if err = migrator.LoadMigrations("./migrations"); err != nil { if err = migrator.LoadMigrations("./migrations"); err != nil {
panic(err) return
} }
if err = migrator.Migrate(context.Background()); err != nil { if err = migrator.Migrate(ctx); err != nil {
panic(err) return
} }
return
} }

15
database/util.go Normal file
View file

@ -0,0 +1,15 @@
package database
import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/rs/zerolog/log"
)
func LogPoolState(pool *pgxpool.Pool, message string) {
stat := pool.Stat()
log.Info().
Int32("acquired_conns", stat.AcquiredConns()).
Int32("idle_conns", stat.IdleConns()).
Int32("total_conns", stat.TotalConns()).
Msg(message)
}

View file

@ -1,6 +1,7 @@
package link package link
import ( import (
"cgnolink/database"
"context" "context"
"errors" "errors"
"github.com/jackc/pgtype" "github.com/jackc/pgtype"
@ -15,7 +16,7 @@ const defaultContextTimeout = 10 * time.Second
type Repository interface { type Repository interface {
Save(link *Link) error Save(link *Link) error
FindById(id string) (*Link, error) FindById(id string) (*Link, error)
FindAll(limit int, offset int) (Links, error) GetAll(limit int, offset int) (Links, error)
Update(link *Link) error Update(link *Link) error
DeleteById(id string) error DeleteById(id string) error
} }
@ -37,6 +38,7 @@ func (r *PgRepository) Save(link *Link) error {
VALUES ($1, $2, $3, $4::timestamp) VALUES ($1, $2, $3, $4::timestamp)
` `
database.LogPoolState(r.pool, "Saving link")
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05")) _, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
if err != nil { if err != nil {
return err return err
@ -55,6 +57,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
WHERE id = $1 WHERE id = $1
` `
database.LogPoolState(r.pool, "Finding link by ID")
entity, err := mapRowToEntity(r.pool.QueryRow(ctx, sql, id)) entity, err := mapRowToEntity(r.pool.QueryRow(ctx, sql, id))
if err != nil { if err != nil {
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
@ -67,7 +70,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
return entity, nil return entity, nil
} }
func (r *PgRepository) FindAll(limit int, offset int) (Links, error) { func (r *PgRepository) GetAll(limit int, offset int) (Links, error) {
if limit < 0 { if limit < 0 {
return nil, errors.New("limit can't be negative") return nil, errors.New("limit can't be negative")
} }
@ -85,6 +88,7 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
OFFSET $2 OFFSET $2
` `
database.LogPoolState(r.pool, "Getting all links")
rows, err := r.pool.Query(ctx, sql, limit, offset) rows, err := r.pool.Query(ctx, sql, limit, offset)
if err != nil { if err != nil {
return nil, err return nil, err
@ -122,6 +126,7 @@ func (r *PgRepository) Update(link *Link) error {
WHERE id = $3 WHERE id = $3
` `
database.LogPoolState(r.pool, "Updating link")
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id) _, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
if err != nil { if err != nil {
return err return err
@ -138,6 +143,7 @@ func (r *PgRepository) DeleteById(id string) error {
DELETE FROM links WHERE id = $1 DELETE FROM links WHERE id = $1
` `
database.LogPoolState(r.pool, "Deleting link")
_, err := r.pool.Exec(ctx, sql, id) _, err := r.pool.Exec(ctx, sql, id)
if err != nil { if err != nil {
return err return err

View file

@ -64,7 +64,7 @@ func (s *PgService) GetById(id string) (*Link, error) {
} }
func (s *PgService) GetAll(limit int, offset int) (Links, error) { func (s *PgService) GetAll(limit int, offset int) (Links, error) {
links, err := s.rep.FindAll(limit, offset) links, err := s.rep.GetAll(limit, offset)
if err != nil { if err != nil {
return nil, apperrors.UnknownError{Err: err} return nil, apperrors.UnknownError{Err: err}
} }

View file

@ -26,7 +26,7 @@ func Logger() echo.MiddlewareFunc {
Str("user_agent", req.UserAgent()). Str("user_agent", req.UserAgent()).
Int("status", res.Status). Int("status", res.Status).
Str("latency", stop.Sub(start).String()). Str("latency", stop.Sub(start).String()).
Send() Msg("Request")
return return
} }