Skip to content

Commit

Permalink
backend: add context to pass parameters need by handler (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing authored Dec 6, 2022
1 parent ed9650d commit 3cc9581
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
6 changes: 4 additions & 2 deletions pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package backend

import (
"context"
"crypto/tls"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -145,15 +146,16 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, clientIO *pnet
auth.capability = commonCaps.Uint32()

resp := pnet.ParseHandshakeResponse(pkt)
if err = handshakeHandler.HandleHandshakeResp(resp, clientIO.SourceAddr().String()); err != nil {
ctx := context.WithValue(context.Background(), ContextKeyClientAddr, clientIO.SourceAddr().String())
if err = handshakeHandler.HandleHandshakeResp(ctx, resp); err != nil {
return err
}
auth.user = resp.User
auth.dbname = resp.DB
auth.collation = resp.Collation
auth.attrs = resp.Attrs

backendIO, err := getBackend(auth, resp)
backendIO, err := getBackend(ctx, auth, resp)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type redirectResult struct {
to string
}

type backendIOGetter func(auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error)
type backendIOGetter func(ctx context.Context, auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error)

// BackendConnManager migrates a session from one BackendConnection to another.
//
Expand Down Expand Up @@ -105,8 +105,8 @@ func NewBackendConnManager(logger *zap.Logger, handshakeHandler HandshakeHandler
signalReceived: make(chan struct{}, 1),
redirectResCh: make(chan *redirectResult, 1),
}
mgr.getBackendIO = func(auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error) {
router, err := handshakeHandler.GetRouter(resp)
mgr.getBackendIO = func(ctx context.Context, auth *Authenticator, resp *pnet.HandshakeResp) (*pnet.PacketIO, error) {
router, err := handshakeHandler.GetRouter(ctx, resp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/backend/backend_conn_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func newBackendMgrTester(t *testing.T, cfg ...cfgOverrider) *backendMgrTester {
return tester
}

func (ts *backendMgrTester) getBackendIO(auth *Authenticator, _ *pnet.HandshakeResp) (*pnet.PacketIO, error) {
func (ts *backendMgrTester) getBackendIO(ctx context.Context, auth *Authenticator, _ *pnet.HandshakeResp) (*pnet.PacketIO, error) {
addr := ts.tc.backendListener.Addr().String()
ts.mp.backendConn = NewBackendConnection(addr)
if err := ts.mp.backendConn.Connect(); err != nil {
Expand Down
21 changes: 17 additions & 4 deletions pkg/proxy/backend/handshake_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@
package backend

import (
"context"

"github.com/pingcap/TiProxy/lib/util/errors"
"github.com/pingcap/TiProxy/pkg/manager/namespace"
"github.com/pingcap/TiProxy/pkg/manager/router"
pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
)

type contextKey string

func (k contextKey) String() string {
return "handler context key " + string(k)
}

// Context keys.
var (
ContextKeyClientAddr = contextKey("client_addr")
)

var _ HandshakeHandler = (*DefaultHandshakeHandler)(nil)

type HandshakeHandler interface {
HandleHandshakeResp(resp *pnet.HandshakeResp, sourceAddr string) error
HandleHandshakeResp(ctx context.Context, resp *pnet.HandshakeResp) error
GetCapability() pnet.Capability
GetRouter(resp *pnet.HandshakeResp) (router.Router, error)
GetRouter(ctx context.Context, resp *pnet.HandshakeResp) (router.Router, error)
}

type DefaultHandshakeHandler struct {
Expand All @@ -39,15 +52,15 @@ func NewDefaultHandshakeHandler(nsManager *namespace.NamespaceManager) *DefaultH
}
}

func (handler *DefaultHandshakeHandler) HandleHandshakeResp(*pnet.HandshakeResp, string) error {
func (handler *DefaultHandshakeHandler) HandleHandshakeResp(context.Context, *pnet.HandshakeResp) error {
return nil
}

func (handler *DefaultHandshakeHandler) GetCapability() pnet.Capability {
return SupportedServerCapabilities
}

func (handler *DefaultHandshakeHandler) GetRouter(resp *pnet.HandshakeResp) (router.Router, error) {
func (handler *DefaultHandshakeHandler) GetRouter(ctx context.Context, resp *pnet.HandshakeResp) (router.Router, error) {
ns, ok := handler.nsManager.GetNamespaceByUser(resp.User)
if !ok {
ns, ok = handler.nsManager.GetNamespace("default")
Expand Down
9 changes: 5 additions & 4 deletions pkg/proxy/backend/mock_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package backend

import (
"context"
"crypto/tls"
"testing"

Expand Down Expand Up @@ -64,7 +65,7 @@ func newMockProxy(t *testing.T, cfg *proxyConfig) *mockProxy {
}

func (mp *mockProxy) authenticateFirstTime(clientIO, backendIO *pnet.PacketIO) error {
if err := mp.authenticator.handshakeFirstTime(mp.logger, clientIO, mp.handshakeHandler, func(*Authenticator, *pnet.HandshakeResp) (*pnet.PacketIO, error) {
if err := mp.authenticator.handshakeFirstTime(mp.logger, clientIO, mp.handshakeHandler, func(context.Context, *Authenticator, *pnet.HandshakeResp) (*pnet.PacketIO, error) {
return backendIO, nil
}, mp.frontendTLSConfig, mp.backendTLSConfig); err != nil {
return err
Expand Down Expand Up @@ -107,14 +108,14 @@ type CustomHandshakeHandler struct {
outAttrs map[string]string
}

func (handler *CustomHandshakeHandler) GetRouter(resp *pnet.HandshakeResp) (router.Router, error) {
func (handler *CustomHandshakeHandler) GetRouter(ctx context.Context, resp *pnet.HandshakeResp) (router.Router, error) {
return nil, nil
}

func (handler *CustomHandshakeHandler) HandleHandshakeResp(resp *pnet.HandshakeResp, addr string) error {
func (handler *CustomHandshakeHandler) HandleHandshakeResp(ctx context.Context, resp *pnet.HandshakeResp) error {
handler.inUsername = resp.User
resp.User = handler.outUsername
handler.inAddr = addr
handler.inAddr = ctx.Value(ContextKeyClientAddr).(string)
resp.Attrs = handler.outAttrs
return nil
}
Expand Down

0 comments on commit 3cc9581

Please sign in to comment.