forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tidb: New domain with retry and backoff (pingcap#1640)
- Loading branch information
Showing
3 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2016 PingCAP, 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/juju/errors" | ||
) | ||
|
||
// RunWithRetry will run the f with backoff and retry. | ||
// retryCnt: Max retry count | ||
// backoff: When run f failed, it will sleep backoff * triedCount time.Millisecond. | ||
// Function f should have two return value. The first one is an bool which indicate if the err if retryable. | ||
// The second is if the f meet any error. | ||
func RunWithRetry(retryCnt int, backoff uint64, f func() (bool, error)) (err error) { | ||
for i := 1; i <= retryCnt; i++ { | ||
var retryAble bool | ||
retryAble, err = f() | ||
if err == nil || !retryAble { | ||
return errors.Trace(err) | ||
} | ||
sleepTime := time.Duration(backoff*uint64(i)) * time.Millisecond | ||
time.Sleep(sleepTime) | ||
} | ||
return errors.Trace(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2016 PingCAP, 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testleak" | ||
) | ||
|
||
var _ = Suite(&testMiscSuite{}) | ||
|
||
type testMiscSuite struct { | ||
} | ||
|
||
func (s *testMiscSuite) SetUpSuite(c *C) { | ||
} | ||
|
||
func (s *testMiscSuite) TearDownSuite(c *C) { | ||
} | ||
|
||
func (s testMiscSuite) TestRunWithRetry(c *C) { | ||
defer testleak.AfterTest(c)() | ||
// Run succ. | ||
cnt := 0 | ||
err := RunWithRetry(3, 1, func() (bool, error) { | ||
cnt += 1 | ||
if cnt < 2 { | ||
return true, errors.New("err") | ||
} | ||
return true, nil | ||
}) | ||
c.Assert(err, IsNil) | ||
c.Assert(cnt, Equals, 2) | ||
|
||
// Run failed. | ||
cnt = 0 | ||
err = RunWithRetry(3, 1, func() (bool, error) { | ||
cnt += 1 | ||
if cnt < 4 { | ||
return true, errors.New("err") | ||
} | ||
return true, nil | ||
}) | ||
c.Assert(err, NotNil) | ||
c.Assert(cnt, Equals, 3) | ||
|
||
// Run failed. | ||
cnt = 0 | ||
err = RunWithRetry(3, 1, func() (bool, error) { | ||
cnt += 1 | ||
if cnt < 2 { | ||
return false, errors.New("err") | ||
} | ||
return true, nil | ||
}) | ||
c.Assert(err, NotNil) | ||
c.Assert(cnt, Equals, 1) | ||
} |