-
Notifications
You must be signed in to change notification settings - Fork 642
CASSGO-97 Protocol version negotiation doesn't work if server replies with stream id different than 0 #1920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,6 @@ import ( | |
| "math/rand" | ||
| "net" | ||
| "os" | ||
| "regexp" | ||
| "strconv" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
@@ -202,56 +201,12 @@ func shuffleHosts(hosts []*HostInfo) []*HostInfo { | |
| return shuffled | ||
| } | ||
|
|
||
| // this is going to be version dependant and a nightmare to maintain :( | ||
| var protocolSupportRe = regexp.MustCompile(`the lowest supported version is \d+ and the greatest is (\d+)$`) | ||
| var betaProtocolRe = regexp.MustCompile(`Beta version of the protocol used \(.*\), but USE_BETA flag is unset`) | ||
|
|
||
| func parseProtocolFromError(err error) int { | ||
| errStr := err.Error() | ||
|
|
||
| var errProtocol ErrProtocol | ||
| if errors.As(err, &errProtocol) { | ||
| err = errProtocol.error | ||
| } | ||
|
|
||
| // I really wish this had the actual info in the error frame... | ||
| matches := betaProtocolRe.FindAllStringSubmatch(errStr, -1) | ||
| if len(matches) == 1 { | ||
| var protoErr *protocolError | ||
| if errors.As(err, &protoErr) { | ||
| version := protoErr.frame.Header().version.version() | ||
| if version > 0 { | ||
| return int(version - 1) | ||
| } | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| matches = protocolSupportRe.FindAllStringSubmatch(errStr, -1) | ||
| if len(matches) != 1 || len(matches[0]) != 2 { | ||
| var protoErr *protocolError | ||
| if errors.As(err, &protoErr) { | ||
| return int(protoErr.frame.Header().version.version()) | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| max, err := strconv.Atoi(matches[0][1]) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| return max | ||
| } | ||
|
|
||
| const highestProtocolVersionSupported = 5 | ||
| const highestProtocolVersionSupported = protoVersion5 | ||
| const lowestProtocolVersionSupported = protoVersion3 | ||
|
|
||
| func (c *controlConn) discoverProtocol(hosts []*HostInfo) (int, error) { | ||
| hosts = shuffleHosts(hosts) | ||
|
|
||
| connCfg := *c.session.connCfg | ||
| connCfg.ProtoVersion = highestProtocolVersionSupported | ||
|
|
||
| handler := connErrorHandlerFn(func(c *Conn, err error, closed bool) { | ||
| // we should never get here, but if we do it means we connected to a | ||
| // host successfully which means our attempted protocol version worked | ||
|
|
@@ -262,26 +217,27 @@ func (c *controlConn) discoverProtocol(hosts []*HostInfo) (int, error) { | |
|
|
||
| var err error | ||
| for _, host := range hosts { | ||
| var conn *Conn | ||
| conn, err = c.session.dial(c.session.ctx, host, &connCfg, handler) | ||
| if conn != nil { | ||
| conn.Close() | ||
| } | ||
| connCfg := *c.session.connCfg | ||
| for proto := highestProtocolVersionSupported; proto >= lowestProtocolVersionSupported; proto-- { | ||
| connCfg.ProtoVersion = proto | ||
|
|
||
| var conn *Conn | ||
| conn, err = c.session.dial(c.session.ctx, host, &connCfg, handler) | ||
| if conn != nil { | ||
| conn.Close() | ||
| } | ||
|
|
||
| if err == nil { | ||
| c.session.logger.Debug("Discovered protocol version using host.", | ||
| NewLogFieldInt("protocol_version", connCfg.ProtoVersion), NewLogFieldIP("host_addr", host.ConnectAddress()), NewLogFieldString("host_id", host.HostID())) | ||
| return connCfg.ProtoVersion, nil | ||
| } | ||
| if err == nil { | ||
| c.session.logger.Debug("Discovered protocol version using host.", | ||
| NewLogFieldInt("protocol_version", connCfg.ProtoVersion), NewLogFieldIP("host_addr", host.ConnectAddress())) | ||
| return connCfg.ProtoVersion, nil | ||
| } | ||
|
|
||
| if proto := parseProtocolFromError(err); proto > 0 { | ||
| c.session.logger.Debug("Discovered protocol version using host after parsing protocol error.", | ||
| NewLogFieldInt("protocol_version", proto), NewLogFieldIP("host_addr", host.ConnectAddress()), NewLogFieldString("host_id", host.HostID())) | ||
| return proto, nil | ||
| c.session.logger.Debug("Failed to connect to the host using protocol version.", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should only attempt to reconnect to the same host with a lower protocol version if the error is assumed to be related to unsupported protocol version:
I propose we use a custom internal error type (unsupportedProtocolVersionErr for example) just for this case that we can return inside the dial method and we test for it here in discoverProtocol(). Reference code in the java driver here. Technically the java driver does check for the error string which I didn't know but I still believe we shouldn't do that, checking the above 2 conditions is enough.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, we don't really have to re-try each time if the error is not protocol-related |
||
| NewLogFieldIP("host_addr", host.ConnectAddress()), | ||
| NewLogFieldInt("protocol_version", connCfg.ProtoVersion), | ||
| NewLogFieldError("err", err)) | ||
| } | ||
|
|
||
| c.session.logger.Debug("Failed to discover protocol version using host.", | ||
| NewLogFieldIP("host_addr", host.ConnectAddress()), NewLogFieldString("host_id", host.HostID()), NewLogFieldError("err", err)) | ||
| } | ||
|
|
||
| return 0, err | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any particular reason you removed the host_id log field from both calls? Is it because it's not populated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is not populated, so there is really no useful information