sse

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeHTTPHandler

func MakeHTTPHandler(s sseServer, log logger) http.Handler

MakeHTTPHandler returns a handler that makes a set of endpoints available on predefined paths.

func TranslateEventsToSSEChannel

func TranslateEventsToSSEChannel(sse sseService) events.Listener

TranslateEventsToSSEChannel translates the events from the events package to the webhook events.

Types

type Event

type Event struct {
	ID        int64
	Data      EventData
	TTL       int64 `json:"-"`
	Timestamp int64 `json:"-"`
}

Event struct

func (Event) IsExpired

func (e Event) IsExpired() bool

IsExpired func

func (Event) MapToSseEvent

func (e Event) MapToSseEvent() sse.Event

MapToSseEvent func to convert struct Event to sse.Event

type EventData

type EventData struct {
	Name    string      `json:"name"`
	Payload interface{} `json:"payload"`
}

EventData struct

type MemStorage

type MemStorage struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MemStorage struct

func NewMemStorage

func NewMemStorage() *MemStorage

NewMemStorage is a factory func, returns a new instance of the MemStorage structure

func (*MemStorage) Add

func (s *MemStorage) Add(channelID string, event Event) error

Add event to storage

func (*MemStorage) Delete

func (s *MemStorage) Delete(channelID string, event Event) error

Delete event from storage

func (*MemStorage) GC

func (s *MemStorage) GC(eventMaxAge, gcPeriod string, wg *sync.WaitGroup) error

GC - garbage collector

func (*MemStorage) GetAllInChannel

func (s *MemStorage) GetAllInChannel(channelID string) []Event

GetAllInChannel returns all events in channel

func (*MemStorage) GetByLastID

func (s *MemStorage) GetByLastID(channelID string, lastEventID int64) []Event

GetByLastID returns events in a channel which has id greater than given one

type Service

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

SSE service struct

func NewService

func NewService(storage Storage) *Service

NewService factory func returns new SSE service

func (*Service) DumpStorage

func (s *Service) DumpStorage(channelID string) []Event

DumpStorage func returns all events in channel

func (*Service) PubEvent

func (s *Service) PubEvent(channelID string, data EventData, ttl int64) error

PubEvent func publishes data to channel with given ttl in seconds, returns error if any

func (*Service) SubscribeToChannel

func (s *Service) SubscribeToChannel(channelID, lastEventID string) (chan interface{}, []Event, error)

SubscribeToChannel func returns channel with events and history of events, returns error if any

func (*Service) Unsubscribe

func (s *Service) Unsubscribe(channelID string, listener chan interface{}) error

Unsubscribe from channel with given listener returns error if any

type Storage

type Storage interface {
	// get all events in channel
	GetAllInChannel(channelID string) []Event
	// get events in a channel which has id greater than given one
	GetByLastID(channelID string, lastEventID int64) []Event
	// Add event to storage
	Add(channelID string, event Event) error
	// Delete event from storage
	Delete(channelID string, event Event) error
	// Deletes event which is older then given time from channel
	GC(eventMaxAge, gcPeriod string, wg *sync.WaitGroup) error
}

Storage interface

Jump to

Keyboard shortcuts

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