This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathserver.go
87 lines (73 loc) · 1.97 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package server
import (
"time"
"github.com/opentracing/opentracing-go"
sqle "github.com/src-d/go-mysql-server"
"github.com/src-d/go-mysql-server/auth"
"vitess.io/vitess/go/mysql"
)
// Server is a MySQL server for SQLe engines.
type Server struct {
Listener *mysql.Listener
h *Handler
}
// Config for the mysql server.
type Config struct {
// Protocol for the connection.
Protocol string
// Address of the server.
Address string
// Auth of the server.
Auth auth.Auth
// Tracer to use in the server. By default, a noop tracer will be used if
// no tracer is provided.
Tracer opentracing.Tracer
ConnReadTimeout time.Duration
ConnWriteTimeout time.Duration
}
// NewDefaultServer creates a Server with the default session builder.
func NewDefaultServer(cfg Config, e *sqle.Engine) (*Server, error) {
return NewServer(cfg, e, DefaultSessionBuilder)
}
// NewServer creates a server with the given protocol, address, authentication
// details given a SQLe engine and a session builder.
func NewServer(cfg Config, e *sqle.Engine, sb SessionBuilder) (*Server, error) {
var tracer opentracing.Tracer
if cfg.Tracer != nil {
tracer = cfg.Tracer
} else {
tracer = opentracing.NoopTracer{}
}
if cfg.ConnReadTimeout < 0 {
cfg.ConnReadTimeout = 0
}
if cfg.ConnWriteTimeout < 0 {
cfg.ConnWriteTimeout = 0
}
handler := NewHandler(e,
NewSessionManager(
sb, tracer,
e.Catalog.MemoryManager,
cfg.Address),
cfg.ConnReadTimeout)
a := cfg.Auth.Mysql()
l, err := NewListener(cfg.Protocol, cfg.Address, handler)
if err != nil {
return nil, err
}
vtListnr, err := mysql.NewFromListener(l, a, handler, cfg.ConnReadTimeout, cfg.ConnWriteTimeout)
if err != nil {
return nil, err
}
return &Server{Listener: vtListnr, h: handler}, nil
}
// Start starts accepting connections on the server.
func (s *Server) Start() error {
s.Listener.Accept()
return nil
}
// Close closes the server connection.
func (s *Server) Close() error {
s.Listener.Close()
return nil
}