eventengine

package module
v0.0.0-...-abf0de2 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2021 License: MIT Imports: 13 Imported by: 0

README

go-eventengine

Commitizen friendly Go Report Card GoDoc Build Status codecov

simple event driven tools

Example

package main

import (
	"context"

	eventengine "github.com/Laisky/go-eventengine"
	"github.com/Laisky/go-eventengine/types"
	gutils "github.com/Laisky/go-utils"
	"github.com/Laisky/zap"
)

var closed = make(chan struct{})

func handler(evt *types.Event) error {
	gutils.Logger.Info("handler", zap.Any("event", evt))
	closed <- struct{}{}
	return nil
}

func main() {
	ctx := context.Background()

	engine, err := eventengine.New(ctx)
	if err != nil {
		gutils.Logger.Panic("new engine", zap.Error(err))
	}

	var topic types.EventTopic = "hello"

	// register handler to topic
	engine.Register(topic, handler)

	// trigger event
	engine.Publish(ctx, &types.Event{Topic: topic})

	// wait for handler to be called
	<-closed
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFuncAddress

func GetFuncAddress(v interface{}) string

GetFuncAddress get address of func

func GetHandlerID

func GetHandlerID(handler Handler) types.HandlerID

GetHandlerID calculate handler func's address as id

Types

type Handler

type Handler func(*types.Event) error

Handler function to handle event

type Interface

type Interface interface {
	// Register register new handler for topic
	//
	// Args:
	//   * topic: specific the topic that will trigger the handler
	//   * handler: the func that used to process event
	//
	// handler will be invoked when eventengine received an event in specific topic.
	//
	// you can register same handler with different topic.
	// if you register same handler with same topic,
	// only one handler will be accepted.
	Register(topic types.EventTopic, handler Handler)
	// UnRegister remove handler for topic
	//
	// if one handler register in different topic,
	// only the handler in specific topic will be removed.
	UnRegister(topic types.EventTopic, handler Handler)
	// Publish publish new event
	Publish(ctx context.Context, evt *types.Event)
}

func New

func New(ctx context.Context, opts ...OptFunc) (Interface, error)

New new event store manager

Args:

  • ctx:
  • WithNFork: n goroutines to run handlers in parallel
  • WithChanBuffer: length of channel to receive published event
  • WithLogger: internal logger in event engine
  • WithSuppressPanic: if is true, will not raise panic when running handler

type OptFunc

type OptFunc func(*eventStoreManagerOpt) error

OptFunc options for EventEngine

func WithChanBuffer

func WithChanBuffer(msgBufferSize int) OptFunc

WithChanBuffer set msg buffer size of event store

default to 1

func WithLogger

func WithLogger(logger *gutils.LoggerType) OptFunc

WithLogger set event store's logger

default to gutils' internal logger

func WithMQ

func WithMQ(mq mq.Interface) OptFunc

WithMQ set whether suppress event handler's panic

default to null

func WithNFork

func WithNFork(nfork int) OptFunc

WithNFork set nfork of event store

default to 2

func WithSuppressPanic

func WithSuppressPanic(suppressPanic bool) OptFunc

WithSuppressPanic set whether suppress event handler's panic

default to false

type Type

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

Type event driven engine

Usage

you -> produce event -> trigger multiply handlers

  1. create an engine by `NewEventEngine`
  2. register handlers with specified event type by `engine.Register`
  3. produce event to trigger handlers by `engine.Publish`

func (*Type) Publish

func (e *Type) Publish(ctx context.Context, evt *types.Event)

Publish publish new event

func (*Type) Register

func (e *Type) Register(topic types.EventTopic, handler Handler)

Register register handler

func (*Type) UnRegister

func (e *Type) UnRegister(topic types.EventTopic, handler Handler)

UnRegister unregister handler

Directories

Path Synopsis
internal
mq
mock
Package mock is mock mq for unit test
Package mock is mock mq for unit test
redis
Package redis mq base on redis
Package redis mq base on redis

Jump to

Keyboard shortcuts

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