package temperrcatcher

Import Path
	github.com/jbenet/go-temp-err-catcher (on go.dev)

Dependency Relation
	imports 2 packages, and imported by one package

Involved Source Files Package temperrcatcher provides a TempErrCatcher object, which implements simple error-retrying functionality. It is meant to be used with things like net.Lister.Accept: import ( tec "github.com/jbenet/go-temp-err-catcher" ) func listen(listener net.Listener) { var c tec.TempErrCatcher for { conn, err := listener.Accept() if err != nil && c.IsTemporary(c) { continue } return conn, err } } You can make your errors implement `Temporary`: type errTemp struct { e error } func (e errTemp) Temporary() bool { return true } func (e errTemp) Error() string { return e.e.Error() } err := errors.New("beep boop") var c tec.TempErrCatcher c.IsTemporary(err) // false c.IsTemporary(errTemp{err}) // true Or just use `ErrTemp`: err := errors.New("beep boop") var c tec.TempErrCatcher c.IsTemporary(err) // false c.IsTemporary(tec.ErrTemp{err}) // true You can also define an `IsTemp` function to classify errors: var ErrSkip = errors.New("this should be skipped") var ErrNotSkip = errors.New("this should not be skipped") var c tec.TempErrCatcher c.IsTemp = func(e error) bool { return e == ErrSkip } c.IsTemporary(ErrSkip) // true c.IsTemporary(ErrNotSkip) // false c.IsTemporary(ErrTemp) // false! no longer accepts Temporary() Package temperrcatcher provides a TempErrCatcher object, which implements simple error-retrying functionality.
Package-Level Type Names (total 3)
/* sort by: | */
ErrTemporary wraps any error and implements Temporary function. err := errors.New("beep boop") var c tec.TempErrCatcher c.IsTemporary(err) // false c.IsTemporary(tec.ErrTemp{err}) // true Err error ( ErrTemporary) Error() string ( ErrTemporary) String() string ( ErrTemporary) Temporary() bool ErrTemporary : Temporary ErrTemporary : error ErrTemporary : expvar.Var ErrTemporary : fmt.Stringer
TempErrCatcher catches temporary errors for you. It then sleeps for a bit before returning (you should then try again). This may seem odd, but it's exactly what net/http does: http://golang.org/src/net/http/server.go?s=51504:51550#L1728 You can set a few options in TempErrCatcher. They all have defaults so a zero TempErrCatcher is ready to be used: var c tec.TempErrCatcher c.IsTemporary(tempErr) // the classifier to use. default: ErrIsTemporary // the maximum time to wait. default: time.Second // the delay to start with. default: InitialDelay // the wait func to call. default: time.Sleep IsTemporary checks whether an error is temporary. It will call tec.Wait before returning, with a delay. The delay is also doubled, so we do not constantly spin. This is the strategy net.Listener uses. Note: you will want to call Reset() if you get a success, so that the stored delay is brough back to 0. Reset sets the internal delay counter to 0
Temporary is an interface errors can implement to ensure they are correctly classified by the default TempErrCatcher classifier ( Temporary) Temporary() bool ErrTemporary *github.com/libp2p/go-yamux/v5.Error *github.com/libp2p/go-yamux/v5.GoAwayError *github.com/ncruces/go-sqlite3.Error github.com/ncruces/go-sqlite3.ErrorCode github.com/ncruces/go-sqlite3.ExtendedErrorCode *github.com/pion/dtls/v2/pkg/protocol.FatalError *github.com/pion/dtls/v2/pkg/protocol.HandshakeError *github.com/pion/dtls/v2/pkg/protocol.InternalError *github.com/pion/dtls/v2/pkg/protocol.TemporaryError *github.com/pion/dtls/v2/pkg/protocol.TimeoutError *github.com/pion/dtls/v3/pkg/protocol.FatalError *github.com/pion/dtls/v3/pkg/protocol.HandshakeError *github.com/pion/dtls/v3/pkg/protocol.InternalError *github.com/pion/dtls/v3/pkg/protocol.TemporaryError *github.com/pion/dtls/v3/pkg/protocol.TimeoutError *github.com/quic-go/quic-go/internal/qerr.HandshakeTimeoutError *github.com/quic-go/quic-go/internal/qerr.IdleTimeoutError *github.com/quic-go/quic-go/internal/qerr.StatelessResetError github.com/soheilhy/cmux.ErrNotMatched google.golang.org/grpc/internal/transport.ConnectionError *internal/poll.DeadlineExceededError *net.AddrError *net.DNSConfigError *net.DNSError net.Error (interface) net.InvalidAddrError *net.OpError *net.ParseError net.UnknownNetworkError *net/url.Error syscall.Errno
Package-Level Functions (only one)
ErrIsTemporary returns whether an error is Temporary(), iff it implements the Temporary interface.
Package-Level Variables (only one)
InitialDelay governs how long to wait the first time. This is defaulted to time.Millisecond, which makes sense for network listener failures. You may want a much smaller delay. You can configure this package wide, or in each TempErrCatcher