| Jan Schär | 75ea9f4 | 2024-07-29 17:01:41 +0200 | [diff] [blame^] | 1 | // Package up is used to run a function for some duration. If a new function is |
| 2 | // added while a previous run is still ongoing, nothing new will be executed. |
| 3 | package up |
| 4 | |
| 5 | // Taken and modified from CoreDNS, under Apache 2.0. |
| 6 | |
| 7 | import ( |
| 8 | "sync" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Probe is used to run a single Func until it returns true |
| 13 | // (indicating a target is healthy). |
| 14 | // If an Func is already in progress no new one will be added, |
| 15 | // i.e. there is always a maximum of 1 checks in flight. |
| 16 | // |
| 17 | // There is a tradeoff to be made in figuring out quickly that an upstream is |
| 18 | // healthy and not doing much work (sending queries) to find that out. |
| 19 | // Having some kind of exp. backoff here won't help much, because you don't |
| 20 | // want to backoff too much. You then also need random queries to be performed |
| 21 | // every so often to quickly detect a working upstream. In the end we just send |
| 22 | // a query every 0.5 second to check the upstream. This hopefully strikes a |
| 23 | // balance between getting information about the upstream state quickly and not |
| 24 | // doing too much work. Note that 0.5s is still an eternity in DNS, so we may |
| 25 | // actually want to shorten it. |
| 26 | type Probe struct { |
| 27 | sync.Mutex |
| 28 | inprogress int |
| 29 | interval time.Duration |
| 30 | } |
| 31 | |
| 32 | // Func is used to determine if a target is alive. |
| 33 | // If so this function must return nil. |
| 34 | type Func func() error |
| 35 | |
| 36 | // New returns a pointer to an initialized Probe. |
| 37 | func New() *Probe { return &Probe{} } |
| 38 | |
| 39 | // Do will probe target, if a probe is already in progress this is a noop. |
| 40 | func (p *Probe) Do(f Func) { |
| 41 | p.Lock() |
| 42 | if p.inprogress != idle { |
| 43 | p.Unlock() |
| 44 | return |
| 45 | } |
| 46 | p.inprogress = active |
| 47 | interval := p.interval |
| 48 | p.Unlock() |
| 49 | // Passed the lock. Now run f for as long it returns false. |
| 50 | // If a true is returned we return from the goroutine |
| 51 | // and we can accept another Func to run. |
| 52 | go func() { |
| 53 | i := 1 |
| 54 | for { |
| 55 | if err := f(); err == nil { |
| 56 | break |
| 57 | } |
| 58 | time.Sleep(interval) |
| 59 | p.Lock() |
| 60 | if p.inprogress == stop { |
| 61 | p.Unlock() |
| 62 | return |
| 63 | } |
| 64 | p.Unlock() |
| 65 | i++ |
| 66 | } |
| 67 | |
| 68 | p.Lock() |
| 69 | p.inprogress = idle |
| 70 | p.Unlock() |
| 71 | }() |
| 72 | } |
| 73 | |
| 74 | // Stop stops the probing. |
| 75 | func (p *Probe) Stop() { |
| 76 | p.Lock() |
| 77 | p.inprogress = stop |
| 78 | p.Unlock() |
| 79 | } |
| 80 | |
| 81 | // Start will initialize the probe manager, |
| 82 | // after which probes can be initiated with Do. |
| 83 | func (p *Probe) Start(interval time.Duration) { |
| 84 | p.Lock() |
| 85 | p.interval = interval |
| 86 | p.Unlock() |
| 87 | } |
| 88 | |
| 89 | const ( |
| 90 | idle = iota |
| 91 | active |
| 92 | stop |
| 93 | ) |