Skip to content

Commit

Permalink
proxy: add OnConnClose for handler (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Dec 16, 2022
1 parent 5490197 commit 1a5316d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import (
"go.uber.org/zap"
)

var (
ErrCloseConnMgr = errors.New("failed to close connection manager")
)

const (
sqlQueryState = "SHOW SESSION_STATES"
sqlSetState = "SET SESSION_STATES '%s'"
Expand Down Expand Up @@ -387,16 +391,18 @@ func (mgr *BackendConnManager) Close() error {
}
mgr.wg.Wait()

var err error
var connErr error
var addr string
mgr.processLock.Lock()
if mgr.backendConn != nil {
addr = mgr.backendConn.address
err = mgr.backendConn.Close()
connErr = mgr.backendConn.Close()
mgr.backendConn = nil
}
mgr.processLock.Unlock()

handErr := mgr.handshakeHandler.OnConnClose(mgr.authenticator)

eventReceiver := mgr.getEventReceiver()
if eventReceiver != nil {
// Notify the receiver if there's any event.
Expand All @@ -410,5 +416,5 @@ func (mgr *BackendConnManager) Close() error {
}
}
}
return err
return errors.Collect(ErrCloseConnMgr, connErr, handErr)
}
15 changes: 10 additions & 5 deletions pkg/proxy/backend/handshake_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type ConnContext interface {

type HandshakeHandler interface {
HandleHandshakeResp(ctx ConnContext, resp *pnet.HandshakeResp) error
GetCapability() pnet.Capability
GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)
OnConnClose(ctx ConnContext) error
GetCapability() pnet.Capability
}

type DefaultHandshakeHandler struct {
Expand All @@ -59,10 +60,6 @@ func (handler *DefaultHandshakeHandler) HandleHandshakeResp(ConnContext, *pnet.H
return nil
}

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

func (handler *DefaultHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error) {
ns, ok := handler.nsManager.GetNamespaceByUser(resp.User)
if !ok {
Expand All @@ -73,3 +70,11 @@ func (handler *DefaultHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.Ha
}
return ns.GetRouter(), nil
}

func (handler *DefaultHandshakeHandler) OnConnClose(ConnContext) error {
return nil
}

func (handler *DefaultHandshakeHandler) GetCapability() pnet.Capability {
return SupportedServerCapabilities
}
4 changes: 4 additions & 0 deletions pkg/proxy/backend/mock_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (handler *CustomHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.Han
return nil, nil
}

func (handler *CustomHandshakeHandler) OnConnClose(ctx ConnContext) error {
return nil
}

func (handler *CustomHandshakeHandler) HandleHandshakeResp(ctx ConnContext, resp *pnet.HandshakeResp) error {
handler.inUsername = resp.User
resp.User = handler.outUsername
Expand Down

0 comments on commit 1a5316d

Please sign in to comment.