forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_control.go
More file actions
200 lines (172 loc) · 4.09 KB
/
transfer_control.go
File metadata and controls
200 lines (172 loc) · 4.09 KB
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
package connect
import (
"context"
"sync"
"github.com/golang/glog"
"github.com/urnetwork/connect/protocol"
)
// control sync is a pattern to sync control messages between the server and client
// it ensures:
// - control messages are sent in order
// - only the latest message per scope is retried.
// Create one `ControlSync` object per scope.
// - if a send fails due to ack timeout or other local error, the send is retried
type ControlSync struct {
ctx context.Context
cancel context.CancelFunc
client *Client
scopeTag string
monitor *Monitor
sendLock sync.Mutex
syncCount uint64
}
func NewControlSync(ctx context.Context, client *Client, scopeTag string) *ControlSync {
cancelCtx, cancel := context.WithCancel(ctx)
return &ControlSync{
ctx: cancelCtx,
cancel: cancel,
client: client,
scopeTag: scopeTag,
monitor: NewMonitor(),
syncCount: 0,
}
}
func (self *ControlSync) Send(frame *protocol.Frame, updateFrame func() *protocol.Frame, ackCallback AckFunction) {
// 1. try to send non-blocking
// 2. if fails, send blocking with no timeout
// 3. keep retying on error until the handle context or client is closed
safeAckCallback := func(err error) {
if ackCallback != nil {
HandleError(func() {
ackCallback(err)
})
}
}
handleCtx, handleCancel := context.WithCancel(self.ctx)
self.sendLock.Lock()
defer self.sendLock.Unlock()
self.syncCount += 1
syncIndex := self.syncCount
notify := self.monitor.NotifyAll()
go func() {
defer handleCancel()
for {
done := false
select {
case <-notify:
func() {
self.sendLock.Lock()
defer self.sendLock.Unlock()
done = syncIndex != self.syncCount
}()
case <-handleCtx.Done():
done = true
}
if done {
return
}
}
}()
var controlSync func(*protocol.Frame)
controlSync = func(updatedFrame *protocol.Frame) {
defer handleCancel()
defer func() {
self.sendLock.Lock()
defer self.sendLock.Unlock()
if self.syncCount == syncIndex {
glog.V(2).Infof("[control][%d]stop sync for scope = %s\n", syncIndex, self.scopeTag)
} else {
glog.V(2).Infof("[control][%d]replace sync for scope = %s\n", syncIndex, self.scopeTag)
}
}()
for {
glog.V(2).Infof("[control][%d]start sync for scope = %s\n", syncIndex, self.scopeTag)
done := false
success := false
var err error
func() {
self.sendLock.Lock()
defer self.sendLock.Unlock()
select {
case <-handleCtx.Done():
done = true
default:
done = syncIndex != self.syncCount
}
if done {
return
}
updatedFrameCopy := &protocol.Frame{
MessageType: updatedFrame.MessageType,
MessageBytes: MessagePoolShareReadOnly(updatedFrame.MessageBytes),
}
success, err = self.client.SendWithTimeoutDetailed(
updatedFrameCopy,
DestinationId(ControlId),
func(err error) {
if err == nil {
safeAckCallback(nil)
MessagePoolReturn(updatedFrame.MessageBytes)
} else {
go controlSync(updatedFrame)
}
},
-1,
Ctx(handleCtx),
)
}()
if done {
MessagePoolReturn(updatedFrame.MessageBytes)
return
}
if success {
return
}
if err != nil {
// only stop when the context or client is done
select {
case <-handleCtx.Done():
MessagePoolReturn(frame.MessageBytes)
return
case <-self.client.Done():
MessagePoolReturn(frame.MessageBytes)
return
default:
}
}
// else try again
if updateFrame != nil {
f := updateFrame()
if f != updatedFrame {
MessagePoolReturn(updatedFrame.MessageBytes)
updatedFrame = f
}
}
}
}
frameCopy := &protocol.Frame{
MessageType: frame.MessageType,
MessageBytes: MessagePoolShareReadOnly(frame.MessageBytes),
}
success := self.client.SendWithTimeout(
frameCopy,
DestinationId(ControlId),
func(err error) {
if err == nil {
safeAckCallback(nil)
MessagePoolReturn(frame.MessageBytes)
} else {
go controlSync(frame)
}
},
0,
Ctx(handleCtx),
)
if success {
return
}
go controlSync(frame)
}
func (self *ControlSync) Close() {
self.cancel()
}