Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetHandlerID ¶
GetHandlerID calculate handler func's address as id
Types ¶
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 ¶
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
Example ¶
ctx := context.Background()
evtstore, err := New(ctx,
WithChanBuffer(1),
WithNFork(2),
WithSuppressPanic(false),
)
if err != nil {
gutils.Logger.Panic("new evt engine", zap.Error(err))
}
var (
topic1 types.EventTopic = "t1"
topic2 types.EventTopic = "t2"
)
evt1 := &types.Event{
Topic: topic1,
Meta: types.EventMeta{
"name": "yo",
},
}
evt2 := &types.Event{
Topic: topic2,
Meta: types.EventMeta{
"name": "yo2",
},
}
handler := func(evt *types.Event) error {
fmt.Printf("got event %s: %v\n", evt.Topic, evt.Meta)
return nil
}
evtstore.Register(topic1, handler)
evtstore.Publish(ctx, evt1) // registered topic invokes its handler
evtstore.Publish(ctx, evt2) // nothing print
evtstore.UnRegister(topic1, handler)
evtstore.Publish(ctx, evt1) // nothing print
evtstore.Publish(ctx, evt2) // nothing print
type OptFunc ¶
type OptFunc func(*eventStoreManagerOpt) error
OptFunc options for EventEngine
func WithSuppressPanic ¶
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
- create an engine by `NewEventEngine`
- register handlers with specified event type by `engine.Register`
- produce event to trigger handlers by `engine.Publish`
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
Click to show internal directories.
Click to hide internal directories.