Pass a logger to ConnPool and yamux, instead of an io.Writer

Allowing us to remove the LogOutput field from config.
This commit is contained in:
Daniel Nephin 2020-07-29 14:43:33 -04:00
parent 7c4566f116
commit 73493ca01b
5 changed files with 14 additions and 15 deletions

View File

@ -591,16 +591,10 @@ func (a *Agent) initializeConnectionPool() error {
rpcSrcAddr = &net.TCPAddr{IP: a.config.RPCBindAddr.IP}
}
// Ensure we have a log output for the connection pool.
logOutput := a.LogOutput
if logOutput == nil {
logOutput = os.Stderr
}
pool := &pool.ConnPool{
Server: a.config.ServerMode,
SrcAddr: rpcSrcAddr,
LogOutput: logOutput,
Logger: a.logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
TLSConfigurator: a.tlsConfigurator,
Datacenter: a.config.Datacenter,
}

View File

@ -140,7 +140,7 @@ func NewClientWithOptions(config *Config, options ...ConsulOption) (*Client, err
connPool = &pool.ConnPool{
Server: false,
SrcAddr: config.RPCSrcAddr,
LogOutput: config.LogOutput,
Logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
MaxTime: clientRPCConnMaxIdle,
MaxStreams: clientMaxStreams,
TLSConfigurator: tlsConfigurator,

View File

@ -295,7 +295,10 @@ func (s *Server) handleNativeTLS(conn net.Conn) {
func (s *Server) handleMultiplexV2(conn net.Conn) {
defer conn.Close()
conf := yamux.DefaultConfig()
conf.LogOutput = s.config.LogOutput
// override the default because LogOutput conflicts with Logger
conf.LogOutput = nil
// TODO: should this be created once and cached?
conf.Logger = s.logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true})
server, _ := yamux.Server(conn, conf)
for {
sub, err := server.Accept()

View File

@ -400,7 +400,7 @@ func NewServerWithOptions(config *Config, options ...ConsulOption) (*Server, err
connPool = &pool.ConnPool{
Server: true,
SrcAddr: config.RPCSrcAddr,
LogOutput: config.LogOutput,
Logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
MaxTime: serverRPCCache,
MaxStreams: serverMaxStreams,
TLSConfigurator: tlsConfigurator,

View File

@ -4,7 +4,7 @@ import (
"container/list"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/rpc"
"sync"
@ -126,8 +126,9 @@ type ConnPool struct {
// SrcAddr is the source address for outgoing connections.
SrcAddr *net.TCPAddr
// LogOutput is used to control logging
LogOutput io.Writer
// Logger passed to yamux
// TODO: consider refactoring to accept a full yamux.Config instead of a logger
Logger *log.Logger
// The maximum time to keep a connection open
MaxTime time.Duration
@ -454,9 +455,10 @@ func (p *ConnPool) getNewConn(dc string, nodeName string, addr net.Addr) (*Conn,
return nil, err
}
// Setup the logger
conf := yamux.DefaultConfig()
conf.LogOutput = p.LogOutput
// override the default because LogOutput conflicts with Logger.
conf.LogOutput = nil
conf.Logger = p.Logger
// Create a multiplexed session
session, err := yamux.Client(conn, conf)