-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.go
57 lines (48 loc) · 1.5 KB
/
service.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
package async_webhook
import (
"context"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/code/async"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/server/grpc/messaging"
sync_util "github.com/code-payments/code-server/pkg/sync"
)
type service struct {
log *logrus.Entry
conf *conf
data code_data.Provider
messagingClient messaging.InternalMessageClient
webhookLocks *sync_util.StripedLock // todo: distributed lock
metricsMu sync.Mutex
successfulWebhooks int
failedWebhooks int
}
func New(data code_data.Provider, messagingClient messaging.InternalMessageClient, configProvider ConfigProvider) async.Service {
return &service{
log: logrus.StandardLogger().WithField("service", "webhook"),
conf: configProvider(),
data: data,
messagingClient: messagingClient,
webhookLocks: sync_util.NewStripedLock(1024),
}
}
func (p *service) Start(ctx context.Context, interval time.Duration) error {
go func() {
err := p.worker(ctx, interval)
if err != nil && err != context.Canceled {
p.log.WithError(err).Warnf("webhook processing loop terminated unexpectedly")
}
}()
go func() {
err := p.metricsGaugeWorker(ctx)
if err != nil && err != context.Canceled {
p.log.WithError(err).Warn("webhook metrics gauge loop terminated unexpectedly")
}
}()
select {
case <-ctx.Done():
return ctx.Err()
}
}