Extract context timeout for link repository into file level const

This commit is contained in:
Andrey Chervyakov 2021-03-15 01:26:34 +06:00
parent fca15187b2
commit 9fe79d3f43

View file

@ -10,6 +10,8 @@ import (
"time"
)
const defaultContextTimeout = 5 * time.Second
type Repository interface {
Save(link *Link) error
FindById(id string) (*Link, error)
@ -22,7 +24,7 @@ type PgRepository struct {
}
func (r *PgRepository) Save(link *Link) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
tx, err := r.pool.Begin(ctx)
@ -49,7 +51,7 @@ func (r *PgRepository) Save(link *Link) error {
}
func (r *PgRepository) FindById(id string) (*Link, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
tx, err := r.pool.Begin(ctx)
@ -88,7 +90,7 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
return nil, errors.New("offset can't be negative")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
tx, err := r.pool.Begin(ctx)
@ -134,7 +136,7 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
}
func (r *PgRepository) DeleteById(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
defer cancel()
tx, err := r.pool.Begin(ctx)