From fc650b2d2add8cc8c678245dc3184e68881ae7b4 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Wed, 8 Sep 2021 18:50:33 +0300 Subject: [PATCH] fix: postgres connection freezes --- repositories/files_repository.go | 8 +------- repositories/posts_repository.go | 28 +++++----------------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/repositories/files_repository.go b/repositories/files_repository.go index 77a6d62..1ca0334 100644 --- a/repositories/files_repository.go +++ b/repositories/files_repository.go @@ -37,12 +37,6 @@ func (r *FilesRepository) Create(f File) error { } func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { - conn, err := Db.Pool.Acquire(context.TODO()) - if err != nil { - return nil, err - } - defer conn.Release() - sql := ` SELECT post_id, name, size FROM files @@ -53,7 +47,7 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { ids := &pgtype.Int4Array{} ids.Set(postIDs) - rows, err := conn.Query(context.TODO(), sql, ids) + rows, err := Db.Pool.Query(context.TODO(), sql, ids) if err != nil { return nil, err } diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index 829c325..013d584 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -12,12 +12,6 @@ type PostsRepository struct{} var Posts PostsRepository func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { - conn, err := Db.Pool.Acquire(context.TODO()) - if err != nil { - return nil, err - } - defer conn.Release() - sql := ` SELECT id, title, text, created_at FROM posts @@ -28,7 +22,7 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { LIMIT $1 ` - rows, err := conn.Query(context.TODO(), sql, limit) + rows, err := Db.Pool.Query(context.TODO(), sql, limit) if err != nil { return nil, err } @@ -65,12 +59,6 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { } func (r *PostsRepository) Create(p Post) (int, error) { - conn, err := Db.Pool.Acquire(context.TODO()) - if err != nil { - return 0, err - } - defer conn.Release() - sql := ` INSERT INTO posts (is_parent, parent_id, title, text, is_sage) VALUES ($1, $2, $3, $4, $5) @@ -79,17 +67,17 @@ func (r *PostsRepository) Create(p Post) (int, error) { var row pgx.Row if p.IsParent { - row = conn.QueryRow( + row = Db.Pool.QueryRow( context.TODO(), sql, p.IsParent, nil, p.Title, p.Text, p.IsSage, ) } else { - row = conn.QueryRow( + row = Db.Pool.QueryRow( context.TODO(), sql, p.IsParent, p.ParentID, p.Title, p.Text, p.IsSage, ) } createdPost := new(Post) - err = row.Scan(&createdPost.ID) + err := row.Scan(&createdPost.ID) if err != nil { return 0, err } @@ -98,12 +86,6 @@ func (r *PostsRepository) Create(p Post) (int, error) { } func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { - conn, err := Db.Pool.Acquire(context.TODO()) - if err != nil { - return nil, err - } - defer conn.Release() - sql := ` SELECT id, @@ -119,7 +101,7 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { ORDER BY created_at ASC ` - rows, err := conn.Query(context.TODO(), sql, ID) + rows, err := Db.Pool.Query(context.TODO(), sql, ID) if err != nil { return nil, err }