forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_connection_header_message.go
243 lines (201 loc) · 6.76 KB
/
tcp_connection_header_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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ravendb
import (
"fmt"
"strconv"
)
type operationTypes = string
const (
operationNone = "None"
operationDrop = "Drop"
operationSubscription = "Subscription"
operationReplication = "Replication"
operationCluster = "Cluster"
operationHeartbeats = "Heartbeats"
operationPing = "Ping"
operationTestConnection = "TestConnection"
)
const (
numberOfRetriesForSendingTCPHeader = 2
pingBaseLine = -1
noneBaseLine = -1
dropBaseLine = -2
hearthbeatsBaseLine = 20
subscriptionBaseLine = 40
testConnectionBaseLine = 50
heartbeatsTCPVersion = hearthbeatsBaseLine
subscriptionTCPVersion = subscriptionBaseLine
testConnectionTCPVersion = testConnectionBaseLine
)
type tcpConnectionHeaderMessage struct {
DatabaseName string `json:"DatabaseName"`
SourceNodeTag string `json:"SourceNodeTag"`
Operation operationTypes `json:"Operation"`
OperationVersion int `json:"OperationVersion"`
Info string `json:"Info"`
}
type pingFeatures struct {
baseLine bool
}
func newPingFeatures() *pingFeatures {
return &pingFeatures{
baseLine: true,
}
}
type noneFeatures struct {
baseLine bool
}
func newNoneFeatures() *noneFeatures {
return &noneFeatures{
baseLine: true,
}
}
type dropFeatures struct {
baseLine bool
}
func newDropFeatures() *dropFeatures {
return &dropFeatures{
baseLine: true,
}
}
type subscriptionFeatures struct {
baseLine bool
}
func newSubscriptionFeatures() *subscriptionFeatures {
return &subscriptionFeatures{
baseLine: true,
}
}
type heartbeatsFeatures struct {
baseLine bool
}
func newHeartbeatsFeatures() *heartbeatsFeatures {
return &heartbeatsFeatures{
baseLine: true,
}
}
type testConnectionFeatures struct {
baseLine bool
}
func newTestConnectionFeatures() *testConnectionFeatures {
return &testConnectionFeatures{
baseLine: true,
}
}
type replicationFeatures struct {
baseLine bool
missingAttachments bool
}
func newReplicationFeatures() *replicationFeatures {
return &replicationFeatures{
baseLine: true,
}
}
type supportedFeatures struct {
protocolVersion int
ping *pingFeatures
none *noneFeatures
drop *dropFeatures
subscription *subscriptionFeatures
heartbeats *heartbeatsFeatures
testConnection *testConnectionFeatures
}
func newSupportedFeatures(version int) *supportedFeatures {
return &supportedFeatures{
protocolVersion: version,
}
}
var (
operationsToSupportedProtocolVersions = map[operationTypes][]int{}
supportedFeaturesByProtocol = map[operationTypes]map[int]*supportedFeatures{}
)
func init() {
operationsToSupportedProtocolVersions[operationPing] = []int{pingBaseLine}
operationsToSupportedProtocolVersions[operationNone] = []int{noneBaseLine}
operationsToSupportedProtocolVersions[operationDrop] = []int{dropBaseLine}
operationsToSupportedProtocolVersions[operationSubscription] = []int{subscriptionBaseLine}
operationsToSupportedProtocolVersions[operationHeartbeats] = []int{hearthbeatsBaseLine}
operationsToSupportedProtocolVersions[operationTestConnection] = []int{testConnectionBaseLine}
pingFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationPing] = pingFeaturesMap
pingFeatures := newSupportedFeatures(pingBaseLine)
pingFeatures.ping = newPingFeatures()
pingFeaturesMap[pingBaseLine] = pingFeatures
noneFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationNone] = noneFeaturesMap
noneFeatures := newSupportedFeatures(noneBaseLine)
noneFeatures.none = newNoneFeatures()
noneFeaturesMap[noneBaseLine] = noneFeatures
dropFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationDrop] = dropFeaturesMap
dropFeatures := newSupportedFeatures(dropBaseLine)
dropFeatures.drop = newDropFeatures()
dropFeaturesMap[dropBaseLine] = dropFeatures
subscriptionFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationSubscription] = subscriptionFeaturesMap
subscriptionFeatures := newSupportedFeatures(subscriptionBaseLine)
subscriptionFeatures.subscription = newSubscriptionFeatures()
subscriptionFeaturesMap[subscriptionBaseLine] = subscriptionFeatures
heartbeatsFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationHeartbeats] = heartbeatsFeaturesMap
heartbeatsFeatures := newSupportedFeatures(hearthbeatsBaseLine)
heartbeatsFeatures.heartbeats = newHeartbeatsFeatures()
heartbeatsFeaturesMap[hearthbeatsBaseLine] = heartbeatsFeatures
testConnectionFeaturesMap := map[int]*supportedFeatures{}
supportedFeaturesByProtocol[operationTestConnection] = testConnectionFeaturesMap
testConnectionFeatures := newSupportedFeatures(testConnectionBaseLine)
testConnectionFeatures.testConnection = newTestConnectionFeatures()
testConnectionFeaturesMap[testConnectionBaseLine] = testConnectionFeatures
}
var (
// validate
operations = []operationTypes{
operationCluster,
operationDrop,
operationHeartbeats,
operationNone,
operationPing,
operationReplication,
operationSubscription,
operationTestConnection,
}
)
type supportedStatus int
const (
supportedStatus_OUT_OF_RANGE supportedStatus = iota
supportedStatus_NOT_SUPPORTED
supportedStatus_SUPPORTED
)
func operationVersionSupported(operationType operationTypes, version int, currentRef *int) supportedStatus {
*currentRef = -1
supportedProtocols := operationsToSupportedProtocolVersions[operationType]
panicIf(len(supportedProtocols) == 0, "This is a bug. Probably you forgot to add '"+operationType+"' operation to the operationsToSupportedProtocolVersions map")
for i := 0; i < len(supportedProtocols); i++ {
*currentRef = supportedProtocols[i]
if *currentRef == version {
return supportedStatus_SUPPORTED
}
if *currentRef < version {
return supportedStatus_NOT_SUPPORTED
}
}
return supportedStatus_OUT_OF_RANGE
}
func getOperationTcpVersion(operationType operationTypes, index int) int {
// we don't check the if the index go out of range, since this is expected and means that we don't have
switch operationType {
case operationPing, operationNone:
return -1
case operationDrop:
return -2
case operationSubscription, operationReplication, operationCluster,
operationHeartbeats, operationTestConnection:
return operationsToSupportedProtocolVersions[operationType][index]
default:
panic(fmt.Sprintf("invalid operationType '%v'", operationType))
}
}
func getSupportedFeaturesFor(typ operationTypes, protocolVersion int) *supportedFeatures {
features := supportedFeaturesByProtocol[typ][protocolVersion]
panicIf(features == nil, typ+" in protocol "+strconv.Itoa(protocolVersion)+" was not found in the features set")
return features
}