event

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 12 Imported by: 0

README

Type-safe in-process pub/sub event library, for Go language

Features

  • Type-safe: every kind of event is a concrete go type instead of error-prone interface{} or map[string]interface{}. Unmatched event is rejected.
  • Pub-sub (topic): publishes event to multiple listeners, thread-safely
  • Asynchronous: listeners wait on their separated go channels and routines. Listener execution panic won't affect others, error is logged only instead
  • Fine-tuned logging: agnostic logging abstraction, able to adapt various of logging implementation. Go std log and github.com/phuslu/log are built-in provided. Or turn on logging completelly.

Install

go get github.com/fastgh/go-event

Example

package main

import (
  "fmt"

  "github.com/fastgh/go-event"
  "github.com/fastgh/go-event/loggers"
)

type MyEvent struct {
  Name string
}

func main() {
  myHub := event.NewHub("default", loggers.NewDefaultGlobalStdLogger())

  myTopic := event.CreateTopic(myHub, "myTopic", MyEvent{})

  myTopic.Sub("listener1", func(e MyEvent) {
    fmt.Println("listener1 - got event from", e)
  }, 0)

  myTopic.Sub("listener2", func(e MyEvent) {
    fmt.Println("listener2 - got event from", e)
  }, 0)

  myTopic.Pub(event.PubModeAuto, MyEvent{"fastgh"})

  myHub.Close(true)
}

To run the example:

go run ./examples/hello/main.go

It will output:

2022/09/18 18:57:29 <INFO> hub=default, topic=myTopic, listener= --> topic register begin
2022/09/18 18:57:29 <INFO> hub=default, topic=myTopic, listener= --> topic register ok
2022/09/18 18:57:29 <INFO> hub=default, topic=myTopic, listener=listener1 --> listener sub ok
2022/09/18 18:57:29 <INFO> hub=default, topic=myTopic, listener=listener2 --> listener sub ok
listener1 - got event from {fastgh}
listener2 - got event from {fastgh}
2022/09/18 18:57:29 <INFO> hub=default, topic=, listener= --> hub close begin
2022/09/18 18:57:29 <INFO> event={"Id":2,"Hub":"default","Topic":"myTopic","Close":true,"Data":null}, listener=listener1 --> listener close begin
2022/09/18 18:57:29 <INFO> event={"Id":2,"Hub":"default","Topic":"myTopic","Close":true,"Data":null}, listener=listener2 --> listener close begin
2022/09/18 18:57:29 <INFO> hub=default, topic=, listener= --> hub close ok

Logging

type Logger interface {
  LogDebug(enm LogEnum, hub string, topic string, lsner string)
  LogInfo(enm LogEnum, hub string, topic string, lsner string)
  LogError(enm LogEnum, hub string, topic string, lsner string, err any)

  LogEventDebug(enm LogEnum, lsner string, evnt Event)
  LogEventInfo(enm LogEnum, lsner string, evnt Event)
  LogEventError(enm LogEnum, lsner string, evnt Event, err any)
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogEnumStrings

func LogEnumStrings() []string

LogEnumStrings returns a slice of all String values of the enum

func LogLevelStrings

func LogLevelStrings() []string

LogLevelStrings returns a slice of all String values of the enum

Types

type Event

type Event = *EventT

func NewCloseEvent

func NewCloseEvent(id EventId, hub string, topic string) Event

func NewDataEvent

func NewDataEvent(id EventId, hub string, topic string, dat any) Event

func (Event) MarshalObject

func (me Event) MarshalObject(entry *phuslu.Entry)

func (Event) String

func (me Event) String() string

type EventId

type EventId int64

type EventListener

type EventListener[K any] struct {
	// contains filtered or unexported fields
}

func NewEventListener

func NewEventListener[K any](name string, lsner Listener[K], qSize uint32, topicLogr TopicLogger) *EventListener[K]

func (*EventListener[K]) SendEvent

func (me *EventListener[K]) SendEvent(evnt Event)

func (*EventListener[K]) Start

func (me *EventListener[K]) Start()

func (*EventListener[K]) Stop

func (me *EventListener[K]) Stop(stopEvnt Event)

type EventT

type EventT struct {
	Id    EventId
	Hub   string
	Topic string
	Close bool
	Data  any
}

type Hub

type Hub = *HubT

func NewHub

func NewHub(name string, logr Logger) Hub

func (Hub) Close

func (me Hub) Close(wait bool)

func (Hub) GetTopic

func (me Hub) GetTopic(name string, evntExample any) TopicBase

func (Hub) HasTopic

func (me Hub) HasTopic(name string) bool

func (Hub) Logger

func (me Hub) Logger() HubLogger

func (Hub) Name

func (me Hub) Name() string

func (Hub) RegisterTopic

func (me Hub) RegisterTopic(topic TopicBase)

type HubLogger

type HubLogger = *HubLoggerT

func NewHubLogger

func NewHubLogger(hub string, logr Logger) HubLogger

func (HubLogger) Hub

func (me HubLogger) Hub() string

func (HubLogger) LogDebug

func (me HubLogger) LogDebug(enm LogEnum, topic string, lsner string)

func (HubLogger) LogError

func (me HubLogger) LogError(enm LogEnum, topic string, lsner string, err any)

func (HubLogger) LogEventDebug

func (me HubLogger) LogEventDebug(enm LogEnum, lsner string, evnt Event)

func (HubLogger) LogEventError

func (me HubLogger) LogEventError(enm LogEnum, lsner string, evnt Event, err any)

func (HubLogger) LogEventInfo

func (me HubLogger) LogEventInfo(enm LogEnum, lsner string, evnt Event)

func (HubLogger) LogInfo

func (me HubLogger) LogInfo(enm LogEnum, topic string, lsner string)

type HubLoggerT

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

type HubT

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

type LevelFilteringLogger

type LevelFilteringLogger = *LevelFilteringLoggerT

func NewLevelFilteringLogger

func NewLevelFilteringLogger(level LogLevel, target Logger) LevelFilteringLogger

func (LevelFilteringLogger) LogDebug

func (me LevelFilteringLogger) LogDebug(enm LogEnum, hub string, topic string, lsner string)

func (LevelFilteringLogger) LogError

func (me LevelFilteringLogger) LogError(enm LogEnum, hub string, topic string, lsner string, err any)

func (LevelFilteringLogger) LogEventDebug

func (me LevelFilteringLogger) LogEventDebug(enm LogEnum, lsner string, evnt Event)

func (LevelFilteringLogger) LogEventError

func (me LevelFilteringLogger) LogEventError(enm LogEnum, lsner string, evnt Event, err any)

func (LevelFilteringLogger) LogEventInfo

func (me LevelFilteringLogger) LogEventInfo(enm LogEnum, lsner string, evnt Event)

func (LevelFilteringLogger) LogInfo

func (me LevelFilteringLogger) LogInfo(enm LogEnum, hub string, topic string, lsner string)

func (LevelFilteringLogger) Target

func (me LevelFilteringLogger) Target() Logger

type LevelFilteringLoggerT

type LevelFilteringLoggerT struct {
	Level LogLevel
	// contains filtered or unexported fields
}

type Listener

type Listener[K any] func(evnt K)

type ListenerLogger

type ListenerLogger = *ListenerLoggerT

func NewListenerLogger

func NewListenerLogger(lsner string, logr TopicLogger) ListenerLogger

func (ListenerLogger) Listener

func (me ListenerLogger) Listener() string

func (ListenerLogger) LogDebug

func (me ListenerLogger) LogDebug(enm LogEnum)

func (ListenerLogger) LogError

func (me ListenerLogger) LogError(enm LogEnum, err any)

func (ListenerLogger) LogEventDebug

func (me ListenerLogger) LogEventDebug(enm LogEnum, evnt Event)

func (ListenerLogger) LogEventError

func (me ListenerLogger) LogEventError(enm LogEnum, evnt Event, err any)

func (ListenerLogger) LogEventInfo

func (me ListenerLogger) LogEventInfo(enm LogEnum, evnt Event)

func (ListenerLogger) LogInfo

func (me ListenerLogger) LogInfo(enm LogEnum)

type ListenerLoggerT

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

type LogEnum

type LogEnum int64
const (
	HubCloseBegin LogEnum = iota
	HubCloseOk

	ListenerSubOk
	ListenerSubErr
	ListenerUnsubOk
	ListenerUnsubErr

	ListenerCloseBegin
	ListenerCloseOk

	TopicRegisterBegin
	TopicRegisterOk

	TopicCloseBegin
	TopicCloseOk

	EventPubBegin
	EventPubOk

	EventSendBegin
	EventSendOk

	EventHandleBegin
	EventHandleOk
	EventHandleErr
)

func LogEnumString

func LogEnumString(s string) (LogEnum, error)

LogEnumString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func LogEnumValues

func LogEnumValues() []LogEnum

LogEnumValues returns all values of the enum

func (LogEnum) IsALogEnum

func (i LogEnum) IsALogEnum() bool

IsALogEnum returns "true" if the value is listed in the enum definition. "false" otherwise

func (LogEnum) MarshalGQL

func (i LogEnum) MarshalGQL(w io.Writer)

MarshalGQL implements the graphql.Marshaler interface for LogEnum

func (LogEnum) MarshalJSON

func (i LogEnum) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for LogEnum

func (LogEnum) MarshalText

func (i LogEnum) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for LogEnum

func (LogEnum) MarshalYAML

func (i LogEnum) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for LogEnum

func (*LogEnum) Scan

func (i *LogEnum) Scan(value interface{}) error

func (LogEnum) String

func (i LogEnum) String() string

func (*LogEnum) UnmarshalGQL

func (i *LogEnum) UnmarshalGQL(value interface{}) error

UnmarshalGQL implements the graphql.Unmarshaler interface for LogEnum

func (*LogEnum) UnmarshalJSON

func (i *LogEnum) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for LogEnum

func (*LogEnum) UnmarshalText

func (i *LogEnum) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for LogEnum

func (*LogEnum) UnmarshalYAML

func (i *LogEnum) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for LogEnum

func (LogEnum) Value

func (i LogEnum) Value() (driver.Value, error)

type LogLevel

type LogLevel int8
const (
	LogLevelAnyway LogLevel = iota

	LogLevelDebug
	LogLevelInfo
	LogLevelError

	LogLevelSilient LogLevel = 127
)

func LogLevelString

func LogLevelString(s string) (LogLevel, error)

LogLevelString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func LogLevelValues

func LogLevelValues() []LogLevel

LogLevelValues returns all values of the enum

func (LogLevel) IsALogLevel

func (i LogLevel) IsALogLevel() bool

IsALogLevel returns "true" if the value is listed in the enum definition. "false" otherwise

func (LogLevel) MarshalGQL

func (i LogLevel) MarshalGQL(w io.Writer)

MarshalGQL implements the graphql.Marshaler interface for LogLevel

func (LogLevel) MarshalJSON

func (i LogLevel) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for LogLevel

func (LogLevel) MarshalText

func (i LogLevel) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for LogLevel

func (LogLevel) MarshalYAML

func (i LogLevel) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for LogLevel

func (*LogLevel) Scan

func (i *LogLevel) Scan(value interface{}) error

func (LogLevel) String

func (i LogLevel) String() string

func (*LogLevel) UnmarshalGQL

func (i *LogLevel) UnmarshalGQL(value interface{}) error

UnmarshalGQL implements the graphql.Unmarshaler interface for LogLevel

func (*LogLevel) UnmarshalJSON

func (i *LogLevel) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for LogLevel

func (*LogLevel) UnmarshalText

func (i *LogLevel) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for LogLevel

func (*LogLevel) UnmarshalYAML

func (i *LogLevel) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for LogLevel

func (LogLevel) Value

func (i LogLevel) Value() (driver.Value, error)

type Logger

type Logger interface {
	LogDebug(enm LogEnum, hub string, topic string, lsner string)
	LogInfo(enm LogEnum, hub string, topic string, lsner string)
	LogError(enm LogEnum, hub string, topic string, lsner string, err any)

	LogEventDebug(enm LogEnum, lsner string, evnt Event)
	LogEventInfo(enm LogEnum, lsner string, evnt Event)
	LogEventError(enm LogEnum, lsner string, evnt Event, err any)
}

type PubMode

type PubMode byte
const (
	PubModeSync PubMode = iota
	PubModeAsync
	PubModeAuto
)

type Topic

type Topic[K any] interface {
	TopicBase

	Sub(name string, lsner Listener[K], qSize uint32) int
	Pub(mode PubMode, evnt K)
}

func CreateTopic

func CreateTopic[K any](hub Hub, name string, evntExample K) Topic[K]

func GetTopic

func GetTopic[K any](me Hub, name string, evntExample K) Topic[K]

func NewTopic

func NewTopic[K any](name string, hub Hub, evntExample K, logr HubLogger) Topic[K]

type TopicBase

type TopicBase interface {
	Name() string
	Hub() Hub
	CurrEventId() EventId
	NewEventId() EventId
	EventType() reflect.Type

	UnSub(name string) bool
	Close(wait bool)
}

type TopicImpl

type TopicImpl[K any] struct {
	// contains filtered or unexported fields
}

func NewTopicImpl

func NewTopicImpl[K any](name string, hub Hub, example K, logr HubLogger) *TopicImpl[K]

func (*TopicImpl[K]) Close

func (me *TopicImpl[K]) Close(wait bool)

func (*TopicImpl[K]) CurrEventId

func (me *TopicImpl[K]) CurrEventId() EventId

func (*TopicImpl[K]) EventType

func (me *TopicImpl[K]) EventType() reflect.Type

func (*TopicImpl[K]) Hub

func (me *TopicImpl[K]) Hub() Hub

func (*TopicImpl[K]) Name

func (me *TopicImpl[K]) Name() string

func (*TopicImpl[K]) NewEventId

func (me *TopicImpl[K]) NewEventId() EventId

func (*TopicImpl[K]) Pub

func (me *TopicImpl[K]) Pub(mode PubMode, evnt K)

func (*TopicImpl[K]) Sub

func (me *TopicImpl[K]) Sub(name string, lsner Listener[K], qSize uint32) int

func (*TopicImpl[K]) UnSub

func (me *TopicImpl[K]) UnSub(name string) bool

type TopicLogger

type TopicLogger = *TopicLoggerT

func NewTopicLogger

func NewTopicLogger(topic string, logr HubLogger) TopicLogger

func (TopicLogger) LogDebug

func (me TopicLogger) LogDebug(enm LogEnum, lsner string)

func (TopicLogger) LogError

func (me TopicLogger) LogError(enm LogEnum, lsner string, err any)

func (TopicLogger) LogEventDebug

func (me TopicLogger) LogEventDebug(enm LogEnum, lsner string, evnt Event)

func (TopicLogger) LogEventError

func (me TopicLogger) LogEventError(enm LogEnum, lsner string, evnt Event, err any)

func (TopicLogger) LogEventInfo

func (me TopicLogger) LogEventInfo(enm LogEnum, lsner string, evnt Event)

func (TopicLogger) LogInfo

func (me TopicLogger) LogInfo(enm LogEnum, lsner string)

func (TopicLogger) Topic

func (me TopicLogger) Topic() string

type TopicLoggerT

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

Directories

Path Synopsis
examples
loggers
std

Jump to

Keyboard shortcuts

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