1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 11:12:28 +08:00

Compare commits

...

7 Commits

Author SHA1 Message Date
dudaodong
6307d624cb Merge branch 'rc' of github.com:duke-git/lancet into rc 2025-07-07 10:18:37 +08:00
dudaodong
2f9f8b3f3d release v2.3.7 2025-07-07 10:18:05 +08:00
jake
db5d9407bb Update slice_concurrent.go (#316) 2025-06-25 15:33:07 +08:00
dudaodong
55ee000684 merge rc 2025-06-23 11:32:51 +08:00
dudaodong
fc7f2509ca fix: fix issue #314 2025-06-23 11:31:54 +08:00
残念
a97d27c32e perf(retry): the error returned by the Retry function contains the last error (#315) 2025-06-23 11:04:54 +08:00
Grigoris Thanasoulas
c176ba378e arrayqueue: Fix bug in Back() method (#313) 2025-06-20 23:05:14 +08:00
9 changed files with 24 additions and 9 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.3.6-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.3.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.3.6-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.3.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)

View File

@@ -60,7 +60,7 @@ func (q *ArrayQueue[T]) Front() T {
// Back return back value of queue // Back return back value of queue
func (q *ArrayQueue[T]) Back() T { func (q *ArrayQueue[T]) Back() T {
return q.data[q.size-1] return q.data[q.tail-1]
} }
// EnQueue put element into queue // EnQueue put element into queue

View File

@@ -365,7 +365,7 @@ func validateScheme(scheme string) error {
return nil 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 pathRegex = regexp.MustCompile(`^\/([a-zA-Z0-9%_-]+(?:\/[a-zA-Z0-9%_-]+)*)$`)
var alphaNumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]+$`) var alphaNumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]+$`)

View File

@@ -196,6 +196,14 @@ func TestBuildUrl(t *testing.T) {
want: "https://www.test.com/path%20with%20spaces", want: "https://www.test.com/path%20with%20spaces",
wantErr: false, 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 { for _, tt := range tests {

View File

@@ -128,9 +128,10 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
} }
var i uint var i uint
var lastErr error
for i < config.retryTimes { for i < config.retryTimes {
err := retryFunc() lastErr = retryFunc()
if err == nil { if lastErr == nil {
return nil return nil
} }
@@ -148,7 +149,7 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
lastSlash := strings.LastIndex(funcPath, "/") lastSlash := strings.LastIndex(funcPath, "/")
funcName := funcPath[lastSlash+1:] 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. // BackoffStrategy is an interface that defines a method for calculating backoff intervals.

View File

@@ -118,7 +118,7 @@ func ExampleRetryTimes() {
} }
// Output: // 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() { func ExampleRetry() {

View File

@@ -15,14 +15,16 @@ func TestRetryFailed(t *testing.T) {
assert := internal.NewAssert(t, "TestRetryFailed") assert := internal.NewAssert(t, "TestRetryFailed")
var number int var number int
customError := errors.New("error occurs")
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
return errors.New("error occurs") return customError
} }
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50)) err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
assert.IsNotNil(err) assert.IsNotNil(err)
assert.Equal(errors.Is(err, customError), true)
assert.Equal(DefaultRetryTimes, number) assert.Equal(DefaultRetryTimes, number)
} }

View File

@@ -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 { func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
result := make([]T, 0) result := make([]T, 0)
var wg sync.WaitGroup var wg sync.WaitGroup
var mu sync.Mutex
workerChan := make(chan struct{}, numThreads) 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() defer wg.Done()
if predicate(i, v) { if predicate(i, v) {
mu.Lock()
result = append(result, v) result = append(result, v)
mu.Unlock()
} }
<-workerChan <-workerChan