github.com/dolthub/go-mysql-server@v0.18.0/server/server_config.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package server
    16  
    17  import (
    18  	"crypto/tls"
    19  	"net"
    20  	"time"
    21  
    22  	"github.com/dolthub/vitess/go/mysql"
    23  	"go.opentelemetry.io/otel/trace"
    24  
    25  	gms "github.com/dolthub/go-mysql-server"
    26  	sqle "github.com/dolthub/go-mysql-server"
    27  	"github.com/dolthub/go-mysql-server/sql"
    28  )
    29  
    30  // Option is an option to customize server.
    31  type Option func(e *sqle.Engine, sm *SessionManager, handler mysql.Handler)
    32  
    33  // Server is a MySQL server for SQLe engines.
    34  type Server struct {
    35  	Listener   ProtocolListener
    36  	handler    mysql.Handler
    37  	sessionMgr *SessionManager
    38  	Engine     *gms.Engine
    39  }
    40  
    41  // Config for the mysql server.
    42  type Config struct {
    43  	// Protocol for the connection.
    44  	Protocol string
    45  	// Address of the server.
    46  	Address string
    47  	// Custom listener for the mysql server. Use this if you don't want ports or unix sockets to be opened automatically.
    48  	// This can be useful in testing by using a pure go net.Conn implementation.
    49  	Listener net.Listener
    50  	// Tracer to use in the server. By default, a noop tracer will be used if
    51  	// no tracer is provided.
    52  	Tracer trace.Tracer
    53  	// Version string to advertise in running server
    54  	Version string
    55  	// ConnReadTimeout is the server's read timeout
    56  	ConnReadTimeout time.Duration
    57  	// ConnWriteTimeout is the server's write timeout
    58  	ConnWriteTimeout time.Duration
    59  	// MaxConnections is the maximum number of simultaneous connections that the server will allow.
    60  	MaxConnections uint64
    61  	// TLSConfig is the configuration for TLS on this server. If |nil|, TLS is not supported.
    62  	TLSConfig *tls.Config
    63  	// RequestSecureTransport will require incoming connections to be TLS. Requires non-|nil| TLSConfig.
    64  	RequireSecureTransport bool
    65  	// DisableClientMultiStatements will prevent processing of incoming
    66  	// queries as if they contain more than one query. This processing
    67  	// currently works in some simple cases, but breaks in the presence of
    68  	// statements (such as in CREATE TRIGGER queries). Configuring the
    69  	// server to disable processing these is one option for users to get
    70  	// support back for single queries that contain statements, at the cost
    71  	// of not supporting the CLIENT_MULTI_STATEMENTS option on the server.
    72  	DisableClientMultiStatements bool
    73  	// NoDefaults prevents using persisted configuration for new server sessions
    74  	NoDefaults bool
    75  	// Socket is a path to unix socket file
    76  	Socket                   string
    77  	AllowClearTextWithoutTLS bool
    78  	// MaxLoggedQueryLen sets the length at which queries written to the logs are truncated.  A value of 0 will
    79  	// result in no truncation. A value less than 0 will result in the queries being omitted from the logs completely
    80  	MaxLoggedQueryLen int
    81  	// EncodeLoggedQuery determines if logged queries are base64 encoded.
    82  	// If true, queries will be logged as base64 encoded strings.
    83  	// If false (default behavior), queries will be logged as strings, but newlines and tabs will be replaced with spaces.
    84  	EncodeLoggedQuery bool
    85  	// Options add additional options to customize the server.
    86  	Options []Option
    87  }
    88  
    89  func (c Config) NewConfig() (Config, error) {
    90  	if _, val, ok := sql.SystemVariables.GetGlobal("max_connections"); ok {
    91  		mc, ok := val.(int64)
    92  		if !ok {
    93  			return Config{}, sql.ErrUnknownSystemVariable.New("max_connections")
    94  		}
    95  		c.MaxConnections = uint64(mc)
    96  	}
    97  	if _, val, ok := sql.SystemVariables.GetGlobal("net_write_timeout"); ok {
    98  		timeout, ok := val.(int64)
    99  		if !ok {
   100  			return Config{}, sql.ErrUnknownSystemVariable.New("net_write_timeout")
   101  		}
   102  		c.ConnWriteTimeout = time.Duration(timeout) * time.Millisecond
   103  	}
   104  	if _, val, ok := sql.SystemVariables.GetGlobal("net_read_timeout"); ok {
   105  		timeout, ok := val.(int64)
   106  		if !ok {
   107  			return Config{}, sql.ErrUnknownSystemVariable.New("net_read_timeout")
   108  		}
   109  		c.ConnReadTimeout = time.Duration(timeout) * time.Millisecond
   110  	}
   111  	return c, nil
   112  }