Skip to content
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

mysql/server: add PreHandleFunc hook for connections #7513

Merged
merged 1 commit into from
Feb 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion go/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package mysql

import (
"context"
"crypto/tls"
"io"
"net"
Expand Down Expand Up @@ -173,6 +174,13 @@ type Listener struct {

// RequireSecureTransport configures the server to reject connections from insecure clients
RequireSecureTransport bool

// PreHandleFunc is called for each incoming connection, immediately after
// accepting a new connection. By default it's no-op. Useful for custom
// connection inspection or TLS termination. The returned connection is
// handled further by the MySQL handler. An non-nil error will stop
// processing the connection by the MySQL handler.
PreHandleFunc func(context.Context, net.Conn, uint32) (net.Conn, error)
}

// NewFromListener creares a new mysql listener from an existing net.Listener
Expand Down Expand Up @@ -248,6 +256,8 @@ func (l *Listener) Addr() net.Addr {

// Accept runs an accept loop until the listener is closed.
func (l *Listener) Accept() {
ctx := context.Background()

for {
conn, err := l.listener.Accept()
if err != nil {
Expand All @@ -264,7 +274,17 @@ func (l *Listener) Accept() {
connCount.Add(1)
connAccept.Add(1)

go l.handle(conn, connectionID, acceptTime)
go func() {
if l.PreHandleFunc != nil {
conn, err = l.PreHandleFunc(ctx, conn, connectionID)
if err != nil {
log.Errorf("mysql_server pre hook: %s", err)
return
}
}

l.handle(conn, connectionID, acceptTime)
}()
}
}

Expand Down