Skip to content

Use context for timeout/cancel in SQL driver #636

@chappjc

Description

@chappjc

To prevent queries from piling up when the server is overloaded, they should be cancelled if they run too long. The ability to cancel queries is also desirable.

The database/sql package supports using context.Context for query cancellation and timeout (QueryContext, QueryRowContext, ExecContext, etc): https://docs.google.com/document/d/1F778e7ZSNiSmbju3jsEWzShcb8lIO4kDyfKDNm4PNd8/edit

The PostgreSQL driver we use (github.com/lib/pq) supports these functions too: lib/pq#535. However it looks like there is some left to implement: lib/pq#768

More background: The context package added in go1.7 is used to convey request-scoped data like deadlines and cancel signals. For the http handlers in our explorer and api packages, we use WithValue extensively. For the db packages, we'll be instrumenting with WithCancel, WithDeadline, and WithTimeout, which each return a CancelFunc that is called to cancel the context and all child contexts. The interrupt signal handling should make use of this.

For our db queries, using WithTimeout is likely to be more natural than WithDeadline, which is called by WithTimeout. We should create a context for each new query, using a timeout that should be user-configurable via the dcrdata config. The context will be passed to the query function (e.g. ExecContext), which will cancel the query if the time exceeds the timeout.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions