-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanager.go
138 lines (114 loc) · 2.62 KB
/
manager.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package loafergo
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
const messageBufferFactor = 2
// Manager holds the routes and config fields
type Manager struct {
config *Config
routes []Router
}
// NewManager creates a new Manager with the given configuration
func NewManager(config *Config) *Manager {
cfg := loadConfig(config)
return &Manager{config: cfg}
}
// RegisterRoute register a new route to the Manager
func (m *Manager) RegisterRoute(route Router) {
m.routes = append(m.routes, route)
}
// RegisterRoutes register more than one route to the Manager
func (m *Manager) RegisterRoutes(routes []Router) {
m.routes = append(m.routes, routes...)
}
// Run the Manager distributing the worker pool by the number of routes
// returns errors if no routes
func (m *Manager) Run(ctx context.Context) error {
if len(m.routes) == 0 {
return ErrNoRoute
}
var wg sync.WaitGroup
wg.Add(len(m.routes))
for _, r := range m.routes {
err := r.Configure(ctx)
if err != nil {
m.config.Logger.Log(err)
return err
}
go func() {
m.processRoute(ctx, r)
wg.Done()
}()
}
// wait for all routes to finish
wg.Wait()
return nil
}
func (m *Manager) processRoute(ctx context.Context, r Router) {
size := r.WorkerPoolSize(ctx)
message := make(chan Message, size*messageBufferFactor)
defer close(message)
for w := int32(1); w <= size; w++ {
go m.worker(ctx, r, message)
}
m.config.Logger.Log("\nconsumers is ready to consume messages...")
for {
if errors.Is(ctx.Err(), context.Canceled) {
m.config.Logger.Log("context canceled process route stopped")
break
}
msgs, err := r.GetMessages(ctx)
if err != nil {
m.config.Logger.Log(
fmt.Sprintf(
"%s , retrying in %fs",
ErrGetMessage.Context(err).Error(),
m.config.RetryTimeout.Seconds(),
),
)
time.Sleep(m.config.RetryTimeout)
continue
}
for _, msg := range msgs {
message <- msg
}
}
}
func (m *Manager) worker(ctx context.Context, r Router, msg <-chan Message) {
for v := range msg {
err := r.HandlerMessage(ctx, v)
if err != nil {
m.config.Logger.Log(err)
continue
}
err = r.Commit(ctx, v)
if err != nil {
m.config.Logger.Log(err)
continue
}
}
}
// GetRoutes returns the available routes as a slice of a Router type
func (m *Manager) GetRoutes() []Router {
return m.routes
}
func loadConfig(config *Config) *Config {
cfg := &Config{
Logger: newDefaultLogger(),
RetryTimeout: defaultRetryTimeout,
}
if config == nil {
return cfg
}
if config.Logger != nil {
cfg.Logger = config.Logger
}
if config.RetryTimeout > 0 {
cfg.RetryTimeout = config.RetryTimeout
}
return cfg
}