-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from sunmi-OS/batter_new_gorm
优化 grom 的初始化
- Loading branch information
Showing
3 changed files
with
57 additions
and
4 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,21 @@ | ||
package retry | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/sunmi-OS/gocore/xlog" | ||
) | ||
|
||
// Retry 重试 func 最大次数,间隔 | ||
func Retry(callback func() error, maxRetries int, interval time.Duration) error { | ||
var err error | ||
for i := 1; i <= maxRetries; i++ { | ||
if err = callback(); err != nil { | ||
xlog.Warnf("Retry(%d) error(%+v)", i, err) | ||
time.Sleep(interval) | ||
continue | ||
} | ||
return nil | ||
} | ||
return 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,25 @@ | ||
package retry | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sunmi-OS/gocore/xlog" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
var count int32 = 0 | ||
|
||
err := Retry(func() error { | ||
if count < 3 { | ||
atomic.AddInt32(&count, 1) | ||
return fmt.Errorf("%d count retry finished", count) | ||
} | ||
return nil | ||
}, 5, 2*time.Second) | ||
if err != nil { | ||
xlog.Error(err) | ||
} | ||
} |