Skip to content

Commit 2d4ebe5

Browse files
committed
server: Get rid of globals for setting a protocol listener factory. Get rid of unused, global-ridden and complicated Interceptor and Option functionality.
1 parent d06023d commit 2d4ebe5

File tree

3 files changed

+21
-210
lines changed

3 files changed

+21
-210
lines changed

server/extension.go

Lines changed: 0 additions & 183 deletions
This file was deleted.

server/server.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ type ProtocolListener interface {
3737
}
3838

3939
// ProtocolListenerFunc returns a ProtocolListener based on the configuration it was given.
40-
type ProtocolListenerFunc func(cfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error)
41-
42-
// DefaultProtocolListenerFunc is the protocol listener, which defaults to Vitess' protocol listener. Changing
43-
// this function will change the protocol listener used when creating all servers. If multiple servers are needed
44-
// with different protocols, then create each server after changing this function. Servers retain the protocol that
45-
// they were created with.
46-
var DefaultProtocolListenerFunc ProtocolListenerFunc = func(cfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error) {
47-
return mysql.NewListenerWithConfig(cfg)
40+
type ProtocolListenerFunc func(cfg Config, listenerCfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error)
41+
42+
func MySQLProtocolListenerFactory(cfg Config, listenerCfg mysql.ListenerConfig, sel ServerEventListener) (ProtocolListener, error) {
43+
vtListener, err := mysql.NewListenerWithConfig(listenerCfg)
44+
if err != nil {
45+
return nil, err
46+
}
47+
if cfg.Version != "" {
48+
vtListener.ServerVersion = cfg.Version
49+
}
50+
vtListener.TLSConfig = cfg.TLSConfig
51+
vtListener.RequireSecureTransport = cfg.RequireSecureTransport
52+
return vtListener, nil
4853
}
4954

5055
type ServerEventListener interface {
@@ -114,10 +119,6 @@ func portInUse(hostPort string) bool {
114119
}
115120

116121
func newServerFromHandler(cfg Config, e *sqle.Engine, sm *SessionManager, handler mysql.Handler, sel ServerEventListener) (*Server, error) {
117-
for _, option := range cfg.Options {
118-
option(e, sm, handler)
119-
}
120-
121122
if cfg.ConnReadTimeout < 0 {
122123
cfg.ConnReadTimeout = 0
123124
}
@@ -156,19 +157,15 @@ func newServerFromHandler(cfg Config, e *sqle.Engine, sm *SessionManager, handle
156157
ConnReadBufferSize: mysql.DefaultConnBufferSize,
157158
AllowClearTextWithoutTLS: cfg.AllowClearTextWithoutTLS,
158159
}
159-
protocolListener, err := DefaultProtocolListenerFunc(listenerCfg, sel)
160+
plf := cfg.ProtocolListenerFactory
161+
if plf == nil {
162+
plf = MySQLProtocolListenerFactory
163+
}
164+
protocolListener, err := plf(cfg, listenerCfg, sel)
160165
if err != nil {
161166
return nil, err
162167
}
163168

164-
if vtListener, ok := protocolListener.(*mysql.Listener); ok {
165-
if cfg.Version != "" {
166-
vtListener.ServerVersion = cfg.Version
167-
}
168-
vtListener.TLSConfig = cfg.TLSConfig
169-
vtListener.RequireSecureTransport = cfg.RequireSecureTransport
170-
}
171-
172169
return &Server{
173170
Listener: protocolListener,
174171
handler: handler,

server/server_config.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ import (
2323
"go.opentelemetry.io/otel/trace"
2424

2525
gms "github.com/dolthub/go-mysql-server"
26-
sqle "github.com/dolthub/go-mysql-server"
2726
"github.com/dolthub/go-mysql-server/sql"
2827
)
2928

30-
// Option is an option to customize server.
31-
type Option func(e *sqle.Engine, sm *SessionManager, handler mysql.Handler)
32-
3329
// Server is a MySQL server for SQLe engines.
3430
type Server struct {
3531
Listener ProtocolListener
@@ -82,8 +78,9 @@ type Config struct {
8278
// If true, queries will be logged as base64 encoded strings.
8379
// If false (default behavior), queries will be logged as strings, but newlines and tabs will be replaced with spaces.
8480
EncodeLoggedQuery bool
85-
// Options add additional options to customize the server.
86-
Options []Option
81+
// Used to get the ProtocolListener on server start.
82+
// If unset, defaults to MySQLProtocolListenerFactory.
83+
ProtocolListenerFactory ProtocolListenerFunc
8784
}
8885

8986
func (c Config) NewConfig() (Config, error) {

0 commit comments

Comments
 (0)