forked from TrueCloudLab/frostfs-node
[#324] morph/listener: Register handlers of new chain blocks
Extend Listener with RegisterBlockHandler method. All block handlers are called on each block read from Subscriber.BlockNotifications channel. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
c65d1d9db9
commit
23c220ae28
3 changed files with 63 additions and 9 deletions
|
@ -1,8 +1,15 @@
|
|||
package event
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
||||
)
|
||||
|
||||
// Handler is an Event processing function.
|
||||
type Handler func(Event)
|
||||
|
||||
// BlockHandler is a chain block processing function.
|
||||
type BlockHandler func(*block.Block)
|
||||
|
||||
// HandlerInfo is a structure that groups
|
||||
// the parameters of the handler of particular
|
||||
// contract event.
|
||||
|
|
|
@ -43,6 +43,13 @@ type Listener interface {
|
|||
|
||||
// Must stop the event listener.
|
||||
Stop()
|
||||
|
||||
// Must register chain block handler.
|
||||
//
|
||||
// The specified handler must be called after each capture and parsing of the new block from chain.
|
||||
//
|
||||
// Must ignore nil handlers.
|
||||
RegisterBlockHandler(BlockHandler)
|
||||
}
|
||||
|
||||
// ListenerParams is a group of parameters
|
||||
|
@ -67,6 +74,8 @@ type listener struct {
|
|||
log *zap.Logger
|
||||
|
||||
subscriber subscriber.Subscriber
|
||||
|
||||
blockHandlers []BlockHandler
|
||||
}
|
||||
|
||||
const newListenerFailMsg = "could not instantiate Listener"
|
||||
|
@ -144,6 +153,13 @@ func (s listener) listen(ctx context.Context, intError chan<- error) error {
|
|||
}
|
||||
|
||||
func (s listener) listenLoop(ctx context.Context, chEvent <-chan *state.NotificationEvent, intErr chan<- error) {
|
||||
blockChan, err := s.subscriber.BlockNotifications()
|
||||
if err != nil {
|
||||
intErr <- errors.Wrap(err, "could not open block notifications channel")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
|
@ -166,6 +182,23 @@ loop:
|
|||
}
|
||||
|
||||
s.parseAndHandle(notifyEvent)
|
||||
case b, ok := <-blockChan:
|
||||
if !ok {
|
||||
s.log.Warn("stop event listener by block channel")
|
||||
if intErr != nil {
|
||||
intErr <- errors.New("new block notification channel is closed")
|
||||
}
|
||||
|
||||
break loop
|
||||
} else if b == nil {
|
||||
s.log.Warn("nil block was caught")
|
||||
continue loop
|
||||
}
|
||||
|
||||
// TODO: consider asynchronous execution
|
||||
for i := range s.blockHandlers {
|
||||
s.blockHandlers[i](b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -313,6 +346,15 @@ func (s listener) Stop() {
|
|||
s.subscriber.Close()
|
||||
}
|
||||
|
||||
func (s *listener) RegisterBlockHandler(handler BlockHandler) {
|
||||
if handler == nil {
|
||||
s.log.Warn("ignore nil block handler")
|
||||
return
|
||||
}
|
||||
|
||||
s.blockHandlers = append(s.blockHandlers, handler)
|
||||
}
|
||||
|
||||
// NewListener create the notification event listener instance and returns Listener interface.
|
||||
func NewListener(p ListenerParams) (Listener, error) {
|
||||
switch {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue