MCPcopy
hub / github.com/minio/minio-go / IsNetworkOrHostDown

Function IsNetworkOrHostDown

utils.go:653–713  ·  view source on GitHub ↗

IsNetworkOrHostDown - if there was a network error or if the host is down. expectTimeouts indicates that *context* timeouts are expected and does not indicate a downed host. Other timeouts still returns down.

(err error, expectTimeouts bool)

Source from the content-addressed store, hash-verified

651// expectTimeouts indicates that *context* timeouts are expected and does not
652// indicate a downed host. Other timeouts still returns down.
653func IsNetworkOrHostDown(err error, expectTimeouts bool) bool {
654 if err == nil {
655 return false
656 }
657
658 if errors.Is(err, context.Canceled) {
659 return false
660 }
661
662 if expectTimeouts && errors.Is(err, context.DeadlineExceeded) {
663 return false
664 }
665
666 if errors.Is(err, context.DeadlineExceeded) {
667 return true
668 }
669
670 // We need to figure if the error either a timeout
671 // or a non-temporary error.
672 urlErr := &url.Error{}
673 if errors.As(err, &urlErr) {
674 switch urlErr.Err.(type) {
675 case *net.DNSError, *net.OpError, net.UnknownNetworkError, *tls.CertificateVerificationError:
676 return true
677 }
678 }
679 var e net.Error
680 if errors.As(err, &e) {
681 if e.Timeout() {
682 return true
683 }
684 }
685
686 // Fallback to other mechanisms.
687 switch {
688 case strings.Contains(err.Error(), "Connection closed by foreign host"):
689 return true
690 case strings.Contains(err.Error(), "TLS handshake timeout"):
691 // If error is - tlsHandshakeTimeoutError.
692 return true
693 case strings.Contains(err.Error(), "i/o timeout"):
694 // If error is - tcp timeoutError.
695 return true
696 case strings.Contains(err.Error(), "connection timed out"):
697 // If err is a net.Dial timeout.
698 return true
699 case strings.Contains(err.Error(), "connection refused"):
700 // If err is connection refused
701 return true
702 case strings.Contains(err.Error(), "server gave HTTP response to HTTPS client"):
703 // If err is TLS client is used with HTTP server
704 return true
705 case strings.Contains(err.Error(), "Client sent an HTTP request to an HTTPS server"):
706 // If err is plain-text Client is used with a HTTPS server
707 return true
708 case strings.Contains(strings.ToLower(err.Error()), "503 service unavailable"):
709 // Denial errors
710 return true

Callers 2

HealthCheckMethod · 0.85
doMethod · 0.85

Calls 3

IsMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected