feat(backend): pg-connector use Temeout context

This commit is contained in:
ShiKhu 2022-02-04 23:12:33 +01:00
parent 7f14269671
commit ca870106e2

View file

@ -8,6 +8,11 @@ import (
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
) )
func getTimeoutContext() context.Context {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(time.Second*10))
return ctx
}
type Conn struct { type Conn struct {
c *pgxpool.Pool // TODO: conditional usage of Pool/Conn (use interface?) c *pgxpool.Pool // TODO: conditional usage of Pool/Conn (use interface?)
} }
@ -26,15 +31,15 @@ func (conn *Conn) Close() error {
} }
func (conn *Conn) query(sql string, args ...interface{}) (pgx.Rows, error) { func (conn *Conn) query(sql string, args ...interface{}) (pgx.Rows, error) {
return conn.c.Query(context.Background(), sql, args...) return conn.c.Query(getTimeoutContext(), sql, args...)
} }
func (conn *Conn) queryRow(sql string, args ...interface{}) pgx.Row { func (conn *Conn) queryRow(sql string, args ...interface{}) pgx.Row {
return conn.c.QueryRow(context.Background(), sql, args...) return conn.c.QueryRow(getTimeoutContext(), sql, args...)
} }
func (conn *Conn) exec(sql string, args ...interface{}) error { func (conn *Conn) exec(sql string, args ...interface{}) error {
_, err := conn.c.Exec(context.Background(), sql, args...) _, err := conn.c.Exec(getTimeoutContext(), sql, args...)
return err return err
} }