Extract context timeout for link repository into file level const
This commit is contained in:
parent
fca15187b2
commit
9fe79d3f43
1 changed files with 6 additions and 4 deletions
|
|
@ -10,6 +10,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultContextTimeout = 5 * 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)
|
||||||
|
|
@ -22,7 +24,7 @@ type PgRepository struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *PgRepository) Save(link *Link) error {
|
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()
|
defer cancel()
|
||||||
|
|
||||||
tx, err := r.pool.Begin(ctx)
|
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) {
|
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()
|
defer cancel()
|
||||||
|
|
||||||
tx, err := r.pool.Begin(ctx)
|
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")
|
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()
|
defer cancel()
|
||||||
|
|
||||||
tx, err := r.pool.Begin(ctx)
|
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 {
|
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()
|
defer cancel()
|
||||||
|
|
||||||
tx, err := r.pool.Begin(ctx)
|
tx, err := r.pool.Begin(ctx)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue