-
Notifications
You must be signed in to change notification settings - Fork 174
/
enqueue.go
106 lines (88 loc) · 3 KB
/
enqueue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package engine
import (
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/logging"
)
type Message struct {
OriginID flow.Identifier
Payload interface{}
}
// MessageStore is the interface to abstract how messages are buffered in memory
// while waiting to be processed.
type MessageStore interface {
// Put adds the message to the message store. It returns true if the message
// is stored, and false if it is immediately dropped.
// Note: depending on the implementation, message stores might drop messages
// later according to their internal ejection policy. In other words, a return
// value of `true` does _not imply_ that the message is eventually processed.
Put(*Message) bool
// Get retrieves the next message from the message store. It returns true if
// a message is retrieved, and false if the message store is empty.
Get() (*Message, bool)
}
type Pattern struct {
// Match is a function to match a message to this pattern, typically by payload type.
Match MatchFunc
// Map is a function to apply to messages before storing them. If not provided, then the message is stored in its original form.
Map MapFunc
// Store is an abstract message store where we will store the message upon receipt.
Store MessageStore
}
type FilterFunc func(*Message) bool
type MatchFunc func(*Message) bool
type MapFunc func(*Message) (*Message, bool)
func MatchType[T any](m *Message) bool {
_, ok := m.Payload.(T)
return ok
}
type MessageHandler struct {
log zerolog.Logger
notifier Notifier
patterns []Pattern
}
func NewMessageHandler(log zerolog.Logger, notifier Notifier, patterns ...Pattern) *MessageHandler {
return &MessageHandler{
log: log.With().Str("component", "message_handler").Logger(),
notifier: notifier,
patterns: patterns,
}
}
// Process iterates over the internal processing patterns and determines if the payload matches.
// The _first_ matching pattern processes the payload.
// Returns
// - IncompatibleInputTypeError if no matching processor was found
// - All other errors are potential symptoms of internal state corruption or bugs (fatal).
func (e *MessageHandler) Process(originID flow.Identifier, payload interface{}) error {
msg := &Message{
OriginID: originID,
Payload: payload,
}
for _, pattern := range e.patterns {
if pattern.Match(msg) {
var keep bool
if pattern.Map != nil {
msg, keep = pattern.Map(msg)
if !keep {
return nil
}
}
ok := pattern.Store.Put(msg)
if !ok {
e.log.Warn().
Str("msg_type", logging.Type(payload)).
Hex("origin_id", originID[:]).
Msg("failed to store message - discarding")
return nil
}
e.notifier.Notify()
// message can only be matched by one pattern, and processed by one handler
return nil
}
}
return fmt.Errorf("no matching processor for message of type %T from origin %x: %w", payload, originID[:], IncompatibleInputTypeError)
}
func (e *MessageHandler) GetNotifier() <-chan struct{} {
return e.notifier.Channel()
}