()
| 31 | } |
| 32 | |
| 33 | func (c *ClientImpl) asyncInitiationLoop() { |
| 34 | var ( |
| 35 | lastSent time.Time |
| 36 | timer *time.Timer |
| 37 | timerC <-chan time.Time |
| 38 | ) |
| 39 | |
| 40 | schedule := func() { |
| 41 | firstNs := c.notifyBatchStartNs.Load() |
| 42 | if firstNs == 0 { |
| 43 | // No pending batch; stop timer if running. |
| 44 | if timer != nil { |
| 45 | if !timer.Stop() { |
| 46 | select { |
| 47 | case <-timer.C: |
| 48 | default: |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | timerC = nil |
| 53 | return |
| 54 | } |
| 55 | lastNs := c.notifyLastEventNs.Load() |
| 56 | |
| 57 | first := time.Unix(0, firstNs) |
| 58 | last := time.Unix(0, lastNs) |
| 59 | cadenceReady := lastSent.Add(NotifyMaxCadence) |
| 60 | |
| 61 | // Reset the 2ms "max debounce" window at the cadence boundary: |
| 62 | // deadline = max(first, cadenceReady) + 2ms |
| 63 | anchor := first |
| 64 | if cadenceReady.After(anchor) { |
| 65 | anchor = cadenceReady |
| 66 | } |
| 67 | deadline := anchor.Add(NotifyMaxDebounceTime) |
| 68 | |
| 69 | // candidate = min(last+500us, deadline) |
| 70 | candidate := last.Add(NotifyDebounceTime) |
| 71 | if deadline.Before(candidate) { |
| 72 | candidate = deadline |
| 73 | } |
| 74 | |
| 75 | // final target = max(cadenceReady, candidate) |
| 76 | target := candidate |
| 77 | if cadenceReady.After(target) { |
| 78 | target = cadenceReady |
| 79 | } |
| 80 | |
| 81 | d := time.Until(target) |
| 82 | if d < 0 { |
| 83 | d = 0 |
| 84 | } |
| 85 | if timer == nil { |
| 86 | timer = time.NewTimer(d) |
| 87 | } else { |
| 88 | if !timer.Stop() { |
| 89 | select { |
| 90 | case <-timer.C: |
no test coverage detected