sse

package
v0.0.0-...-095633c Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

This package contains the Server Sent Events implementation used by aurora.

Index

Constants

This section is empty.

Variables

View Source
var (

	// known errors
	ErrRateLimited = errors.New("Rate limit exceeded")
)

Functions

func WriteEvent

func WriteEvent(ctx context.Context, w http.ResponseWriter, e Event)

WriteEvent does the actual work of formatting an SSE compliant message sending it over the provided ResponseWriter and flushing.

func WritePreamble

func WritePreamble(ctx context.Context, w http.ResponseWriter) bool

WritePreamble prepares this http connection for streaming using Server Sent Events. It sends the initial http response with the appropriate headers to do so.

Types

type Event

type Event struct {
	Data  interface{}
	Error error
	ID    string
	Event string
	Retry int
}

Event is the packet of data that gets sent over the wire to a connected client.

type GenerateEventsFunc

type GenerateEventsFunc func() ([]Event, error)

GenerateEventsFunc generates a slice of sse.Event which are sent via streaming.

type LedgerSourceFactory

type LedgerSourceFactory interface {
	Get() ledger.Source
}

type Stream

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

func NewStream

func NewStream(ctx context.Context, w http.ResponseWriter) *Stream

NewStream creates a new stream against the provided response writer.

func (*Stream) Done

func (s *Stream) Done()

func (*Stream) Err

func (s *Stream) Err(err error)

func (*Stream) Init

func (s *Stream) Init()

Init function is only executed once. It writes the preamble event which includes the HTTP response code and a hello message. This should be called before any method that writes to the client to ensure that the preamble has been sent first.

func (*Stream) Send

func (s *Stream) Send(e Event)

func (*Stream) SetLimit

func (s *Stream) SetLimit(limit int)

type StreamHandler

type StreamHandler struct {
	RateLimiter         *throttled.HTTPRateLimiter
	LedgerSourceFactory LedgerSourceFactory
}

StreamHandler represents a stream handling action

func (StreamHandler) ServeStream

func (handler StreamHandler) ServeStream(
	w http.ResponseWriter,
	r *http.Request,
	limit int,
	generateEvents GenerateEventsFunc,
)

ServeStream handles a SSE requests, sending data every time there is a new ledger.

Jump to

Keyboard shortcuts

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