Skip to content

Commit

Permalink
refactor: remove retry mechanism
Browse files Browse the repository at this point in the history
It turned out to be useless.
  • Loading branch information
aofei committed May 16, 2021
1 parent 893c3f6 commit 3a16987
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 71 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/aofei/air v0.22.0
github.com/fsnotify/fsnotify v1.4.9
github.com/google/uuid v1.2.0 // indirect
github.com/goproxy/goproxy v0.6.1
github.com/goproxy/goproxy v0.6.2
github.com/json-iterator/go v1.1.11 // indirect
github.com/klauspost/cpuid/v2 v2.0.6 // indirect
github.com/magiconair/properties v1.8.5 // indirect
Expand All @@ -29,9 +29,9 @@ require (
github.com/spf13/viper v1.7.1
github.com/tdewolff/parse/v2 v2.5.16 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.2 // indirect
github.com/yuin/goldmark v1.3.5
github.com/yuin/goldmark v1.3.6
golang.org/x/mod v0.4.2
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/goproxy/goproxy v0.6.1 h1:3K2bYCZ+QkHOnqtCivjmPkGBVBoRWMWw9TIqq2FVkGU=
github.com/goproxy/goproxy v0.6.1/go.mod h1:B5HBG1xFsJqfOjEMOHVwaFm8eY/qFuF2SZ0OXtwV06w=
github.com/goproxy/goproxy v0.6.2 h1:Q3243zjiKzXErE9JAgukyCLjSovjCOTSauwXJBi/U40=
github.com/goproxy/goproxy v0.6.2/go.mod h1:B5HBG1xFsJqfOjEMOHVwaFm8eY/qFuF2SZ0OXtwV06w=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down Expand Up @@ -271,8 +271,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.3.6 h1:rvdBidUJAJM2O9VLcNTB4oRwxG33uIxY+zUq6yWUT8c=
github.com/yuin/goldmark v1.3.6/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
Expand Down Expand Up @@ -364,8 +364,8 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
80 changes: 18 additions & 62 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"path"
"strconv"
"strings"
"time"

"github.com/air-gases/cacheman"
"github.com/aofei/air"
Expand Down Expand Up @@ -226,15 +225,14 @@ func qiniuKodoUpload(
}

if size <= qiniuKodoMultipartUploadPartSize {
PutObject:
content := content
if ra, ok := content.(io.ReaderAt); ok {
content = io.NewSectionReader(ra, 0, size)
} else if _, err := content.Seek(0, io.SeekStart); err != nil {
return err
}

if _, err := qiniuKodoCore.PutObject(
_, err := qiniuKodoCore.PutObject(
ctx,
qiniuKodoBucketName,
name,
Expand All @@ -245,19 +243,24 @@ func qiniuKodoUpload(
minio.PutObjectOptions{
ContentType: contentType,
},
); err != nil {
if isRetryableMinIOError(err) {
time.Sleep(time.Second)
goto PutObject
}
)

return err
}
return err
}

var uploadID string
uploadID, err := qiniuKodoCore.NewMultipartUpload(
ctx,
qiniuKodoBucketName,
name,
minio.PutObjectOptions{
ContentType: contentType,
},
)
if err != nil {
return err
}
defer func() {
if err != nil && uploadID != "" {
if err != nil {
qiniuKodoCore.AbortMultipartUpload(
ctx,
qiniuKodoBucketName,
Expand All @@ -267,31 +270,13 @@ func qiniuKodoUpload(
}
}()

NewMultipartUpload:
if uploadID, err = qiniuKodoCore.NewMultipartUpload(
ctx,
qiniuKodoBucketName,
name,
minio.PutObjectOptions{
ContentType: contentType,
},
); err != nil {
if isRetryableMinIOError(err) {
time.Sleep(time.Second)
goto NewMultipartUpload
}

return err
}

var completeParts []minio.CompletePart
for offset := int64(0); offset < size; {
partSize := qiniuKodoMultipartUploadPartSize
if r := size - offset; r < partSize {
partSize = r
}

PutObjectPart:
content := content
if ra, ok := content.(io.ReaderAt); ok {
content = io.NewSectionReader(ra, offset, partSize)
Expand All @@ -315,11 +300,6 @@ NewMultipartUpload:
nil,
)
if err != nil {
if isRetryableMinIOError(err) {
time.Sleep(time.Second)
goto PutObjectPart
}

return err
}

Expand All @@ -331,39 +311,15 @@ NewMultipartUpload:
offset += part.Size
}

CompleteMultipartUpload:
if _, err := qiniuKodoCore.CompleteMultipartUpload(
_, err = qiniuKodoCore.CompleteMultipartUpload(
ctx,
qiniuKodoBucketName,
name,
uploadID,
completeParts,
); err != nil {
if isRetryableMinIOError(err) {
time.Sleep(time.Second)
goto CompleteMultipartUpload
}

return err
}

return nil
}

// isRetryableMinIOError reports whether the err is a retryable MinIO error.
func isRetryableMinIOError(err error) bool {
switch minio.ToErrorResponse(err).StatusCode {
case http.StatusTooManyRequests,
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
return true
}

t, ok := err.(interface{ Timeout() bool })
)

return ok && t.Timeout()
return err
}

// isNotFoundMinIOError reports whether the err is MinIO not found error.
Expand Down

0 comments on commit 3a16987

Please sign in to comment.