base

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FrameMessage

type FrameMessage func(data []byte) []byte

FrameMessage is a function type that allows wrapping of the message before sending it to the client

type Handler

type Handler struct {
	Sessions SessionStore
	Logger   jsonrpc.Logger // Logger for error messages
}

Handler represents a jsonrpc endpoint

func NewHandler

func NewHandler() *Handler

func (*Handler) HandleMessage

func (e *Handler) HandleMessage(ctx context.Context, session *Session, data []byte, output *bytes.Buffer)

type Option

type Option func(s *Session)

Option represents option

func WithEventBuffer added in v0.6.0

func WithEventBuffer(size int) Option

WithEventBuffer sets size of in-memory event buffer for session so that server can re-deliver messages on Last-Event-ID reconnect.

func WithEventOverflowPolicy added in v0.15.0

func WithEventOverflowPolicy(policy OverflowPolicy) Option

WithEventOverflowPolicy sets the overflow policy for event buffering.

func WithFramer

func WithFramer(framer FrameMessage) Option

func WithSSE added in v0.11.0

func WithSSE() Option

WithSSE enables SSE id injection on each framed message and stores the same id for resumability (Last-Event-ID).

type OverflowPolicy added in v0.15.0

type OverflowPolicy int

OverflowPolicy defines how the event buffer handles overflow.

const (
	// OverflowDropOldest drops the oldest events when the buffer is full.
	OverflowDropOldest OverflowPolicy = iota
	// OverflowMark sets an overflow flag when buffer would overflow, while still dropping oldest.
	OverflowMark
)

type RemovalPolicy added in v0.15.0

type RemovalPolicy int

RemovalPolicy determines when a session should be removed from the session store.

const (
	// RemovalOnDisconnect removes session as soon as streaming connection closes.
	// Useful for strict cleanup behavior.
	RemovalOnDisconnect RemovalPolicy = iota
	// RemovalAfterGrace keeps session for a grace period to allow quick reconnects.
	RemovalAfterGrace
	// RemovalAfterIdle removes session after it has been idle for a configured TTL.
	RemovalAfterIdle
	// RemovalManual leaves removal entirely to explicit DELETE or external cleanup.
	RemovalManual
)

type Session

type Session struct {
	Id         string `json:"id"`
	RoundTrips *transport.RoundTrips
	Writer     io.Writer
	Handler    transport.Handler

	RequestIdSeq uint64

	sync.Mutex

	// Lifecycle metadata
	CreatedAt     time.Time
	LastSeen      time.Time
	DetachedAt    *time.Time
	State         SessionState
	WriterPresent bool
	// contains filtered or unexported fields
}

func NewSession

func NewSession(ctx context.Context, id string, writer io.Writer, newHandler transport.NewHandler, options ...Option) *Session

func (*Session) Error

func (s *Session) Error() error

Error returns error

func (*Session) EventsAfter added in v0.6.0

func (s *Session) EventsAfter(lastID uint64) [][]byte

EventsAfter returns buffered framed messages with id greater than lastID.

func (*Session) LastRequestID added in v0.7.3

func (s *Session) LastRequestID() jsonrpc.RequestId

LastRequestID returns the most recently generated request id without mutating the underlying sequence. It is concurrency-safe and can be used to inspect the current sequence value.

func (*Session) MarkActiveWithWriter added in v0.15.0

func (s *Session) MarkActiveWithWriter(w io.Writer)

MarkActiveWithWriter re-attaches a writer and marks session active.

func (*Session) MarkDetached added in v0.15.0

func (s *Session) MarkDetached()

MarkDetached marks session as detached and records time.

func (*Session) NextRequestID added in v0.7.1

func (s *Session) NextRequestID() jsonrpc.RequestId

func (*Session) SendData

func (s *Session) SendData(ctx context.Context, data []byte)

SendData sends data

func (*Session) SendError

func (s *Session) SendError(ctx context.Context, error *jsonrpc.Error)

SendError sends error

func (*Session) SendRequest

func (s *Session) SendRequest(ctx context.Context, request *jsonrpc.Request)

SendRequest sends response

func (*Session) SendResponse

func (s *Session) SendResponse(ctx context.Context, response *jsonrpc.Response)

SendResponse sends response

func (*Session) SetError

func (s *Session) SetError(err error)

SetError sets error

func (*Session) Touch added in v0.15.0

func (s *Session) Touch()

Touch updates LastSeen timestamp.

func (*Session) WriterGeneration added in v0.15.0

func (s *Session) WriterGeneration() uint64

WriterGeneration returns the current writer attachment generation.

type SessionState added in v0.15.0

type SessionState int

SessionState represents lifecycle state of a session.

const (
	SessionStateActive SessionState = iota
	SessionStateDetached
	SessionStateClosed
)

type SessionStore added in v0.15.0

type SessionStore interface {
	Get(id string) (*Session, bool)
	Put(id string, s *Session)
	Delete(id string)
	Range(func(id string, s *Session) bool)
}

SessionStore abstracts session persistence. Default implementation is in-memory; custom stores (e.g., Redis) can implement this interface.

func NewMemorySessionStore added in v0.15.0

func NewMemorySessionStore() SessionStore

NewMemorySessionStore creates an in-memory SessionStore.

type Transport

type Transport struct {
	TripTimeout time.Duration
	// contains filtered or unexported fields
}

Transport represents a Transport

func NewTransport

func NewTransport(tripper *transport.RoundTrips, sendData func(ctx context.Context, data []byte), session *Session) *Transport

NewTransport creates a new Transport

func (*Transport) LastRequestID added in v0.7.3

func (s *Transport) LastRequestID() jsonrpc.RequestId

LastRequestID returns the most recently generated request id without mutating the sequence.

func (*Transport) NextRequestID added in v0.7.1

func (s *Transport) NextRequestID() jsonrpc.RequestId

func (*Transport) Notify

func (s *Transport) Notify(ctx context.Context, notification *jsonrpc.Notification) error

func (*Transport) Send

func (s *Transport) Send(ctx context.Context, request *jsonrpc.Request) (*jsonrpc.Response, error)

Jump to

Keyboard shortcuts

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