1package configsource
2
3import (
4 "context"
5 "time"
6)
7
8func (c *Controller) Run(ctx context.Context, hup <-chan struct{}) {
9 var tick <-chan time.Time
10 if c.git {
11 ch, stop := c.tick(pollInterval)
12 defer stop()
13 tick = ch
14 }
15 done := make(chan struct{}, 1)
16 inflight, queued := false, false
17 start := func() {
18 inflight = true
19 go func() {
20 if err := c.Step(ctx); err != nil && ctx.Err() == nil {
21 c.logger.Warn("config step failed", "err", err)
22 }
23 done <- struct{}{}
24 }()
25 }
26 trigger := func() {
27 if inflight {
28 queued = true
29 return
30 }
31 start()
32 }
33 for {
34 select {
35 case <-ctx.Done():
36 if inflight {
37 <-done
38 }
39 return
40 case <-tick:
41 trigger()
42 case _, ok := <-hup:
43 if !ok {
44 hup = nil
45 continue
46 }
47 trigger()
48 case <-done:
49 inflight = false
50 if queued {
51 queued = false
52 start()
53 }
54 }
55 }
56}