-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
100 lines (85 loc) · 1.66 KB
/
message.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
package gotcc
import (
"strings"
"sync"
)
type message struct {
senderId uint32
senderName string
value interface{}
}
type ErrorMessage struct {
TaskName string
Error error
}
func newErrorMessage(taskName string, err error) *ErrorMessage {
return &ErrorMessage{
TaskName: taskName,
Error: err,
}
}
type errorLisk struct {
lock sync.Mutex
items []*ErrorMessage
}
func (el *errorLisk) append(em *ErrorMessage) {
el.lock.Lock()
el.items = append(el.items, em)
el.lock.Unlock()
}
func (el *errorLisk) reset() {
el.lock.Lock()
el.items = []*ErrorMessage{}
el.lock.Unlock()
}
func (el *errorLisk) String() string {
var sb strings.Builder
el.lock.Lock()
for i := range el.items {
sb.WriteString(el.items[i].TaskName)
sb.WriteString(": ")
sb.WriteString(el.items[i].Error.Error())
sb.WriteString("\n")
}
el.lock.Unlock()
return sb.String()
}
type StateMessage struct {
TaskName string
State State
}
type State interface {
String() string
}
func newStateMessage(taskName string, state State) *StateMessage {
return &StateMessage{
TaskName: taskName,
State: state,
}
}
type cancelList struct {
lock sync.Mutex
items []*StateMessage
}
func (cl *cancelList) append(sm *StateMessage) {
cl.lock.Lock()
cl.items = append(cl.items, sm)
cl.lock.Unlock()
}
func (cl *cancelList) reset() {
cl.lock.Lock()
cl.items = []*StateMessage{}
cl.lock.Unlock()
}
func (cl *cancelList) String() string {
var sb strings.Builder
cl.lock.Lock()
for i := range cl.items {
sb.WriteString(cl.items[i].TaskName)
sb.WriteString(": ")
sb.WriteString(cl.items[i].State.String())
sb.WriteString("\n")
}
cl.lock.Unlock()
return sb.String()
}