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

Add context.Context support #72

Closed
gwvandesteeg opened this issue Dec 6, 2019 · 6 comments
Closed

Add context.Context support #72

gwvandesteeg opened this issue Dec 6, 2019 · 6 comments

Comments

@gwvandesteeg
Copy link

Add support on transactions and queries to use the standard context.Context objects to determine timeouts, deadlines, and cancellations for a request.

For example, expanding the Session type with a

RunWithContext(ctx context.Context, cyper string, params map[string]interface{}, configurers ...func(*TransactinConfig)) (Result, error)

When building a microservice such as a REST API that needs to do a Cypher query, being able to pass in the received requests context to the query to handle cancellations and graceful shutdowns properly is really a must have to avoid leaving goroutines deadlocked in the background.

A sample use would be

import (
        "http"
	n4jdriver "github.com/neo4j/neo4j-go-driver/neo4j"
)

type Neo4JConnection struct {
  Driver n4jdriver.Driver
}

func (conn Neo4JConnection) GetUser(w http.ResponseWriter, r *http.Request) {
        var result n4jdriver.Result

        guid, ok  := r.URL.Query()["guid"]
        if !ok {
                // handle the lack of guid
        }
	session, err := conn.Driver.Session(n4jdriver.AccessModeWrite)
	if err != nil {
		// handle the error
	}
	defer session.Close()

        dataset := map[string]interface{}{
            guid: guid,
        } 

        result, err := session.RunWithContext(r.Context(), `
MATCH (u:User {guid: $guid}) RETURN u.guid, u.fullname
`, dataset)
    if err != nil {
        // handle the error, which could be a context timeout or deadline exceeded or a cancellation
    }
    // do something with the data and return it to the end user

}
@2hdddg
Copy link
Contributor

2hdddg commented Apr 1, 2020

@gwvandesteeg this is a good suggestion. The 1.7 driver is currently reworked to be pure Go (v1.8) which would make the implementation of context better (context can go a lot deeper into the internals).

I'm not sure if we want to add an API for providing context on 1.8 driver but the underlying implementations will more or less use contexts. I'm putting this into the list of requested features for the 4.0 driver (version scheme now follows server versions).

@2hdddg 2hdddg closed this as completed Apr 1, 2020
@damianopetrungaro
Copy link

@2hdddg do you need help on this?
I need to make context management around all the session to make sure it won't block longer than the context deadline.

@arjantop-cai
Copy link

@2hdddg Any plans for this? As I see it did not make it in the 4.x version

@lukestoward
Copy link

Hi @2hdddg, i'm guessing there still is no support for context?

@fbiville
Copy link
Contributor

fbiville commented Feb 22, 2022

Hello, current Go driver maintainer here (Peter moved to Kernel a year ago).
There are now context-aware APIs in the 5.0 branch. It's about to be released in preview mode this week.

@fbiville
Copy link
Contributor

fbiville commented Feb 23, 2022

Hey everyone, the experimental, pre-release 5.0.0-preview is out and includes these context-aware APIs.
You can give it a... go 🥁: https://github.com/neo4j/neo4j-go-driver/releases/tag/v5.0.0-preview

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

6 participants