-
Notifications
You must be signed in to change notification settings - Fork 60
/
router.go
247 lines (201 loc) · 6.43 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package router
import (
"fmt"
"net/url"
"reflect"
"strings"
"time"
t "github.com/containrrr/shoutrrr/pkg/types"
)
// ServiceRouter is responsible for routing a message to a specific notification service using the notification URL
type ServiceRouter struct {
logger t.StdLogger
services []t.Service
queue []string
Timeout time.Duration
}
// New creates a new service router using the specified logger and service URLs
func New(logger t.StdLogger, serviceURLs ...string) (*ServiceRouter, error) {
router := ServiceRouter{
logger: logger,
Timeout: 10 * time.Second,
}
for _, serviceURL := range serviceURLs {
if err := router.AddService(serviceURL); err != nil {
return nil, fmt.Errorf("error initializing router services: %s", err)
}
}
return &router, nil
}
// AddService initializes the specified service from its URL, and adds it if no errors occur
func (router *ServiceRouter) AddService(serviceURL string) error {
service, err := router.initService(serviceURL)
if err == nil {
router.services = append(router.services, service)
}
return err
}
// Send sends the specified message using the routers underlying services
func (router *ServiceRouter) Send(message string, params *t.Params) []error {
if router == nil {
return []error{fmt.Errorf("error sending message: no senders")}
}
serviceCount := len(router.services)
errors := make([]error, serviceCount)
results := router.SendAsync(message, params)
for i := range router.services {
errors[i] = <-results
}
return errors
}
// SendItems sends the specified message items using the routers underlying services
func (router *ServiceRouter) SendItems(items []t.MessageItem, params t.Params) []error {
if router == nil {
return []error{fmt.Errorf("error sending message: no senders")}
}
// Fallback using old API for now
message := strings.Builder{}
for _, item := range items {
message.WriteString(item.Text)
}
serviceCount := len(router.services)
errors := make([]error, serviceCount)
results := router.SendAsync(message.String(), ¶ms)
for i := range router.services {
errors[i] = <-results
}
return errors
}
// SendAsync sends the specified message using the routers underlying services
func (router *ServiceRouter) SendAsync(message string, params *t.Params) chan error {
serviceCount := len(router.services)
proxy := make(chan error, serviceCount)
errors := make(chan error, serviceCount)
if params == nil {
params = &t.Params{}
}
for _, service := range router.services {
go sendToService(service, proxy, router.Timeout, message, *params)
}
go func() {
for i := 0; i < serviceCount; i++ {
errors <- <-proxy
}
close(errors)
}()
return errors
}
func sendToService(service t.Service, results chan error, timeout time.Duration, message string, params t.Params) {
result := make(chan error)
// TODO: There really ought to be a better way to name the services
pkg := reflect.TypeOf(service).Elem().PkgPath()
serviceName := pkg[strings.LastIndex(pkg, "/")+1:]
go func() { result <- service.Send(message, ¶ms) }()
select {
case res := <-result:
results <- res
case <-time.After(timeout):
results <- fmt.Errorf("failed to send using %v: timed out", serviceName)
}
}
// Enqueue adds the message to an internal queue and sends it when Flush is invoked
func (router *ServiceRouter) Enqueue(message string, v ...interface{}) {
if len(v) > 0 {
message = fmt.Sprintf(message, v...)
}
router.queue = append(router.queue, message)
}
// Flush sends all messages that have been queued up as a combined message. This method should be deferred!
func (router *ServiceRouter) Flush(params *t.Params) {
// Since this method is supposed to be deferred we just have to ignore errors
_ = router.Send(strings.Join(router.queue, "\n"), params)
router.queue = []string{}
}
// SetLogger sets the logger that the services will use to write progress logs
func (router *ServiceRouter) SetLogger(logger t.StdLogger) {
router.logger = logger
for _, service := range router.services {
service.SetLogger(logger)
}
}
// ExtractServiceName from a notification URL
func (router *ServiceRouter) ExtractServiceName(rawURL string) (string, *url.URL, error) {
serviceURL, err := url.Parse(rawURL)
if err != nil {
return "", &url.URL{}, err
}
scheme := serviceURL.Scheme
schemeParts := strings.Split(scheme, "+")
if len(schemeParts) > 1 {
scheme = schemeParts[0]
}
return scheme, serviceURL, nil
}
// Route a message to a specific notification service using the notification URL
func (router *ServiceRouter) Route(rawURL string, message string) error {
service, err := router.Locate(rawURL)
if err != nil {
return err
}
return service.Send(message, nil)
}
func (router *ServiceRouter) initService(rawURL string) (t.Service, error) {
scheme, configURL, err := router.ExtractServiceName(rawURL)
if err != nil {
return nil, err
}
service, err := newService(scheme)
if err != nil {
return nil, err
}
if configURL.Scheme != scheme {
router.log("Got custom URL:", configURL.String())
customURLService, ok := service.(t.CustomURLService)
if !ok {
return nil, fmt.Errorf("custom URLs are not supported by '%s' service", scheme)
}
configURL, err = customURLService.GetConfigURLFromCustom(configURL)
if err != nil {
return nil, err
}
router.log("Converted service URL:", configURL.String())
}
err = service.Initialize(configURL, router.logger)
if err != nil {
return service, err
}
return service, nil
}
// NewService returns a new uninitialized service instance
func (*ServiceRouter) NewService(serviceScheme string) (t.Service, error) {
return newService(serviceScheme)
}
// newService returns a new uninitialized service instance
func newService(serviceScheme string) (t.Service, error) {
serviceFactory, valid := serviceMap[strings.ToLower(serviceScheme)]
if !valid {
return nil, fmt.Errorf("unknown service %q", serviceScheme)
}
return serviceFactory(), nil
}
// ListServices returns the available services
func (router *ServiceRouter) ListServices() []string {
services := make([]string, len(serviceMap))
i := 0
for key := range serviceMap {
services[i] = key
i++
}
return services
}
// Locate returns the service implementation that corresponds to the given service URL
func (router *ServiceRouter) Locate(rawURL string) (t.Service, error) {
service, err := router.initService(rawURL)
return service, err
}
func (router *ServiceRouter) log(v ...interface{}) {
if router.logger == nil {
return
}
router.logger.Println(v...)
}