diff --git a/lib/backup/s3remote/s3.go b/lib/backup/s3remote/s3.go index 9db96c9c9..0f6ece5f9 100644 --- a/lib/backup/s3remote/s3.go +++ b/lib/backup/s3remote/s3.go @@ -3,6 +3,7 @@ package s3remote import ( "bytes" "context" + "errors" "fmt" "io" "strings" @@ -287,7 +288,8 @@ func (fs *FS) HasFile(filePath string) (bool, error) { } o, err := fs.s3.GetObject(input) if err != nil { - if ae, ok := err.(awserr.Error); ok && ae.Code() == s3.ErrCodeNoSuchKey { + var ae awserr.Error + if errors.As(err, &ae) && ae.Code() == s3.ErrCodeNoSuchKey { return false, nil } return false, fmt.Errorf("cannot open %q at %s (remote path %q): %w", filePath, fs, path, err) diff --git a/lib/ingestserver/graphite/server.go b/lib/ingestserver/graphite/server.go index 02be8da7b..ae2d9085d 100644 --- a/lib/ingestserver/graphite/server.go +++ b/lib/ingestserver/graphite/server.go @@ -1,6 +1,7 @@ package graphite import ( + "errors" "io" "net" "runtime" @@ -86,7 +87,8 @@ func serveTCP(ln net.Listener, insertHandler func(r io.Reader) error) { for { c, err := ln.Accept() if err != nil { - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("graphite: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) time.Sleep(time.Second) @@ -125,7 +127,8 @@ func serveUDP(ln net.PacketConn, insertHandler func(r io.Reader) error) { n, addr, err := ln.ReadFrom(bb.B) if err != nil { writeErrorsUDP.Inc() - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("graphite: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) time.Sleep(time.Second) diff --git a/lib/ingestserver/influx/server.go b/lib/ingestserver/influx/server.go index f429d9bfa..02bc5c60e 100644 --- a/lib/ingestserver/influx/server.go +++ b/lib/ingestserver/influx/server.go @@ -1,6 +1,7 @@ package influx import ( + "errors" "io" "net" "runtime" @@ -86,7 +87,8 @@ func serveTCP(ln net.Listener, insertHandler func(r io.Reader) error) { for { c, err := ln.Accept() if err != nil { - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("influx: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) time.Sleep(time.Second) @@ -125,7 +127,8 @@ func serveUDP(ln net.PacketConn, insertHandler func(r io.Reader) error) { n, addr, err := ln.ReadFrom(bb.B) if err != nil { writeErrorsUDP.Inc() - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("influx: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) time.Sleep(time.Second) diff --git a/lib/ingestserver/opentsdb/listener_switch.go b/lib/ingestserver/opentsdb/listener_switch.go index bd4689587..9697d836e 100644 --- a/lib/ingestserver/opentsdb/listener_switch.go +++ b/lib/ingestserver/opentsdb/listener_switch.go @@ -1,6 +1,7 @@ package opentsdb import ( + "errors" "io" "net" "sync" @@ -64,7 +65,8 @@ func (ls *listenerSwitch) worker() { for { c, err := ls.ln.Accept() if err != nil { - if ne, ok := err.(net.Error); ok && ne.Temporary() { + var ne net.Error + if errors.As(err, &ne) && ne.Temporary() { logger.Infof("listenerSwitch: temporary error at %q: %s; sleeping for a second...", ls.ln.Addr(), err) time.Sleep(time.Second) continue diff --git a/lib/ingestserver/opentsdb/server.go b/lib/ingestserver/opentsdb/server.go index af83bc923..c7aca100c 100644 --- a/lib/ingestserver/opentsdb/server.go +++ b/lib/ingestserver/opentsdb/server.go @@ -1,6 +1,7 @@ package opentsdb import ( + "errors" "io" "net" "net/http" @@ -106,7 +107,8 @@ func serveTelnet(ln net.Listener, insertHandler func(r io.Reader) error) { for { c, err := ln.Accept() if err != nil { - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("opentsdb: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) time.Sleep(time.Second) @@ -145,7 +147,8 @@ func serveUDP(ln net.PacketConn, insertHandler func(r io.Reader) error) { n, addr, err := ln.ReadFrom(bb.B) if err != nil { writeErrorsUDP.Inc() - if ne, ok := err.(net.Error); ok { + var ne net.Error + if errors.As(err, &ne) { if ne.Temporary() { logger.Errorf("opentsdb: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) time.Sleep(time.Second) diff --git a/lib/netutil/conn.go b/lib/netutil/conn.go index feb81452c..9bd8c1100 100644 --- a/lib/netutil/conn.go +++ b/lib/netutil/conn.go @@ -1,6 +1,7 @@ package netutil import ( + "errors" "fmt" "io" "net" @@ -57,7 +58,8 @@ func (sc *statConn) Read(p []byte) (int, error) { sc.cm.readCalls.Inc() sc.cm.readBytes.Add(n) if err != nil && err != io.EOF { - if ne, ok := err.(net.Error); ok && ne.Timeout() { + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { sc.cm.readTimeouts.Inc() } else { sc.cm.readErrors.Inc() @@ -71,7 +73,8 @@ func (sc *statConn) Write(p []byte) (int, error) { sc.cm.writeCalls.Inc() sc.cm.writtenBytes.Add(n) if err != nil { - if ne, ok := err.(net.Error); ok && ne.Timeout() { + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { sc.cm.writeTimeouts.Inc() } else { sc.cm.writeErrors.Inc() diff --git a/lib/netutil/tcplistener.go b/lib/netutil/tcplistener.go index 267802c77..8f5afa166 100644 --- a/lib/netutil/tcplistener.go +++ b/lib/netutil/tcplistener.go @@ -1,6 +1,7 @@ package netutil import ( + "errors" "flag" "fmt" "net" @@ -63,7 +64,8 @@ func (ln *TCPListener) Accept() (net.Conn, error) { conn, err := ln.Listener.Accept() ln.accepts.Inc() if err != nil { - if ne, ok := err.(net.Error); ok && ne.Temporary() { + var ne net.Error + if errors.As(err, &ne) && ne.Temporary() { logger.Errorf("temporary error when listening for TCP addr %q: %s", ln.Addr(), err) time.Sleep(time.Second) continue diff --git a/lib/protoparser/graphite/streamparser.go b/lib/protoparser/graphite/streamparser.go index 5b4d7fece..825ffdcb7 100644 --- a/lib/protoparser/graphite/streamparser.go +++ b/lib/protoparser/graphite/streamparser.go @@ -1,6 +1,7 @@ package graphite import ( + "errors" "flag" "fmt" "io" @@ -53,7 +54,8 @@ func (ctx *streamContext) Read(r io.Reader) bool { } ctx.reqBuf, ctx.tailBuf, ctx.err = common.ReadLinesBlock(r, ctx.reqBuf, ctx.tailBuf) if ctx.err != nil { - if ne, ok := ctx.err.(net.Error); ok && ne.Timeout() { + var ne net.Error + if errors.As(ctx.err, &ne) && ne.Timeout() { // Flush the read data on timeout and try reading again. ctx.err = nil } else { diff --git a/lib/protoparser/opentsdb/streamparser.go b/lib/protoparser/opentsdb/streamparser.go index b250f3c4a..dffa229c2 100644 --- a/lib/protoparser/opentsdb/streamparser.go +++ b/lib/protoparser/opentsdb/streamparser.go @@ -1,6 +1,7 @@ package opentsdb import ( + "errors" "flag" "fmt" "io" @@ -52,7 +53,8 @@ func (ctx *streamContext) Read(r io.Reader) bool { } ctx.reqBuf, ctx.tailBuf, ctx.err = common.ReadLinesBlock(r, ctx.reqBuf, ctx.tailBuf) if ctx.err != nil { - if ne, ok := ctx.err.(net.Error); ok && ne.Timeout() { + var ne net.Error + if errors.As(ctx.err, &ne) && ne.Timeout() { // Flush the read data on timeout and try reading again. ctx.err = nil } else {