-
Notifications
You must be signed in to change notification settings - Fork 39
/
error_handler.go
76 lines (61 loc) · 1.18 KB
/
error_handler.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
package main
import (
"context"
"log"
"sync"
"time"
"github.com/bluenviron/gomavlib/v3"
)
type errorHandler struct {
ctx context.Context
wg *sync.WaitGroup
printSingleErrors bool
errorCount int
errorCountMutex sync.Mutex
}
func newErrorHandler(
ctx context.Context,
wg *sync.WaitGroup,
printSingleErrors bool,
) (*errorHandler, error) {
eh := &errorHandler{
ctx: ctx,
wg: wg,
printSingleErrors: printSingleErrors,
}
wg.Add(1)
go eh.run()
return eh, nil
}
func (eh *errorHandler) run() {
defer eh.wg.Done()
if eh.printSingleErrors {
return
}
t := time.NewTicker(5 * time.Second)
defer t.Stop()
for {
select {
case <-t.C:
func() {
eh.errorCountMutex.Lock()
defer eh.errorCountMutex.Unlock()
if eh.errorCount > 0 {
log.Printf("%d errors in the last 5 seconds", eh.errorCount)
eh.errorCount = 0
}
}()
case <-eh.ctx.Done():
return
}
}
}
func (eh *errorHandler) onEventError(evt *gomavlib.EventParseError) {
if eh.printSingleErrors {
log.Printf("ERR: %s", evt.Error)
return
}
eh.errorCountMutex.Lock()
defer eh.errorCountMutex.Unlock()
eh.errorCount++
}