-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
226 lines (183 loc) · 4.08 KB
/
adapter.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package art
import (
"reflect"
"sync/atomic"
)
type AdapterHub interface {
Join(adapterId string, adp IAdapter) error
RemoveOne(filter func(IAdapter) bool)
}
type Prosumer interface {
Producer
Consumer
}
type Producer interface {
IAdapter
Send(messages ...*Message) error
RawSend(messages ...*Message) error
}
type Consumer interface {
IAdapter
Listen() (err error)
}
type IAdapter interface {
Identifier() string
Log() Logger
SetLog(Logger)
OnDisconnect(terminates ...func(adp IAdapter))
Stop() error
IsStopped() bool // IsStopped is used for polling
WaitStop() chan struct{} // WaitStop is used for event push
RawInfra() any
}
type Adapter struct {
ingressMux *Mux
rawRecv func(Logger) (*Message, error)
egressMux *Mux
rawSend func(Logger, *Message) error
rawStop func(Logger) error
fixupMaxRetrySecond int
rawFixup func(IAdapter) error
// WaitPingSendPong or SendPingWaitPong
pp func() error
identifier string
logger Logger
application IAdapter
lifecycle *Lifecycle
rawInfra any
hub AdapterHub
recvResult chan error
isStopped atomic.Bool
waitStop chan struct{}
}
func (adp *Adapter) Log() Logger {
return adp.logger
}
func (adp *Adapter) SetLog(logger Logger) {
adp.logger = logger
}
func (adp *Adapter) pingpong() {
go func() {
if adp.pp == nil {
return
}
var Err error
defer func() {
if !adp.isStopped.Load() {
adp.Stop()
}
if Err != nil {
adp.logger.Error("art.Adapter pingpong: %v", Err)
}
adp.recvResult <- Err
}()
if adp.rawFixup == nil {
Err = adp.pp()
return
}
Err = ReliableTask(
adp.pp,
adp.IsStopped,
adp.fixupMaxRetrySecond,
func() error { return adp.rawFixup(adp.application) },
)
}()
}
func (adp *Adapter) Identifier() string { return adp.identifier }
func (adp *Adapter) OnDisconnect(terminates ...func(adp IAdapter)) {
adp.lifecycle.OnDisconnect(terminates...)
}
func (adp *Adapter) Listen() (err error) {
if adp.rawRecv == nil {
return nil
}
if adp.isStopped.Load() {
return ErrorWrapWithMessage(ErrClosed, "art.Adapter Listen")
}
go func() {
var Err error
defer func() {
if !adp.isStopped.Load() {
adp.Stop()
}
if Err != nil {
adp.logger.Error("art.Adapter Listen: %v", Err)
}
adp.recvResult <- Err
}()
if adp.rawFixup == nil {
Err = adp.listen()
return
}
Err = ReliableTask(
adp.listen,
adp.IsStopped,
adp.fixupMaxRetrySecond,
func() error { return adp.rawFixup(adp.application) },
)
}()
return <-adp.recvResult
}
func (adp *Adapter) listen() error {
for !adp.isStopped.Load() {
ingress, err := adp.rawRecv(adp.logger)
if adp.isStopped.Load() {
return nil
}
if err != nil {
return err
}
err = adp.ingressMux.HandleMessage(ingress, adp.application)
if err != nil {
}
}
return nil
}
func (adp *Adapter) Send(messages ...*Message) error {
if adp.isStopped.Load() {
return ErrorWrapWithMessage(ErrClosed, "art.Adapter Send")
}
for _, egress := range messages {
if adp.egressMux == nil {
return nil
}
err := adp.egressMux.HandleMessage(egress, adp.application)
if err != nil {
return err
}
}
return nil
}
func (adp *Adapter) RawSend(messages ...*Message) error {
if adp.isStopped.Load() {
return ErrorWrapWithMessage(ErrClosed, "art.Adapter RawSend")
}
for _, egress := range messages {
if adp.rawSend == nil {
return nil
}
err := adp.rawSend(adp.logger, egress)
if err != nil {
return err
}
}
return nil
}
func (adp *Adapter) Stop() error {
if adp.isStopped.Swap(true) {
return ErrorWrapWithMessage(ErrClosed, "repeated execute stop")
}
err := adp.rawStop(adp.logger)
if adp.hub != nil && !reflect.ValueOf(adp.hub).IsZero() {
adp.hub.RemoveOne(func(adapter IAdapter) bool { return adapter == adp.application })
}
adp.lifecycle.asyncTerminate(adp.application)
adp.lifecycle.wait()
close(adp.waitStop)
return err
}
func (adp *Adapter) IsStopped() bool { return adp.isStopped.Load() }
func (adp *Adapter) WaitStop() chan struct{} { return adp.waitStop }
func (adp *Adapter) RawInfra() any {
return adp.rawInfra
}