Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix traces in queryRowWithTimeout #77

Merged
merged 1 commit into from
Jan 31, 2024
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
20 changes: 14 additions & 6 deletions internal/mysql/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,15 @@ func (n *Node) queryRow(queryName string, arg interface{}, result interface{}) e
}

func (n *Node) queryRowWithTimeout(queryName string, arg interface{}, result interface{}, timeout time.Duration) error {
return n.processQuery(queryName, arg, func(rows *sqlx.Rows) error {
var err error

if arg == nil {
arg = struct{}{}
}
query := n.getQuery(queryName)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
rows, err := n.db.NamedQueryContext(ctx, query, arg)
if err == nil {
defer func() { _ = rows.Close() }()
if rows.Next() {
err = rows.StructScan(result)
} else {
Expand All @@ -169,13 +175,15 @@ func (n *Node) queryRowWithTimeout(queryName string, arg interface{}, result int
err = sql.ErrNoRows
}
}

return err
}, timeout)
}
n.traceQuery(query, arg, result, err)
return err
}

// nolint: unparam
func (n *Node) queryRows(queryName string, arg interface{}, scanner func(*sqlx.Rows) error) error {
// TODO we need to rewrite processQuery, to make traceQuery work properly
// traceQuery should be called with result, not *Rows
return n.processQuery(queryName, arg, func(rows *sqlx.Rows) error {
var err error

Expand Down
Loading