(ctx context.Context)
| 78 | } |
| 79 | |
| 80 | func (h *failureHandler) Serve(ctx context.Context) error { |
| 81 | cfg := h.cfg.Subscribe(h) |
| 82 | defer h.cfg.Unsubscribe(h) |
| 83 | url, sub, evChan := h.applyOpts(cfg.Options, nil) |
| 84 | |
| 85 | var err error |
| 86 | timer := time.NewTimer(minDelay) |
| 87 | resetTimer := make(chan struct{}) |
| 88 | for err == nil { |
| 89 | select { |
| 90 | case opts := <-h.optsChan: |
| 91 | url, sub, evChan = h.applyOpts(opts, sub) |
| 92 | case e, ok := <-evChan: |
| 93 | if !ok { |
| 94 | // Just to be safe - shouldn't ever happen, as |
| 95 | // evChan is set to nil when unsubscribing. |
| 96 | h.addReport(contract.FailureData{Description: evChanClosed}, time.Now()) |
| 97 | evChan = nil |
| 98 | continue |
| 99 | } |
| 100 | var data contract.FailureData |
| 101 | switch d := e.Data.(type) { |
| 102 | case string: |
| 103 | data.Description = d |
| 104 | case contract.FailureData: |
| 105 | data = d |
| 106 | default: |
| 107 | // Same here, shouldn't ever happen. |
| 108 | h.addReport(contract.FailureData{Description: invalidEventDataType}, time.Now()) |
| 109 | continue |
| 110 | } |
| 111 | h.addReport(data, e.Time) |
| 112 | case <-timer.C: |
| 113 | reports := make([]contract.FailureReport, 0, len(h.buf)) |
| 114 | now := time.Now() |
| 115 | for descr, stat := range h.buf { |
| 116 | if now.Sub(stat.last) > minDelay || now.Sub(stat.first) > maxDelay { |
| 117 | reports = append(reports, newFailureReport(stat)) |
| 118 | delete(h.buf, descr) |
| 119 | } |
| 120 | } |
| 121 | if len(reports) > 0 { |
| 122 | // Lets keep process events/configs while it might be timing out for a while |
| 123 | go func() { |
| 124 | sendFailureReports(ctx, reports, url) |
| 125 | select { |
| 126 | case resetTimer <- struct{}{}: |
| 127 | case <-ctx.Done(): |
| 128 | } |
| 129 | }() |
| 130 | } else { |
| 131 | timer.Reset(minDelay) |
| 132 | } |
| 133 | case <-resetTimer: |
| 134 | timer.Reset(minDelay) |
| 135 | case <-ctx.Done(): |
| 136 | err = ctx.Err() |
| 137 | } |
nothing calls this directly
no test coverage detected