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

Use context for timeout/cancel in SQL driver #636

Closed
chappjc opened this issue Aug 27, 2018 · 1 comment
Closed

Use context for timeout/cancel in SQL driver #636

chappjc opened this issue Aug 27, 2018 · 1 comment
Assignees
Milestone

Comments

@chappjc
Copy link
Member

chappjc commented Aug 27, 2018

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.

@chappjc chappjc added this to the 3.1 milestone Aug 27, 2018
@chappjc chappjc self-assigned this Aug 27, 2018
@chappjc chappjc added this to To Do in dcrdata board Aug 27, 2018
@chappjc chappjc moved this from To Do to In Progress in dcrdata board Oct 11, 2018
@chappjc chappjc modified the milestones: 3.1, 3.2 Oct 24, 2018
@chappjc
Copy link
Member Author

chappjc commented Nov 26, 2018

Initial context wiring and cancellation resolved by: #761 (made it into 3.1)
Timeouts resolved by: #792 (NOT in 3.1)

@chappjc chappjc closed this as completed Nov 26, 2018
@chappjc chappjc moved this from In Progress to Done in dcrdata board Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

1 participant