replaced retry with simple loop

This commit is contained in:
Anand Babu (AB) Periasamy
2015-04-18 00:19:08 -07:00
parent 449ac416f0
commit b77ea5e763
4 changed files with 21 additions and 130 deletions
+11 -7
View File
@@ -88,11 +88,14 @@ func doListCmd(ctx *cli.Context) {
if bucket == "" && getURLType(u) != urlFS {
var err error
var buckets []*client.Bucket
for r := retries.init(); r.retry(); {
for i := 0; ; i++ {
buckets, err = clnt.ListBuckets()
if !isValidRetry(err) {
break
if err == nil || i >= globalMaxRetryFlag {
break // Success. No more retries.
}
// Progressively longer delays
time.Sleep(time.Duration(i*i) * time.Second)
}
if err != nil {
log.Debug.Println(iodine.New(err, nil))
@@ -101,12 +104,13 @@ func doListCmd(ctx *cli.Context) {
console.Infoln()
printBuckets(buckets)
} else {
for r := retries.init(); r.retry(); {
for i := 0; ; i++ {
items, err = clnt.ListObjects(bucket, object)
if !isValidRetry(err) {
break
if err == nil || i >= globalMaxRetryFlag {
break // Success. No more retries.
}
// Progressively longer delays
time.Sleep(time.Duration(i*i) * time.Second)
}
if err != nil {
log.Debug.Println(iodine.New(err, nil))
+7 -4
View File
@@ -17,6 +17,8 @@
package main
import (
"time"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/console"
@@ -67,12 +69,13 @@ func doMakeBucketCmd(ctx *cli.Context) {
console.Fatalf("mc: Unable to create new client to [%s]\n", u)
}
// retry - 5 times
for r := retries.init(); r.retry(); {
for i := 0; ; i++ {
err = clnt.PutBucket(bucket)
if !isValidRetry(err) {
break
if err == nil || i >= globalMaxRetryFlag {
break // Success. No more retries.
}
// Progressively longer delays
time.Sleep(time.Duration(i*i) * time.Second)
}
if err != nil {
log.Debug.Println(iodine.New(err, nil))
+3 -2
View File
@@ -24,8 +24,9 @@ import (
)
var (
globalDebugFlag = false // Debug flag set via command line
globalQuietFlag = false // Quiet flag set via command line
globalDebugFlag = false // Debug flag set via command line
globalQuietFlag = false // Quiet flag set via command line
globalMaxRetryFlag = 5 // Maximum number of retries
mcUserAgent = "Minio/" +
strings.SplitN(mcGitCommitHash, " ", 3)[1] +
-117
View File
@@ -1,117 +0,0 @@
/*
* Mini Copy, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"io"
"net"
"time"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
)
// isValidRetry - check if we should retry for the given error sequence
func isValidRetry(err error) bool {
if err == nil {
return false
}
// handle io Errors separately since they are typed as *errors.String
switch iodine.ToError(err) {
case io.ErrUnexpectedEOF:
return true
}
// DNSError, Network Operation error
switch e := iodine.ToError(err).(type) {
case *net.DNSError:
return true
case *net.OpError:
switch e.Op {
case "read", "write", "dial":
return true
}
}
return false
}
// TODO: this is global, make it configurable
var retries = waitTime{
duration: 5 * time.Second,
delayDuration: 1 * time.Second,
}
// waitTime represents sequence waiting for an action to complete successfully.
type waitTime struct {
duration time.Duration // total duration of retries.
delayDuration time.Duration // delay interval between each retry
}
type retryOp struct {
waittime waitTime
last time.Time
end time.Time
count int
}
// instantiate new sequence of retries for the given waittime.
func (s waitTime) init() *retryOp {
console.Error("Retrying... ")
now := time.Now()
return &retryOp{
waittime: s,
last: now,
end: now.Add(s.duration),
}
}
// retry()
func (a *retryOp) retry() bool {
// grab current time
now := time.Now()
// get sleep duration based on configured waitTime delayDuration
sleep := func() time.Duration {
sleep := a.waittime.delayDuration - now.Sub(a.last)
if sleep < 0 {
return 0
}
return sleep
}
// check if we have reached the end, if yes return false
if !now.Add(sleep()).Before(a.end) {
console.Infoln() // print a new line
return false
}
// sleep for sleep duration
if sleep() > 0 && a.count > 0 {
time.Sleep(sleep())
now = time.Now()
}
// increment retry count
a.count++
// copy current time as last time
a.last = now
// print current retry count
console.Infof("%d ", a.count)
return true
}