Skip to content

Commit

Permalink
server: Get rid of globals for setting a protocol listener factory. G…
Browse files Browse the repository at this point in the history
…et rid of unused, global-ridden and complicated Interceptor and Option functionality.
  • Loading branch information
reltuk committed Mar 5, 2025
1 parent 690400e commit 46d0287
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 210 deletions.
183 changes: 0 additions & 183 deletions server/extension.go

This file was deleted.

39 changes: 18 additions & 21 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ type ProtocolListener interface {
}

// ProtocolListenerFunc returns a ProtocolListener based on the configuration it was given.
type ProtocolListenerFunc func(cfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error)

// DefaultProtocolListenerFunc is the protocol listener, which defaults to Vitess' protocol listener. Changing
// this function will change the protocol listener used when creating all servers. If multiple servers are needed
// with different protocols, then create each server after changing this function. Servers retain the protocol that
// they were created with.
var DefaultProtocolListenerFunc ProtocolListenerFunc = func(cfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error) {
return mysql.NewListenerWithConfig(cfg)
type ProtocolListenerFunc func(cfg Config, listenerCfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error)

func MySQLProtocolListenerFactory(cfg Config, listenerCfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error) {
vtListener, err := mysql.NewListenerWithConfig(listenerCfg)
if err != nil {
return nil, err
}
if cfg.Version != "" {
vtListener.ServerVersion = cfg.Version
}
vtListener.TLSConfig = cfg.TLSConfig
vtListener.RequireSecureTransport = cfg.RequireSecureTransport
return vtListener, nil
}

type ServerEventListener interface {
Expand Down Expand Up @@ -114,10 +119,6 @@ func portInUse(hostPort string) bool {
}

func newServerFromHandler(cfg Config, e *sqle.Engine, sm *SessionManager, handler mysql.Handler, sel ServerEventListener) (*Server, error) {
for _, option := range cfg.Options {
option(e, sm, handler)
}

if cfg.ConnReadTimeout < 0 {
cfg.ConnReadTimeout = 0
}
Expand Down Expand Up @@ -156,19 +157,15 @@ func newServerFromHandler(cfg Config, e *sqle.Engine, sm *SessionManager, handle
ConnReadBufferSize: mysql.DefaultConnBufferSize,
AllowClearTextWithoutTLS: cfg.AllowClearTextWithoutTLS,
}
protocolListener, err := DefaultProtocolListenerFunc(listenerCfg, sel)
plf := cfg.ProtocolListenerFactory
if plf == nil {
plf = MySQLProtocolListenerFactory
}
protocolListener, err := plf(cfg, listenerCfg, sel)
if err != nil {
return nil, err
}

if vtListener, ok := protocolListener.(*mysql.Listener); ok {
if cfg.Version != "" {
vtListener.ServerVersion = cfg.Version
}
vtListener.TLSConfig = cfg.TLSConfig
vtListener.RequireSecureTransport = cfg.RequireSecureTransport
}

return &Server{
Listener: protocolListener,
handler: handler,
Expand Down
9 changes: 3 additions & 6 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ import (
"go.opentelemetry.io/otel/trace"

gms "github.com/dolthub/go-mysql-server"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/sql"
)

// Option is an option to customize server.
type Option func(e *sqle.Engine, sm *SessionManager, handler mysql.Handler)

// Server is a MySQL server for SQLe engines.
type Server struct {
Listener ProtocolListener
Expand Down Expand Up @@ -82,8 +78,9 @@ type Config struct {
// If true, queries will be logged as base64 encoded strings.
// If false (default behavior), queries will be logged as strings, but newlines and tabs will be replaced with spaces.
EncodeLoggedQuery bool
// Options add additional options to customize the server.
Options []Option
// Used to get the ProtocolListener on server start.
// If unset, defaults to MySQLProtocolListenerFactory.
ProtocolListenerFactory ProtocolListenerFunc
}

func (c Config) NewConfig() (Config, error) {
Expand Down

0 comments on commit 46d0287

Please sign in to comment.