fix: postgres connection freezes

This commit is contained in:
Yanislav Igonin 2021-09-08 18:50:33 +03:00
parent 81de0e69b7
commit fc650b2d2a
2 changed files with 6 additions and 30 deletions

View File

@ -37,12 +37,6 @@ func (r *FilesRepository) Create(f File) error {
} }
func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]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 := ` sql := `
SELECT post_id, name, size SELECT post_id, name, size
FROM files FROM files
@ -53,7 +47,7 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) {
ids := &pgtype.Int4Array{} ids := &pgtype.Int4Array{}
ids.Set(postIDs) ids.Set(postIDs)
rows, err := conn.Query(context.TODO(), sql, ids) rows, err := Db.Pool.Query(context.TODO(), sql, ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -12,12 +12,6 @@ type PostsRepository struct{}
var Posts PostsRepository var Posts PostsRepository
func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { 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 := ` sql := `
SELECT id, title, text, created_at SELECT id, title, text, created_at
FROM posts FROM posts
@ -28,7 +22,7 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
LIMIT $1 LIMIT $1
` `
rows, err := conn.Query(context.TODO(), sql, limit) rows, err := Db.Pool.Query(context.TODO(), sql, limit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -65,12 +59,6 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
} }
func (r *PostsRepository) Create(p Post) (int, 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 := ` sql := `
INSERT INTO posts (is_parent, parent_id, title, text, is_sage) INSERT INTO posts (is_parent, parent_id, title, text, is_sage)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5)
@ -79,17 +67,17 @@ func (r *PostsRepository) Create(p Post) (int, error) {
var row pgx.Row var row pgx.Row
if p.IsParent { if p.IsParent {
row = conn.QueryRow( row = Db.Pool.QueryRow(
context.TODO(), sql, p.IsParent, nil, p.Title, p.Text, p.IsSage, context.TODO(), sql, p.IsParent, nil, p.Title, p.Text, p.IsSage,
) )
} else { } else {
row = conn.QueryRow( row = Db.Pool.QueryRow(
context.TODO(), sql, p.IsParent, p.ParentID, p.Title, p.Text, p.IsSage, context.TODO(), sql, p.IsParent, p.ParentID, p.Title, p.Text, p.IsSage,
) )
} }
createdPost := new(Post) createdPost := new(Post)
err = row.Scan(&createdPost.ID) err := row.Scan(&createdPost.ID)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -98,12 +86,6 @@ func (r *PostsRepository) Create(p Post) (int, error) {
} }
func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, 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 := ` sql := `
SELECT SELECT
id, id,
@ -119,7 +101,7 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) {
ORDER BY created_at ASC 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 { if err != nil {
return nil, err return nil, err
} }