From 9fe79d3f43ed0c40a9897f03347c8302056f831c Mon Sep 17 00:00:00 2001 From: Andrey Chervyakov Date: Mon, 15 Mar 2021 01:26:34 +0600 Subject: [PATCH] Extract context timeout for link repository into file level const --- link/repository.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/link/repository.go b/link/repository.go index 878042e..f07792e 100644 --- a/link/repository.go +++ b/link/repository.go @@ -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)