Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

postgres: use sql db context functions #635

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions internal/postgresql/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,11 @@ var (
)

func dbExec(ctx context.Context, db *sql.DB, query string, args ...interface{}) (sql.Result, error) {
ch := make(chan struct {
res sql.Result
err error
})
go func() {
res, err := db.Exec(query, args...)
ch <- struct {
res sql.Result
err error
}{res, err}
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case out := <-ch:
return out.res, out.err
}
return db.ExecContext(ctx, query, args...)
}

func query(ctx context.Context, db *sql.DB, query string, args ...interface{}) (*sql.Rows, error) {
ch := make(chan struct {
rows *sql.Rows
err error
})
go func() {
rows, err := db.Query(query, args...)
ch <- struct {
rows *sql.Rows
err error
}{rows, err}
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case out := <-ch:
return out.rows, out.err
}
return db.QueryContext(ctx, query, args...)
}

func ping(ctx context.Context, connParams ConnParams) error {
Expand Down