Investigate adding a session management solution for reuse across multiple operations

XMLWordPrintableJSON

    • Type: Question
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Go Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • 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:

      1. Why has this not been implemented already?
      2. Do other MongoDB drivers offer similar functionality?
      3. What are the potential costs or challenges associated with managing sessions this way?
      4. 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 {
          // ...
      })
      

            Assignee:
            Qingyang Hu
            Reporter:
            Preston Vasquez
            None
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:

                Estimated:
                Original Estimate - 8 weeks
                8w
                Remaining:
                Remaining Estimate - 8 weeks
                8w
                Logged:
                Time Spent - Not Specified
                Not Specified