Skip to content

Commit

Permalink
refactor: use ErrSchemeMismatch and ECONNREFUSED
Browse files Browse the repository at this point in the history
ref: golang/go#44855

Signed-off-by: Yadong Ding <[email protected]>
  • Loading branch information
Desiki-high committed Dec 22, 2023
1 parent 4974037 commit 5aa6504
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions contrib/nydusify/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"syscall"
"time"

"github.com/containerd/containerd/archive/compression"
Expand Down Expand Up @@ -40,22 +42,6 @@ const (
V6
)

// IsErrHTTPResponseToHTTPSClient returns whether err is
// "http: server gave HTTP response to HTTPS client"
func isErrHTTPResponseToHTTPSClient(err error) bool {
// The error string is unexposed as of Go 1.16, so we can't use `errors.Is`.
// https://github.com/golang/go/issues/44855
const unexposed = "server gave HTTP response to HTTPS client"
return strings.Contains(err.Error(), unexposed)
}

// IsErrConnectionRefused return whether err is
// "connect: connection refused"
func isErrConnectionRefused(err error) bool {
const errMessage = "connect: connection refused"
return strings.Contains(err.Error(), errMessage)
}

func GetNydusFsVersionOrDefault(annotations map[string]string, defaultVersion FsVersion) FsVersion {
if annotations == nil {
return defaultVersion
Expand Down Expand Up @@ -92,7 +78,7 @@ func WithRetry(op func() error) error {
}

func RetryWithHTTP(err error) bool {
return err != nil && (isErrHTTPResponseToHTTPSClient(err) || isErrConnectionRefused(err))
return err != nil && (errors.Is(err, http.ErrSchemeMismatch) || errors.Is(err, syscall.ECONNREFUSED))
}

func MarshalToDesc(data interface{}, mediaType string) (*ocispec.Descriptor, []byte, error) {
Expand Down Expand Up @@ -131,15 +117,10 @@ func IsSupportedArch(arch string) bool {

// A matched nydus image should match os/arch
func MatchNydusPlatform(dst *ocispec.Descriptor, os, arch string) bool {

if dst.Platform.Architecture != arch || dst.Platform.OS != os {
return false
}

if dst.Platform.OSFeatures == nil {
return false
}

for _, feature := range dst.Platform.OSFeatures {
if feature == ManifestOSFeatureNydus {
return true
Expand Down

0 comments on commit 5aa6504

Please sign in to comment.