service.go

Overview

service.go implements the Database Session Service, a persistent, transactional backend for managing user-agent interaction sessions within a relational database. It fulfills the session.Service interface and uses the GORM ORM to connect to SQL databases such as PostgreSQL, Spanner, or SQLite.

This service handles creation, retrieval, listing, deletion, and event appending for sessions with full transactional guarantees. It supports merging and isolating state across three namespaces — application, user, and session — while filtering out transient state keys. The file also manages event histories associated with sessions, ensuring data consistency and concurrency control through timestamp checks.

This database-backed implementation complements the Session Management topic by providing durable session storage suitable for production and scalable deployments.


Main Types and Structures

databaseService


Core Functions and Methods

NewSessionService

func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.Service, error)

AutoMigrate

func AutoMigrate(service session.Service) error

(*databaseService) Create

func (s *databaseService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error)
resp, err := dbService.Create(ctx, &session.CreateRequest{
    AppName: "myapp",
    UserID:  "user123",
    State:   map[string]any{"key": "value"},
})
if err != nil {
    // handle error
}
createdSession := resp.Session

(*databaseService) Get

func (s *databaseService) Get(ctx context.Context, req *session.GetRequest) (*session.GetResponse, error)

(*databaseService) List

func (s *databaseService) List(ctx context.Context, req *session.ListRequest) (*session.ListResponse, error)

(*databaseService) Delete

func (s *databaseService) Delete(ctx context.Context, req *session.DeleteRequest) error

(*databaseService) AppendEvent

func (s *databaseService) AppendEvent(ctx context.Context, curSession session.Session, event *session.Event) error
err := dbService.AppendEvent(ctx, currentSession, newEvent)
if err != nil {
    // handle error
}

applyEvent (private)

func (s *databaseService) applyEvent(ctx context.Context, session *localSession, event *session.Event) error

Helper Functions

fetchStorageAppState

fetchStorageUserState

fetchAllAppStorageUserState


extractStateDeltas

func extractStateDeltas(delta map[string]any) (appStateDelta, userStateDelta, sessionStateDelta map[string]any)

mergeStates

func mergeStates(appState, userState, sessionState map[string]any) map[string]any

Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
class databaseService {
-db : *gorm.DB
+Create(ctx, req)
+Get(ctx, req)
+List(ctx, req)
+Delete(ctx, req)
+AppendEvent(ctx, session, event)
-applyEvent(ctx, session, event)
}
class storageSession {
+AppName
+UserID
+ID
+State
+UpdateTime
}
class storageEvent {
+AppName
+UserID
+SessionID
+Timestamp
+Actions
+Partial
}
class storageAppState {
+AppName
+State
}
class storageUserState {
+AppName
+UserID
+State
}
databaseService o-- storageSession : uses
databaseService o-- storageEvent : uses
databaseService o-- storageAppState : uses
databaseService o-- storageUserState : uses

This class diagram illustrates the databaseService struct as the core database session service implementation. It interacts with four primary storage models representing sessions, events, application state, and user state. The methods correspond to session lifecycle operations, including event appending and transactional state management.


References