all: use errors.As instead of type assertion for detecting net.Error

This commit is contained in:
Aliaksandr Valialkin 2020-07-01 00:15:34 +03:00
parent 7c2c8b2981
commit 56ccfa5218
9 changed files with 35 additions and 13 deletions

View file

@ -3,6 +3,7 @@ package s3remote
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"strings" "strings"
@ -287,7 +288,8 @@ func (fs *FS) HasFile(filePath string) (bool, error) {
} }
o, err := fs.s3.GetObject(input) o, err := fs.s3.GetObject(input)
if err != nil { 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, nil
} }
return false, fmt.Errorf("cannot open %q at %s (remote path %q): %w", filePath, fs, path, err) return false, fmt.Errorf("cannot open %q at %s (remote path %q): %w", filePath, fs, path, err)

View file

@ -1,6 +1,7 @@
package graphite package graphite
import ( import (
"errors"
"io" "io"
"net" "net"
"runtime" "runtime"
@ -86,7 +87,8 @@ func serveTCP(ln net.Listener, insertHandler func(r io.Reader) error) {
for { for {
c, err := ln.Accept() c, err := ln.Accept()
if err != nil { if err != nil {
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("graphite: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) logger.Errorf("graphite: temporary error when listening for TCP addr %q: %s", ln.Addr(), err)
time.Sleep(time.Second) 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) n, addr, err := ln.ReadFrom(bb.B)
if err != nil { if err != nil {
writeErrorsUDP.Inc() writeErrorsUDP.Inc()
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("graphite: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) logger.Errorf("graphite: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err)
time.Sleep(time.Second) time.Sleep(time.Second)

View file

@ -1,6 +1,7 @@
package influx package influx
import ( import (
"errors"
"io" "io"
"net" "net"
"runtime" "runtime"
@ -86,7 +87,8 @@ func serveTCP(ln net.Listener, insertHandler func(r io.Reader) error) {
for { for {
c, err := ln.Accept() c, err := ln.Accept()
if err != nil { if err != nil {
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("influx: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) logger.Errorf("influx: temporary error when listening for TCP addr %q: %s", ln.Addr(), err)
time.Sleep(time.Second) 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) n, addr, err := ln.ReadFrom(bb.B)
if err != nil { if err != nil {
writeErrorsUDP.Inc() writeErrorsUDP.Inc()
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("influx: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) logger.Errorf("influx: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err)
time.Sleep(time.Second) time.Sleep(time.Second)

View file

@ -1,6 +1,7 @@
package opentsdb package opentsdb
import ( import (
"errors"
"io" "io"
"net" "net"
"sync" "sync"
@ -64,7 +65,8 @@ func (ls *listenerSwitch) worker() {
for { for {
c, err := ls.ln.Accept() c, err := ls.ln.Accept()
if err != nil { 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) logger.Infof("listenerSwitch: temporary error at %q: %s; sleeping for a second...", ls.ln.Addr(), err)
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue

View file

@ -1,6 +1,7 @@
package opentsdb package opentsdb
import ( import (
"errors"
"io" "io"
"net" "net"
"net/http" "net/http"
@ -106,7 +107,8 @@ func serveTelnet(ln net.Listener, insertHandler func(r io.Reader) error) {
for { for {
c, err := ln.Accept() c, err := ln.Accept()
if err != nil { if err != nil {
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("opentsdb: temporary error when listening for TCP addr %q: %s", ln.Addr(), err) logger.Errorf("opentsdb: temporary error when listening for TCP addr %q: %s", ln.Addr(), err)
time.Sleep(time.Second) 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) n, addr, err := ln.ReadFrom(bb.B)
if err != nil { if err != nil {
writeErrorsUDP.Inc() writeErrorsUDP.Inc()
if ne, ok := err.(net.Error); ok { var ne net.Error
if errors.As(err, &ne) {
if ne.Temporary() { if ne.Temporary() {
logger.Errorf("opentsdb: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err) logger.Errorf("opentsdb: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err)
time.Sleep(time.Second) time.Sleep(time.Second)

View file

@ -1,6 +1,7 @@
package netutil package netutil
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -57,7 +58,8 @@ func (sc *statConn) Read(p []byte) (int, error) {
sc.cm.readCalls.Inc() sc.cm.readCalls.Inc()
sc.cm.readBytes.Add(n) sc.cm.readBytes.Add(n)
if err != nil && err != io.EOF { 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() sc.cm.readTimeouts.Inc()
} else { } else {
sc.cm.readErrors.Inc() sc.cm.readErrors.Inc()
@ -71,7 +73,8 @@ func (sc *statConn) Write(p []byte) (int, error) {
sc.cm.writeCalls.Inc() sc.cm.writeCalls.Inc()
sc.cm.writtenBytes.Add(n) sc.cm.writtenBytes.Add(n)
if err != nil { 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() sc.cm.writeTimeouts.Inc()
} else { } else {
sc.cm.writeErrors.Inc() sc.cm.writeErrors.Inc()

View file

@ -1,6 +1,7 @@
package netutil package netutil
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"net" "net"
@ -63,7 +64,8 @@ func (ln *TCPListener) Accept() (net.Conn, error) {
conn, err := ln.Listener.Accept() conn, err := ln.Listener.Accept()
ln.accepts.Inc() ln.accepts.Inc()
if err != nil { 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) logger.Errorf("temporary error when listening for TCP addr %q: %s", ln.Addr(), err)
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue

View file

@ -1,6 +1,7 @@
package graphite package graphite
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "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) ctx.reqBuf, ctx.tailBuf, ctx.err = common.ReadLinesBlock(r, ctx.reqBuf, ctx.tailBuf)
if ctx.err != nil { 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. // Flush the read data on timeout and try reading again.
ctx.err = nil ctx.err = nil
} else { } else {

View file

@ -1,6 +1,7 @@
package opentsdb package opentsdb
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "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) ctx.reqBuf, ctx.tailBuf, ctx.err = common.ReadLinesBlock(r, ctx.reqBuf, ctx.tailBuf)
if ctx.err != nil { 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. // Flush the read data on timeout and try reading again.
ctx.err = nil ctx.err = nil
} else { } else {