-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
106 lines (83 loc) · 1.89 KB
/
event.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package timelimiter
import (
"fmt"
"time"
)
type EventType string
const (
SUCCESS EventType = "SUCCESS"
TIMEOUT EventType = "TIMEOUT"
PANIC EventType = "PANIC"
)
type Event interface {
fmt.Stringer
TimeLimiterName() string
CreationTime() time.Time
EventType() EventType
}
type SuccessEvent interface {
Event
}
type TimeoutEvent interface {
Event
}
type PanicEvent interface {
Event
Panic() any
}
func newSuccessEvent(timeLimiterName string) Event {
return &successEvent{event{timeLimiterName: timeLimiterName, creationTime: time.Now()}}
}
func newTimeoutEvent(timeLimiterName string) Event {
return &timeoutEvent{event{timeLimiterName: timeLimiterName, creationTime: time.Now()}}
}
func newPanicEvent(timeLimiterName string, panic any) Event {
return &panicEvent{event{timeLimiterName: timeLimiterName, creationTime: time.Now()}, panic}
}
type event struct {
timeLimiterName string
creationTime time.Time
}
func (e *event) TimeLimiterName() string {
return e.timeLimiterName
}
func (e *event) CreationTime() time.Time {
return e.creationTime
}
type successEvent struct {
event
}
func (e *successEvent) EventType() EventType {
return SUCCESS
}
func (e *successEvent) String() string {
return fmt.Sprintf(
"%v: TimeLimiter '%s' recorded a successful call.",
e.creationTime, e.timeLimiterName)
}
type timeoutEvent struct {
event
}
func (e *timeoutEvent) EventType() EventType {
return TIMEOUT
}
func (e *timeoutEvent) String() string {
return fmt.Sprintf(
"%v: TimeLimiter '%s' recorded a timeout exception.",
e.creationTime, e.timeLimiterName)
}
type panicEvent struct {
event
panic any
}
func (e *panicEvent) EventType() EventType {
return PANIC
}
func (e *panicEvent) Panic() any {
return e.panic
}
func (e *panicEvent) String() string {
return fmt.Sprintf(
"%v: TimeLimiter '%s' recorded a failure call with panic: %v.",
e.creationTime, e.timeLimiterName, e.panic)
}