inmemory.go

Overview

This file implements an in-memory, thread-safe session service as part of the broader Session Management module. It provides a volatile storage backend for sessions, handling creation, retrieval, listing, deletion, and event appending for user-agent interaction sessions. The service maintains session states and event histories entirely in memory, supporting layered state merging at application, user, and session scopes. This implementation is optimized for development, testing, or scenarios requiring fast, ephemeral session storage without external dependencies.

The in-memory service conforms to the Service interface, ensuring compatibility with other session backend implementations such as the database-backed service.


Main Types and Their Responsibilities

inMemoryService

session

id

state

events


Key Methods in inMemoryService

Create(ctx context.Context, req *CreateRequest) (*CreateResponse, error)

Usage example:

resp, err := service.Create(ctx, &CreateRequest{
    AppName: "myapp",
    UserID: "user123",
    State: map[string]any{"foo": "bar"},
})
if err != nil {
    // handle error
}
session := resp.Session

Get(ctx context.Context, req *GetRequest) (*GetResponse, error)

Usage example:

resp, err := service.Get(ctx, &GetRequest{
    AppName: "myapp",
    UserID: "user123",
    SessionID: "sess456",
    NumRecentEvents: 10,
    After: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
})
if err != nil {
    // handle error
}
session := resp.Session

List(ctx context.Context, req *ListRequest) (*ListResponse, error)


Delete(ctx context.Context, req *DeleteRequest) error


AppendEvent(ctx context.Context, curSession Session, event *Event) error


State Management Helpers


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Class Structure of In-Memory Session Service

classDiagram
class inMemoryService {
- mu: RWMutex
- sessions: omap.Map
- userState: map[string]map[string]stateMap
- appState: map[string]stateMap
+ Create()
+ Get()
+ List()
+ Delete()
+ AppendEvent()
+ updateAppState()
+ updateUserState()
+ mergeStates()
}
class session {
- id: id
- mu: RWMutex
- events: []*Event
- state: map[string]any
- updatedAt: time.Time
+ ID()
+ AppName()
+ UserID()
+ State()
+ Events()
+ LastUpdateTime()
+ appendEvent()
}
class id {
- appName: string
- userID: string
- sessionID: string
+ Encode()
+ Decode()
}
inMemoryService "1" o-- "*" session : manages
session "1" *-- "1" id : identifies

Detailed Explanation of Selected Functions and Methods

func (s *session) appendEvent(event *Event) error


Auxiliary Functions

trimTempDeltaState(event *Event) *Event

updateSessionState(session *session, event *Event) error

copySessionWithoutStateAndEvents(sess *session) *session


Data Structures Summary

Type

Description

inMemoryService

Main service struct managing sessions and states

session

Represents a user-agent session with events and state

id

Composite key struct for appName, userID, sessionID

state

Thread-safe wrapper around session state map

events

Wrapper over event slice with iterator methods


Usage Context


This file is a crucial component of the overall Session Management system, providing a performant and straightforward in-memory option for session handling aligned with the system’s design principles for session state layering, event-driven updates, and concurrency safety.