Skip to content

Commit

Permalink
Better classification of ProtocolError
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Jun 18, 2024
1 parent f60bd28 commit a244c8b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion proxy/pkg/zdmproxy/controlconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zdmproxy

import (
"context"
"errors"
"fmt"
"github.com/datastax/go-cassandra-native-protocol/frame"
"github.com/datastax/go-cassandra-native-protocol/message"
Expand Down Expand Up @@ -378,7 +379,8 @@ func (cc *ControlConn) connAndNegotiateProtoVer(endpoint Endpoint, initialProtoV
}
newConn := NewCqlConnection(tcpConn, cc.username, cc.password, ccReadTimeout, ccWriteTimeout, cc.conf)
err = newConn.InitializeContext(protoVer, ctx)
if err != nil && strings.Contains(err.Error(), "Invalid or unsupported protocol version") {
var respErr *ResponseError
if err != nil && errors.As(err, &respErr) && respErr.IsProtocolError() && strings.Contains(err.Error(), "Invalid or unsupported protocol version") {
// unsupported protocol version
// protocol renegotiation requires opening a new TCP connection
err2 := newConn.Close()
Expand Down
2 changes: 2 additions & 0 deletions proxy/pkg/zdmproxy/cqlconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ func (c *cqlConn) PerformHandshake(version primitive.ProtocolVersion, ctx contex
}
}
}
case *message.ProtocolError:
err = &ResponseError{Response: response}
default:
err = fmt.Errorf("expected AUTHENTICATE or READY, got %v", response.Body.Message)
}
Expand Down
23 changes: 22 additions & 1 deletion proxy/pkg/zdmproxy/response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package zdmproxy

import "github.com/datastax/go-cassandra-native-protocol/frame"
import (
"fmt"
"github.com/datastax/go-cassandra-native-protocol/frame"
"github.com/datastax/go-cassandra-native-protocol/message"
)

type Response struct {
responseFrame *frame.RawFrame
Expand Down Expand Up @@ -37,3 +41,20 @@ func (r *Response) GetStreamId() int16 {
return r.requestFrame.Header.StreamId
}
}

type ResponseError struct {
Response *frame.Frame
}

func (pre *ResponseError) Error() string {
return fmt.Sprintf("%v", pre.Response.Body.Message)
}

func (pre *ResponseError) IsProtocolError() bool {
switch pre.Response.Body.Message.(type) {
case *message.ProtocolError:
return true
default:
return false
}
}

0 comments on commit a244c8b

Please sign in to comment.