forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_event_loop.sk.go
93 lines (78 loc) · 2.3 KB
/
config_event_loop.sk.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
// Code generated by solo-kit. DO NOT EDIT.
package v1
import (
"context"
"go.opencensus.io/trace"
"github.com/hashicorp/go-multierror"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/errutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/eventloop"
"github.com/solo-io/solo-kit/pkg/errors"
)
type ConfigSyncer interface {
Sync(context.Context, *ConfigSnapshot) error
}
type ConfigSyncers []ConfigSyncer
func (s ConfigSyncers) Sync(ctx context.Context, snapshot *ConfigSnapshot) error {
var multiErr *multierror.Error
for _, syncer := range s {
if err := syncer.Sync(ctx, snapshot); err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return multiErr.ErrorOrNil()
}
type configEventLoop struct {
emitter ConfigEmitter
syncer ConfigSyncer
}
func NewConfigEventLoop(emitter ConfigEmitter, syncer ConfigSyncer) eventloop.EventLoop {
return &configEventLoop{
emitter: emitter,
syncer: syncer,
}
}
func (el *configEventLoop) Run(namespaces []string, opts clients.WatchOpts) (<-chan error, error) {
opts = opts.WithDefaults()
opts.Ctx = contextutils.WithLogger(opts.Ctx, "v1.event_loop")
logger := contextutils.LoggerFrom(opts.Ctx)
logger.Infof("event loop started")
errs := make(chan error)
watch, emitterErrs, err := el.emitter.Snapshots(namespaces, opts)
if err != nil {
return nil, errors.Wrapf(err, "starting snapshot watch")
}
go errutils.AggregateErrs(opts.Ctx, errs, emitterErrs, "v1.emitter errors")
go func() {
// create a new context for each loop, cancel it before each loop
var cancel context.CancelFunc = func() {}
// use closure to allow cancel function to be updated as context changes
defer func() { cancel() }()
for {
select {
case snapshot, ok := <-watch:
if !ok {
return
}
// cancel any open watches from previous loop
cancel()
ctx, span := trace.StartSpan(opts.Ctx, "config.supergloo.solo.io.EventLoopSync")
ctx, canc := context.WithCancel(ctx)
cancel = canc
err := el.syncer.Sync(ctx, snapshot)
span.End()
if err != nil {
select {
case errs <- err:
default:
logger.Errorf("write error channel is full! could not propagate err: %v", err)
}
}
case <-opts.Ctx.Done():
return
}
}
}()
return errs, nil
}