This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
controller.go
125 lines (98 loc) · 2.47 KB
/
controller.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
package controller
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/zalando-incubator/mate/consumers"
"github.com/zalando-incubator/mate/pkg"
"github.com/zalando-incubator/mate/producers"
)
const (
defaultSyncPeriod = 1 * time.Minute
)
type Controller struct {
producer producers.Producer
consumer consumers.Consumer
options *Options
// results is used to pass endpoints from producer to consumer
results chan *pkg.Endpoint
// errors can be used by producers and consumers to report errors up
errors chan error
// done is used to send termination signals to nested goroutines
done chan struct{}
// wg keeps track of running goroutines
wg sync.WaitGroup
}
type Options struct {
syncPeriod time.Duration
SyncOnly bool
}
func New(producer producers.Producer, consumer consumers.Consumer, options *Options) *Controller {
if options == nil {
options = &Options{}
}
if options.syncPeriod == 0 {
options.syncPeriod = defaultSyncPeriod
}
return &Controller{
producer: producer,
consumer: consumer,
options: options,
results: make(chan *pkg.Endpoint),
errors: make(chan error),
done: make(chan struct{}),
}
}
func (c *Controller) Run() chan error {
go c.Synchronize()
if !c.options.SyncOnly {
go c.Watch()
}
return c.errors
}
func (c *Controller) Wait() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Info("Shutdown signal received, exiting...")
close(c.done)
c.wg.Wait()
}
func (c *Controller) Synchronize() {
c.wg.Add(1)
defer c.wg.Done()
for {
log.Debugf("[Synchronize] Sleeping for %s...", c.options.syncPeriod)
select {
case <-time.After(c.options.syncPeriod):
case <-c.done:
log.Info("[Synchronize] Exited synchronization loop.")
return
}
log.Infoln("[Synchronize] Synchronizing DNS entries...")
endpoints, err := c.producer.Endpoints()
if err != nil {
c.errors <- fmt.Errorf("[Synchronize] Error getting endpoints from producer: %v", err)
continue
}
err = c.consumer.Sync(endpoints)
if err != nil {
c.errors <- fmt.Errorf("[Synchronize] Error consuming endpoints: %v", err)
continue
}
}
}
func (c *Controller) Watch() {
go c.monitorProducer()
go c.consumeEndpoints()
}
func (c *Controller) monitorProducer() {
c.producer.Monitor(c.results, c.errors, c.done, &c.wg)
}
func (c *Controller) consumeEndpoints() {
c.consumer.Consume(c.results, c.errors, c.done, &c.wg)
}