session.go

Overview

The session.go file within the database package implements an in-memory representation of a session for the database-backed session service. It provides a concrete local session implementation that manages session metadata, event history, and session-scoped mutable state with concurrent access control. This file encapsulates the core data structures and logic to maintain a session's state and events in memory, facilitating transactional updates and querying within the database session service context.

Although the file is part of the database session backend, it manages session state and events in memory with synchronization primitives. It also handles filtering out temporary state keys from events and updating session state based on incoming event state deltas. This local session implementation conforms to interfaces defined in the Session Management topic, serving as an internal representation of sessions managed by the database-backed session service.


Types and Structures

localSession

A struct representing a single user session in memory, with fields and methods managing session data and concurrency.


Methods on localSession

ID() string

Returns the session's unique identifier (sessionID).

AppName() string

Returns the name of the application associated with the session.

UserID() string

Returns the user ID associated with the session.

State() session.State

Returns a session.State interface wrapping the session's state map guarded by the session mutex.

Events() session.Events

Returns a session.Events interface wrapping the internal slice of session events.

LastUpdateTime() time.Time

Returns the timestamp of the last update to the session.

appendEvent(event *session.Event) error

Appends a new event to the session's event history and updates the session state accordingly.


Supporting Types and Methods

events

A slice type alias for []*session.Event implementing the session.Events interface.

state

Wraps a session state map and mutex, implementing the session.State interface.


Key Functions

trimTempDeltaState(event *session.Event) *session.Event

Filters out temporary state delta keys from an event’s state delta map.

updateSessionState(sess *localSession, event *session.Event) error

Applies the state delta from an event to the session’s state map.


Implementation Details and Concurrency


Interaction with Other Components


Usage Example

// Assume sess is an instance of *localSession and event is a *session.Event

// Append a new event to the session
err := sess.appendEvent(event)
if err != nil {
    // handle error
}

// Retrieve session state
state := sess.State()
val, err := state.Get("key")
if err == nil {
    fmt.Println("Value:", val)
}

// Iterate all events
for it := sess.Events().All(); ; {
    var e *session.Event
    if !it(func(ev *session.Event) bool {
        e = ev
        return false // stop after retrieving one for example
    }) {
        break
    }
    fmt.Println(e)
}

Mermaid Class Diagram

classDiagram
class localSession {
-appName: string
-userID: string
-sessionID: string
-mu: sync.RWMutex
-events: []*session.Event
-state: map[string]any
-updatedAt: time.Time
+ID()
+AppName()
+UserID()
+State()
+Events()
+LastUpdateTime()
+appendEvent(event *session.Event) error
}
class events {
-events: []*session.Event
+All()
+Len()
+At(i int)
}
class state {
-mu: *sync.RWMutex
-state: map[string]any
+Get(key string) (any, error)
+All()
+Set(key string, value any) error
}
localSession --> state : provides
localSession --> events : provides

This documentation covers the structure, behavior, and integration of the session.go file’s contents within the database session service subsystem of the Session Management topic. It focuses on the in-memory session representation, event handling, and state management implemented here.