socket_classify.go

 1package server
 2
 3import (
 4	"errors"
 5	"io/fs"
 6	"net"
 7	"syscall"
 8)
 9
10// IsStaleSocketErr reports whether err indicates that a Unix-domain
11// socket file exists on disk but no process is listening on it (a stale
12// or orphaned socket). It returns false for nil and for timeout errors.
13//
14// The classification is cross-platform: ECONNREFUSED and fs.ErrNotExist
15// are defined on every supported OS, so callers on Windows can use the
16// same helper even though stale-socket recovery only applies to Unix
17// sockets in practice.
18func IsStaleSocketErr(err error) bool {
19	if err == nil {
20		return false
21	}
22	var netErr net.Error
23	if errors.As(err, &netErr) && netErr.Timeout() {
24		return false
25	}
26	return errors.Is(err, syscall.ECONNREFUSED) || errors.Is(err, fs.ErrNotExist)
27}