snapshot: add TLS support to HalfCloser interface (#6216)

Calls net.TCPConn.CloseWrite or mtls.Conn.CloseWrite, which was added in https://go-review.googlesource.com/c/go/+/31318/
This commit is contained in:
Mike Morris 2019-08-12 12:47:02 -04:00 committed by GitHub
parent 8241787e92
commit 61206fdf42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package pool
import ( import (
"container/list" "container/list"
"crypto/tls"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -257,11 +258,8 @@ func (p *ConnPool) acquire(dc string, addr net.Addr, version int, useTLS bool) (
return nil, fmt.Errorf("rpc error: lead thread didn't get connection") return nil, fmt.Errorf("rpc error: lead thread didn't get connection")
} }
// HalfCloser is an interface that exposes a TCP half-close. We need this // HalfCloser is an interface that exposes a TCP half-close without exposing
// because we want to expose the raw TCP connection underlying a TLS one in a // the underlying TLS or raw TCP connection.
// way that's hard to screw up and use for anything else. There's a change
// brewing that will allow us to use the TLS connection for this instead -
// https://go-review.googlesource.com/#/c/25159/.
type HalfCloser interface { type HalfCloser interface {
CloseWrite() error CloseWrite() error
} }
@ -296,11 +294,13 @@ func DialTimeoutWithRPCType(dc string, addr net.Addr, src *net.TCPAddr, timeout
return nil, nil, err return nil, nil, err
} }
// Cast to TCPConn
var hc HalfCloser var hc HalfCloser
if tcp, ok := conn.(*net.TCPConn); ok { if tcp, ok := conn.(*net.TCPConn); ok {
tcp.SetKeepAlive(true) tcp.SetKeepAlive(true)
tcp.SetNoDelay(true) tcp.SetNoDelay(true)
// Expose TCPConn CloseWrite method on HalfCloser
hc = tcp hc = tcp
} }
@ -319,6 +319,11 @@ func DialTimeoutWithRPCType(dc string, addr net.Addr, src *net.TCPAddr, timeout
return nil, nil, err return nil, nil, err
} }
conn = tlsConn conn = tlsConn
// If this is a tls.Conn, expose HalfCloser to caller
if tlsConn, ok := conn.(*tls.Conn); ok {
hc = tlsConn
}
} }
return conn, hc, nil return conn, hc, nil