-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
49 lines (39 loc) · 1.42 KB
/
router.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
package kafka
import (
"context"
"errors"
"fmt"
)
// Router extends the [Handler] interface to include routing functionality.
type Router interface {
Handler
Route(ctx context.Context, msg Message) (Handler, error)
}
// RouterFunc is an adapter type that allow the use of ordinary function as a [Router].
type RouterFunc func(ctx context.Context, msg Message) (Handler, error)
// Route calls itself passing all arguments through.
func (fn RouterFunc) Route(ctx context.Context, msg Message) (Handler, error) {
return fn(ctx, msg)
}
// Handle delegates message processing to the appropriate [Handler] determined by the Route method.
func (fn RouterFunc) Handle(ctx context.Context, msg Message) error {
h, err := fn(ctx, msg)
if err != nil {
return fmt.Errorf("failed to resolve handler: %w", err)
}
return h.Handle(ctx, msg)
}
// StaticRouterByTopic routes messages to handlers based on the topic.
type StaticRouterByTopic map[string]Handler
// Route resolves a [Handler] based on the topic of the [Message].
func (r StaticRouterByTopic) Route(_ context.Context, msg Message) (Handler, error) {
h, ok := r[msg.Topic]
if !ok {
return nil, errors.New("failed resolve handler by topic")
}
return h, nil
}
// Handle delegates message processing to the appropriate [Handler] determined by the Route method.
func (r StaticRouterByTopic) Handle(ctx context.Context, msg Message) error {
return RouterFunc(r.Route).Handle(ctx, msg)
}