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 (
"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)

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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()

View file

@ -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

View file

@ -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 {

View file

@ -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 {