-
Notifications
You must be signed in to change notification settings - Fork 2
/
bk_error_info.go
61 lines (53 loc) · 1.53 KB
/
bk_error_info.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
// Copyright 2022 GoFast Author(http://chende.ren). All rights reserved.
// Use of this source code is governed by a MIT license
package fuse
import (
"github.com/qinchende/gofast/skill/mathx"
"strings"
"time"
)
const (
numHistoryReasons = 3
timeFormatReason = "15:04:05#"
)
// 错误信息滑动窗口
type errorWindow struct {
reasonsTime [numHistoryReasons]time.Time
reasons [numHistoryReasons]string
index int
count int
// add by cd.net on 20221116 没必要加锁,量大的时候影响性能
//lock sync.Mutex
}
func (ew *errorWindow) add(reason string) {
//ew.lock.Lock()
ew.reasonsTime[ew.index] = time.Now()
ew.reasons[ew.index] = reason
ew.index = (ew.index + 1) % numHistoryReasons
ew.count = mathx.MinInt(ew.count+1, numHistoryReasons)
//ew.lock.Unlock()
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var (
tmpReasonMem = [numHistoryReasons]string{}
tmpReasons []string
)
// 注意:整体上这个是非线程安全的
func (ew *errorWindow) Errors() []string {
count := 0
//ew.lock.Lock()
for i := ew.index - 1; i >= ew.index-ew.count; i-- {
idx := (i + numHistoryReasons) % numHistoryReasons
tmpReasonMem[count] = ew.reasonsTime[idx].Format(timeFormatReason) + ew.reasons[idx]
count++
}
//ew.lock.Unlock()
tmpReasons = tmpReasonMem[0:count]
defer func() {
tmpReasons = tmpReasons[0:0]
}()
return tmpReasons
}
func (ew *errorWindow) ErrorsJoin(sep string) string {
return strings.Join(ew.Errors(), sep)
}