run golangci-lint and apply suggested fixes
This commit is contained in:
@@ -13,6 +13,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint[:deadcode,unused]
|
||||
func debug(format string, args ...interface{}) {
|
||||
if debugEnabled {
|
||||
fmt.Fprintf(os.Stderr, "rpc/dataconn: %s\n", fmt.Sprintf(format, args...))
|
||||
|
||||
@@ -138,7 +138,6 @@ func (s *Server) serveConn(nc *transport.AuthConn) {
|
||||
default:
|
||||
s.log.WithField("endpoint", endpoint).Error("unknown endpoint")
|
||||
handlerErr = fmt.Errorf("requested endpoint does not exist")
|
||||
return
|
||||
}
|
||||
|
||||
s.log.WithField("endpoint", endpoint).WithField("errType", fmt.Sprintf("%T", handlerErr)).Debug("handler returned")
|
||||
@@ -188,6 +187,4 @@ func (s *Server) serveConn(nc *transport.AuthConn) {
|
||||
s.log.WithError(err).Error("cannot write send stream")
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package frameconn
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -48,7 +47,6 @@ func (f *FrameHeader) Unmarshal(buf []byte) {
|
||||
type Conn struct {
|
||||
readMtx, writeMtx sync.Mutex
|
||||
nc timeoutconn.Conn
|
||||
ncBuf *bufio.ReadWriter
|
||||
readNextValid bool
|
||||
readNext FrameHeader
|
||||
nextReadErr error
|
||||
|
||||
@@ -10,17 +10,11 @@ type shutdownFSM struct {
|
||||
type shutdownFSMState uint32
|
||||
|
||||
const (
|
||||
// zero value is important
|
||||
shutdownStateOpen shutdownFSMState = iota
|
||||
shutdownStateBegin
|
||||
)
|
||||
|
||||
func newShutdownFSM() *shutdownFSM {
|
||||
fsm := &shutdownFSM{
|
||||
state: shutdownStateOpen,
|
||||
}
|
||||
return fsm
|
||||
}
|
||||
|
||||
func (f *shutdownFSM) Begin() (thisCallStartedShutdown bool) {
|
||||
f.mtx.Lock()
|
||||
defer f.mtx.Unlock()
|
||||
|
||||
@@ -11,9 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
state state
|
||||
// if not nil, opErr is returned for ReadFrame and WriteFrame (not for Close, though)
|
||||
opErr atomic.Value // error
|
||||
state state
|
||||
fc *frameconn.Conn
|
||||
sendInterval, timeout time.Duration
|
||||
stopSend chan struct{}
|
||||
@@ -97,7 +95,10 @@ func (c *Conn) sendHeartbeats() {
|
||||
debug("send heartbeat")
|
||||
// if the connection is in zombie mode (aka iptables DROP inbetween peers)
|
||||
// this call or one of its successors will block after filling up the kernel tx buffer
|
||||
c.fc.WriteFrame([]byte{}, heartbeat)
|
||||
err := c.fc.WriteFrame([]byte{}, heartbeat)
|
||||
if err != nil {
|
||||
debug("send heartbeat error: %s", err)
|
||||
}
|
||||
// ignore errors from WriteFrame to rate-limit SendHeartbeat retries
|
||||
c.lastFrameSent.Store(time.Now())
|
||||
}()
|
||||
|
||||
@@ -13,6 +13,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint[:deadcode,unused]
|
||||
func debug(format string, args ...interface{}) {
|
||||
if debugEnabled {
|
||||
fmt.Fprintf(os.Stderr, "rpc/dataconn/heartbeatconn: %s\n", fmt.Sprintf(format, args...))
|
||||
|
||||
@@ -28,6 +28,7 @@ func WithLogger(ctx context.Context, log Logger) context.Context {
|
||||
return context.WithValue(ctx, contextKeyLogger, log)
|
||||
}
|
||||
|
||||
//nolint[:deadcode,unused]
|
||||
func getLog(ctx context.Context) Logger {
|
||||
log, ok := ctx.Value(contextKeyLogger).(Logger)
|
||||
if !ok {
|
||||
|
||||
@@ -23,9 +23,8 @@ type Conn struct {
|
||||
|
||||
// readMtx serializes read stream operations because we inherently only
|
||||
// support a single stream at a time over hc.
|
||||
readMtx sync.Mutex
|
||||
readClean bool
|
||||
allowWriteStreamTo bool
|
||||
readMtx sync.Mutex
|
||||
readClean bool
|
||||
|
||||
// writeMtx serializes write stream operations because we inherently only
|
||||
// support a single stream at a time over hc.
|
||||
@@ -95,7 +94,7 @@ func (c *Conn) ReadStreamedMessage(ctx context.Context, maxSize uint32, frameTyp
|
||||
}()
|
||||
err := readStream(c.frameReads, c.hc, w, frameType)
|
||||
c.readClean = isConnCleanAfterRead(err)
|
||||
w.CloseWithError(readMessageSentinel)
|
||||
_ = w.CloseWithError(readMessageSentinel) // always returns nil
|
||||
wg.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -166,7 +165,7 @@ func (c *Conn) SendStream(ctx context.Context, src zfs.StreamCopier, frameType u
|
||||
var res writeStreamRes
|
||||
res.errStream, res.errConn = writeStream(ctx, c.hc, r, frameType)
|
||||
if w != nil {
|
||||
w.CloseWithError(res.errStream)
|
||||
_ = w.CloseWithError(res.errStream) // always returns nil
|
||||
}
|
||||
writeStreamErrChan <- res
|
||||
}()
|
||||
|
||||
@@ -13,6 +13,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint[:deadcode,unused]
|
||||
func debug(format string, args ...interface{}) {
|
||||
if debugEnabled {
|
||||
fmt.Fprintf(os.Stderr, "rpc/dataconn/stream: %s\n", fmt.Sprintf(format, args...))
|
||||
|
||||
@@ -52,9 +52,6 @@ func (CloseWrite) sender(wire transport.Wire) {
|
||||
log.Printf("closeErr=%T %s", closeErr, closeErr)
|
||||
}()
|
||||
|
||||
type opResult struct {
|
||||
err error
|
||||
}
|
||||
writeDone := make(chan struct{}, 1)
|
||||
go func() {
|
||||
close(writeDone)
|
||||
|
||||
@@ -95,7 +95,7 @@ restart:
|
||||
return n, err
|
||||
}
|
||||
var nCurRead int
|
||||
nCurRead, err = c.Wire.Read(p[n:len(p)])
|
||||
nCurRead, err = c.Wire.Read(p[n:])
|
||||
n += nCurRead
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && nCurRead > 0 {
|
||||
err = nil
|
||||
@@ -111,7 +111,7 @@ restart:
|
||||
return n, err
|
||||
}
|
||||
var nCurWrite int
|
||||
nCurWrite, err = c.Wire.Write(p[n:len(p)])
|
||||
nCurWrite, err = c.Wire.Write(p[n:])
|
||||
n += nCurWrite
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && nCurWrite > 0 {
|
||||
err = nil
|
||||
|
||||
@@ -102,7 +102,7 @@ func TestNoPartialReadsDueToDeadline(t *testing.T) {
|
||||
// io.Copy will encounter a partial read, then wait ~50ms until the other 5 bytes are written
|
||||
// It is still going to fail with deadline err because it expects EOF
|
||||
n, err := io.Copy(&buf, bc)
|
||||
readDuration := time.Now().Sub(beginRead)
|
||||
readDuration := time.Since(beginRead)
|
||||
t.Logf("read duration=%s", readDuration)
|
||||
t.Logf("recv done n=%v err=%v", n, err)
|
||||
t.Logf("buf=%v", buf.Bytes())
|
||||
@@ -153,7 +153,7 @@ func TestPartialWriteMockConn(t *testing.T) {
|
||||
buf := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
begin := time.Now()
|
||||
n, err := mc.Write(buf[:])
|
||||
duration := time.Now().Sub(begin)
|
||||
duration := time.Since(begin)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, n)
|
||||
assert.True(t, duration > 100*time.Millisecond)
|
||||
|
||||
@@ -106,7 +106,7 @@ func NewInterceptors(logger Logger, clientIdentityKey interface{}) (unary grpc.U
|
||||
if !ok {
|
||||
panic("peer.FromContext expected to return a peer in grpc.UnaryServerInterceptor")
|
||||
}
|
||||
logger.WithField("peer_addr", fmt.Sprintf("%s", p.Addr)).Debug("peer addr")
|
||||
logger.WithField("peer_addr", p.Addr.String()).Debug("peer addr")
|
||||
a, ok := p.AuthInfo.(*authConnAuthType)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("NewInterceptors must be used in combination with grpc.NewTransportCredentials, but got auth type %T", p.AuthInfo))
|
||||
|
||||
@@ -88,6 +88,9 @@ func server() {
|
||||
log := logger.NewStderrDebugLogger()
|
||||
|
||||
srv, serve, err := grpchelper.NewServer(authListenerFactory, clientIdentityKey, log)
|
||||
if err != nil {
|
||||
onErr(err, "new server")
|
||||
}
|
||||
svc := &greeter{"hello "}
|
||||
pdu.RegisterGreeterServer(srv, svc)
|
||||
|
||||
|
||||
@@ -5,11 +5,10 @@ package pdu
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
math "math"
|
||||
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
|
||||
@@ -13,6 +13,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint[:deadcode,unused]
|
||||
func debug(format string, args ...interface{}) {
|
||||
if debugEnabled {
|
||||
fmt.Fprintf(os.Stderr, "rpc: %s\n", fmt.Sprintf(format, args...))
|
||||
|
||||
@@ -12,9 +12,6 @@ type contextKey int
|
||||
|
||||
const (
|
||||
contextKeyLoggers contextKey = iota
|
||||
contextKeyGeneralLogger
|
||||
contextKeyControlLogger
|
||||
contextKeyDataLogger
|
||||
)
|
||||
|
||||
/// All fields must be non-nil
|
||||
|
||||
@@ -34,8 +34,6 @@ type Server struct {
|
||||
dataServerServe serveFunc
|
||||
}
|
||||
|
||||
type serverContextKey int
|
||||
|
||||
type HandlerContextInterceptor func(ctx context.Context) context.Context
|
||||
|
||||
// config must be valid (use its Validate function).
|
||||
|
||||
@@ -7,6 +7,7 @@ package transportmux
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -142,7 +143,10 @@ func Demux(ctx context.Context, rawListener transport.AuthenticatedListener, lab
|
||||
continue
|
||||
}
|
||||
|
||||
rawConn.SetDeadline(time.Time{})
|
||||
err = rawConn.SetDeadline(time.Time{})
|
||||
if err != nil {
|
||||
getLog(ctx).WithError(err).Error("cannot reset deadline")
|
||||
}
|
||||
// blocking is intentional
|
||||
demuxListener.conns <- acceptRes{conn: rawConn, err: nil}
|
||||
}
|
||||
@@ -169,7 +173,12 @@ func (c labeledConnecter) Connect(ctx context.Context) (transport.Wire, error) {
|
||||
}
|
||||
|
||||
if dl, ok := ctx.Deadline(); ok {
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
defer func() {
|
||||
err := conn.SetDeadline(time.Time{})
|
||||
if err != nil {
|
||||
getLog(ctx).WithError(err).Error("cannot reset deadline")
|
||||
}
|
||||
}()
|
||||
if err := conn.SetDeadline(dl); err != nil {
|
||||
closeConn(err)
|
||||
return nil, err
|
||||
|
||||
@@ -157,7 +157,7 @@ func DoHandshakeCurrentVersion(conn net.Conn, deadline time.Time) *HandshakeErro
|
||||
|
||||
const HandshakeMessageMaxLen = 16 * 4096
|
||||
|
||||
func DoHandshakeVersion(conn net.Conn, deadline time.Time, version int) *HandshakeError {
|
||||
func DoHandshakeVersion(conn net.Conn, deadline time.Time, version int) (rErr *HandshakeError) {
|
||||
ours := HandshakeMessage{
|
||||
ProtocolVersion: version,
|
||||
Extensions: nil,
|
||||
@@ -167,8 +167,19 @@ func DoHandshakeVersion(conn net.Conn, deadline time.Time, version int) *Handsha
|
||||
return hsErr("could not encode protocol banner: %s", err)
|
||||
}
|
||||
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
conn.SetDeadline(deadline)
|
||||
err = conn.SetDeadline(deadline)
|
||||
if err != nil {
|
||||
return hsErr("could not set deadline for protocol banner handshake: %s", err)
|
||||
}
|
||||
defer func() {
|
||||
if rErr != nil {
|
||||
return
|
||||
}
|
||||
err := conn.SetDeadline(time.Time{})
|
||||
if err != nil {
|
||||
rErr = hsErr("could not reset deadline after protocol banner handshake: %s", err)
|
||||
}
|
||||
}()
|
||||
_, err = io.Copy(conn, bytes.NewBuffer(hsb))
|
||||
if err != nil {
|
||||
return hsErr("could not send protocol banner: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user