-
Type:
Question
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Go Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Description
Investigate the possibility of adding a SessionManager (see "Definition of Done" for example) to the mongo package to reuse session across multiple operations without involving transactions. That is, we want to create wrappers for something like the following type of logic:
// Start a session with causal consistency enabled sessionOpts := options.Session().SetCausalConsistency(true) session, err := client.StartSession(sessionOpts) if err != nil { log.Fatalf("Failed to start session: %v", err) } defer session.EndSession(ctx) // Store session in a shared context for reuse across calls sessionCtx := mongo.NewSessionContext(ctx, session) collection := client.Database("test").Collection("example") _, err = collection.InsertOne(sessionCtx, map[string]interface{}{"name": "Alice"}) if err != nil { log.Fatalf("Insert failed: %v", err) } _, err = collection.InsertOne(sessionCtx, map[string]interface{}{"name": "Bob"}) if err != nil { log.Fatalf("Insert failed: %v", err) }
Key Questions:
- Why has this not been implemented already?
- Do other MongoDB drivers offer similar functionality?
- What are the potential costs or challenges associated with managing sessions this way?
- What ways can we develop this API for best usability and simplicity? One solution is provided in the "Definition of Done" section.
Definition of Done
Here is a possible API:
// SessionManager manages MongoDB sessions for multiple operations. type SessionManager struct { client *mongo.Client session mongo.Session } // NewSessionManager initializes a new session manager. func NewSessionManager(*mongo.Client) (*SessionManager, error) {} // UseSession executes a callback function with the session context. func (sm *SessionManager) UseSession(context.Context, func(context.Context) error) error {} // EndSession cleans up the session. func (sm *SessionManager) EndSession(context.Context) {}
With the following use case:
// Create a new session manager sessionManager, _ := mongo.NewSessionManager(client) defer sessionManager.EndSession(ctx) // Use the session manager to perform multiple operations err = sessionManager.UseSession(ctx, func(sessCtx context.Context) error { // ... })
- related to
-
DRIVERS-2860 Introduce new API for starting a causally consistent session from the timestamps of another operation or client session
-
- Needs Triage
-