service.go

Overview

The service.go file in the session package defines the primary interface and data structures for managing user interaction sessions within the system. It establishes the contract for session storage services, including creation, retrieval, listing, deletion, and event appending functionality. The file also provides a factory method for an in-memory implementation of the session service, and defines request and response types used in the service API.

This file is foundational within the Session Management topic, as it specifies the core abstractions and communication patterns for working with sessions and events, enabling pluggable backend implementations and consistent session lifecycle management.

Key Elements

Service Interface

type Service interface {
	Create(context.Context, *CreateRequest) (*CreateResponse, error)
	Get(context.Context, *GetRequest) (*GetResponse, error)
	List(context.Context, *ListRequest) (*ListResponse, error)
	Delete(context.Context, *DeleteRequest) error
	AppendEvent(context.Context, Session, *Event) error
}

This interface abstracts session management operations and facilitates interchangeable backend implementations, such as in-memory or database-backed services.

InMemoryService Function

func InMemoryService() Service

Request and Response Types

These structures define the input parameters and output data for session operations.

CreateRequest

type CreateRequest struct {
	AppName   string
	UserID    string
	SessionID string // Optional; autogenerated if empty
	State     map[string]any
}

CreateResponse

type CreateResponse struct {
	Session Session
}

GetRequest

type GetRequest struct {
	AppName         string
	UserID          string
	SessionID       string
	NumRecentEvents int       // Optional filter: max number of recent events
	After           time.Time // Optional filter: events after this timestamp
}

GetResponse

type GetResponse struct {
	Session Session
}

ListRequest

type ListRequest struct {
	AppName string
	UserID  string
}

ListResponse

type ListResponse struct {
	Sessions []Session
}

DeleteRequest

type DeleteRequest struct {
	AppName   string
	UserID    string
	SessionID string
}

Important Implementation Details

Interaction With Other System Components

Visual Diagram

classDiagram
class Service {
+Create(ctx, req)
+Get(ctx, req)
+List(ctx, req)
+Delete(ctx, req)
+AppendEvent(ctx, session, event)
}
class InMemoryService {
-appState
-userState
+Create(ctx, req)
+Get(ctx, req)
+List(ctx, req)
+Delete(ctx, req)
+AppendEvent(ctx, session, event)
}
Service <|.. InMemoryService
class CreateRequest {
+AppName
+UserID
+SessionID
+State
}
class CreateResponse {
+Session
}
class GetRequest {
+AppName
+UserID
+SessionID
+NumRecentEvents
+After
}
class GetResponse {
+Session
}
class ListRequest {
+AppName
+UserID
}
class ListResponse {
+Sessions
}
class DeleteRequest {
+AppName
+UserID
+SessionID
}

This diagram illustrates:


For further details on the implementation of the in-memory and database-backed services, and session state management algorithms including event filtering and state scoping, see the related subtopics in Session Management.