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

Support of context canceling #58

Open
meysampg opened this issue Jul 8, 2020 · 2 comments
Open

Support of context canceling #58

meysampg opened this issue Jul 8, 2020 · 2 comments

Comments

@meysampg
Copy link

meysampg commented Jul 8, 2020

Is there any plan for support of canceling with context?
This feature is implemented on other database/sql drivers like on this one for PG.

A common scenario for the usage of this feature is when a user closes the request but the query remains on running state and it will cause to occupying system resources on heavy queries.

@ryantking
Copy link
Contributor

@meysampg I have a similar use case, and this is what I'm currently doing that gets the job done, hope it helps!

func doQuery(ctx context.Context, db *sql.DB, query string) error {
    // prepend a unique identifier to the query string
    query = fmt.Sprintf("/* my-program-%s */ %s", generateID(), query)
    rows, err := db.QueryContext(ctx, query)
    if err != nil {
        // ...
    }
    defer rows.Close()

    // Grab the ID of the query from the runtime table that stores all active running queries
    // This is why we used the unique identifier, so that way two queries with the same SQL won't
    // collide on this table.
    var queryID string
    if err := db.QueryRow("SELECT query_id FROM system.runtime.queries WHERE query = ?', query).Scan(&queryID); err != nil {
    // ...
    }
    for rows.Next() {
        // do what you will
    }
    // Handle the error returned from context cancellation and deadline expiration separately
    // from normal errors so we can run the cancel logic.
    // Can even handle them separately if you'd like.
    if err := rows.Err(); err == context.Canceled || err == context.DeadlineExceeded {
        killQuery := fmt.Sprintf("CALL system.runtime.kill_query('%s')", queryID)
        if err := db.Exec(killQuery); err != nil {
            // something went wrong cancelling
            return err
        }
     } else if err != nil {
         // all other errors
        return err
     }

     return nil
}

@meysampg
Copy link
Author

meysampg commented Sep 1, 2020

@ryantking Brilliant! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants