forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_negotiation.go
73 lines (63 loc) · 2.44 KB
/
tcp_negotiation.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
package ravendb
import (
"encoding/json"
"io"
"strconv"
)
const (
outOfRangeStatus = -1
dropStatus = -2
)
func negotiateProtocolVersion(stream io.Writer, parameters *tcpNegotiateParameters) (*supportedFeatures, error) {
v := parameters.version
currentRef := &v
for {
sendTcpVersionInfo(stream, parameters, *currentRef)
version := parameters.readResponseAndGetVersionCallback(parameters.destinationUrl)
/*
if (logger.isInfoEnabled()) {
logger.info("Read response from " + ObjectUtils.firstNonNull(parameters.getSourceNodeTag(), parameters.getDestinationUrl()) + " for " + parameters.getOperation() + ", received version is '" + version + "'");
}
*/
if version == *currentRef {
break
}
//In this case we usually throw internally but for completeness we better handle it
if version == dropStatus {
return getSupportedFeaturesFor(operationDrop, dropBaseLine), nil
}
status := operationVersionSupported(parameters.operation, version, currentRef)
if status == supportedStatus_OUT_OF_RANGE {
sendTcpVersionInfo(stream, parameters, outOfRangeStatus)
return nil, newIllegalArgumentError("The " + parameters.operation + " version " + strconv.Itoa(parameters.version) + " is out of range, out lowest version is " + strconv.Itoa(*currentRef))
}
/*
if (logger.isInfoEnabled()) {
logger.info("The version " + version + " is " + status + ", will try to agree on '"
+ currentRef.value + "' for " + parameters.getOperation() + " with "
+ ObjectUtils.firstNonNull(parameters.getDestinationNodeTag(), parameters.getDestinationUrl()));
}
*/
}
/*
if (logger.isInfoEnabled()) {
logger.info(ObjectUtils.firstNonNull(parameters.getDestinationNodeTag(), parameters.getDestinationUrl()) + " agreed on version " + currentRef.value + " for " + parameters.getOperation());
}
*/
return getSupportedFeaturesFor(parameters.operation, *currentRef), nil
}
func sendTcpVersionInfo(stream io.Writer, parameters *tcpNegotiateParameters, currentVersion int) error {
/*
if (logger.isInfoEnabled()) {
logger.info("Send negotiation for " + parameters.getOperation() + " in version " + currentVersion);
}
*/
m := map[string]interface{}{
"DatabaseName": parameters.database,
"Operation": parameters.operation,
"SourceNodeTag": parameters.sourceNodeTag,
"OperationVersion": currentVersion,
}
enc := json.NewEncoder(stream)
return enc.Encode(m)
}