session

package
v0.8.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSessionNotFound = fmt.Errorf("session not found")

ErrSessionNotFound is returned when a session is not found

Functions

This section is empty.

Types

type Connection

type Connection interface {
	// EventQueue returns a read-only channel where outbound messages are published.
	EventQueue() <-chan *Message

	// Send pushes a message to the session.
	Send(ctx context.Context, msg *Message) error

	// Close gracefully terminates the session connection.
	Close(ctx context.Context) error

	// Meta returns metadata associated with the session.
	Meta() *Meta
}

Connection represents an active session connection capable of sending messages.

type MemoryConnection

type MemoryConnection struct {
	// contains filtered or unexported fields
}

MemoryConnection implements Connection using in-memory storage

func (*MemoryConnection) Close

func (c *MemoryConnection) Close(_ context.Context) error

Close implements Connection.Close

func (*MemoryConnection) EventQueue

func (c *MemoryConnection) EventQueue() <-chan *Message

EventQueue implements Connection.EventQueue

func (*MemoryConnection) Meta

func (c *MemoryConnection) Meta() *Meta

Meta implements Connection.Meta

func (*MemoryConnection) Send

func (c *MemoryConnection) Send(_ context.Context, msg *Message) error

Send implements Connection.Send

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore implements Store using in-memory storage

func NewMemoryStore

func NewMemoryStore(logger *zap.Logger) *MemoryStore

NewMemoryStore creates a new in-memory session store

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, id string) (Connection, error)

Get implements Store.Get

func (*MemoryStore) List

func (s *MemoryStore) List(_ context.Context) ([]Connection, error)

List implements Store.List

func (*MemoryStore) Register

func (s *MemoryStore) Register(_ context.Context, meta *Meta) (Connection, error)

Register implements Store.Register

func (*MemoryStore) Unregister

func (s *MemoryStore) Unregister(_ context.Context, id string) error

Unregister implements Store.Unregister

type Message

type Message struct {
	Event string // Event type, e.g., "message", "close", "ping"
	Data  []byte // Payload
}

Message represents a unified message structure for session communication.

type Meta

type Meta struct {
	ID        string       `json:"id"`         // Unique session ID
	CreatedAt time.Time    `json:"created_at"` // Timestamp of session creation
	Prefix    string       `json:"prefix"`     // Optional namespace or application prefix
	Type      string       `json:"type"`       // Connection type, e.g., "sse", "streamable"
	Request   *RequestInfo `json:"request"`    // Request information
	Extra     []byte       `json:"extra"`      // Optional serialized extra data
}

Meta holds immutable metadata about a session.

type RedisConnection

type RedisConnection struct {
	// contains filtered or unexported fields
}

RedisConnection implements Connection using Redis

func (*RedisConnection) Close

func (c *RedisConnection) Close(ctx context.Context) error

Close implements Connection.Close

func (*RedisConnection) EventQueue

func (c *RedisConnection) EventQueue() <-chan *Message

EventQueue implements Connection.EventQueue

func (*RedisConnection) Meta

func (c *RedisConnection) Meta() *Meta

Meta implements Connection.Meta

func (*RedisConnection) Send

func (c *RedisConnection) Send(ctx context.Context, msg *Message) error

Send implements Connection.Send

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore implements Store using Redis

func NewRedisStore

func NewRedisStore(logger *zap.Logger, cfg config.SessionRedisConfig) (*RedisStore, error)

NewRedisStore creates a new Redis-based session store func NewRedisStore(logger *zap.Logger, addr, username, password string, db int, topic string) (*RedisStore, error) {

func (*RedisStore) Close

func (s *RedisStore) Close() error

Close closes the Redis store

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, id string) (Connection, error)

Get implements Store.Get

func (*RedisStore) List

func (s *RedisStore) List(ctx context.Context) ([]Connection, error)

List implements Store.List

func (*RedisStore) Register

func (s *RedisStore) Register(ctx context.Context, meta *Meta) (Connection, error)

Register implements Store.Register

func (*RedisStore) Unregister

func (s *RedisStore) Unregister(ctx context.Context, id string) error

Unregister implements Store.Unregister

type RequestInfo

type RequestInfo struct {
	Headers map[string]string `json:"headers"`
	Query   map[string]string `json:"query"`
	Cookies map[string]string `json:"cookies"`
}

RequestInfo holds information about the request that created the session.

type Store

type Store interface {
	// Register creates and registers a new session connection.
	Register(ctx context.Context, meta *Meta) (Connection, error)

	// Get retrieves an active session connection by ID.
	Get(ctx context.Context, id string) (Connection, error)

	// Unregister removes a session connection by ID.
	Unregister(ctx context.Context, id string) error

	// List returns all currently active session connections.
	List(ctx context.Context) ([]Connection, error)
}

Store manages the lifecycle and lookup of active session connections.

func NewStore

func NewStore(logger *zap.Logger, cfg *config.SessionConfig) (Store, error)

NewStore creates a new session store based on configuration

type Type

type Type string

Type represents the type of session store

const (
	// TypeMemory represents in-memory session store
	TypeMemory Type = "memory"
	// TypeRedis represents Redis-based session store
	TypeRedis Type = "redis"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL