mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Compare commits
6 Commits
c176ba378e
...
v2.3.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6307d624cb | ||
|
|
2f9f8b3f3d | ||
|
|
db5d9407bb | ||
|
|
55ee000684 | ||
|
|
fc7f2509ca | ||
|
|
a97d27c32e |
@@ -4,7 +4,7 @@
|
||||
<br/>
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<br/>
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
|
||||
@@ -365,7 +365,7 @@ func validateScheme(scheme string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])(\.[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])*$`)
|
||||
var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]?)(\.[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]?)+$`)
|
||||
var pathRegex = regexp.MustCompile(`^\/([a-zA-Z0-9%_-]+(?:\/[a-zA-Z0-9%_-]+)*)$`)
|
||||
|
||||
var alphaNumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
|
||||
|
||||
@@ -196,6 +196,14 @@ func TestBuildUrl(t *testing.T) {
|
||||
want: "https://www.test.com/path%20with%20spaces",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
scheme: "https",
|
||||
host: "my.api.edu.cn",
|
||||
path: "/api",
|
||||
query: nil,
|
||||
want: "https://my.api.edu.cn/api",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -128,9 +128,10 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
|
||||
}
|
||||
|
||||
var i uint
|
||||
var lastErr error
|
||||
for i < config.retryTimes {
|
||||
err := retryFunc()
|
||||
if err == nil {
|
||||
lastErr = retryFunc()
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -148,7 +149,7 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
|
||||
lastSlash := strings.LastIndex(funcPath, "/")
|
||||
funcName := funcPath[lastSlash+1:]
|
||||
|
||||
return fmt.Errorf("function %s run failed after %d times retry", funcName, i)
|
||||
return fmt.Errorf("function %s run failed after %d times retry, last error: %w", funcName, i, lastErr)
|
||||
}
|
||||
|
||||
// BackoffStrategy is an interface that defines a method for calculating backoff intervals.
|
||||
|
||||
@@ -118,7 +118,7 @@ func ExampleRetryTimes() {
|
||||
}
|
||||
|
||||
// Output:
|
||||
// function retry.ExampleRetryTimes.func1 run failed after 2 times retry
|
||||
// function retry.ExampleRetryTimes.func1 run failed after 2 times retry, last error: error occurs
|
||||
}
|
||||
|
||||
func ExampleRetry() {
|
||||
|
||||
@@ -15,14 +15,16 @@ func TestRetryFailed(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestRetryFailed")
|
||||
|
||||
var number int
|
||||
customError := errors.New("error occurs")
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
return errors.New("error occurs")
|
||||
return customError
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||
|
||||
assert.IsNotNil(err)
|
||||
assert.Equal(errors.Is(err, customError), true)
|
||||
assert.Equal(DefaultRetryTimes, number)
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,8 @@ func ReduceConcurrent[T any](slice []T, initial T, reducer func(index int, item
|
||||
func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
|
||||
result := make([]T, 0)
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
|
||||
workerChan := make(chan struct{}, numThreads)
|
||||
|
||||
@@ -137,7 +139,9 @@ func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool,
|
||||
defer wg.Done()
|
||||
|
||||
if predicate(i, v) {
|
||||
mu.Lock()
|
||||
result = append(result, v)
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
<-workerChan
|
||||
|
||||
Reference in New Issue
Block a user