From c176ba378e8807033e58ccc47839f7614df36866 Mon Sep 17 00:00:00 2001 From: Grigoris Thanasoulas Date: Fri, 20 Jun 2025 17:05:14 +0200 Subject: [PATCH 1/5] arrayqueue: Fix bug in Back() method (#313) --- datastructure/queue/arrayqueue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastructure/queue/arrayqueue.go b/datastructure/queue/arrayqueue.go index d15e933..4532894 100644 --- a/datastructure/queue/arrayqueue.go +++ b/datastructure/queue/arrayqueue.go @@ -60,7 +60,7 @@ func (q *ArrayQueue[T]) Front() T { // Back return back value of queue func (q *ArrayQueue[T]) Back() T { - return q.data[q.size-1] + return q.data[q.tail-1] } // EnQueue put element into queue From a97d27c32ee650552d2d0962ae7fa3cab9dfa72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=8B=E5=BF=B5?= Date: Mon, 23 Jun 2025 11:04:54 +0800 Subject: [PATCH 2/5] perf(retry): the error returned by the Retry function contains the last error (#315) --- retry/retry.go | 7 ++++--- retry/retry_example_test.go | 2 +- retry/retry_test.go | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/retry/retry.go b/retry/retry.go index a30e6a1..c5533c1 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -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. diff --git a/retry/retry_example_test.go b/retry/retry_example_test.go index 3c9cbeb..20cfcda 100644 --- a/retry/retry_example_test.go +++ b/retry/retry_example_test.go @@ -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() { diff --git a/retry/retry_test.go b/retry/retry_test.go index 125c2bc..ac2aac7 100644 --- a/retry/retry_test.go +++ b/retry/retry_test.go @@ -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) } From fc7f2509ca92d8ee50d5f772e93b0b03f70162bd Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 23 Jun 2025 11:31:54 +0800 Subject: [PATCH 3/5] fix: fix issue #314 --- netutil/net.go | 2 +- netutil/net_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/netutil/net.go b/netutil/net.go index 23af993..1b3b2fe 100644 --- a/netutil/net.go +++ b/netutil/net.go @@ -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]+$`) diff --git a/netutil/net_test.go b/netutil/net_test.go index 9b89e85..a080a48 100644 --- a/netutil/net_test.go +++ b/netutil/net_test.go @@ -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 { From db5d9407bba35db9dfc3d5ef24abd2598050c328 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 25 Jun 2025 15:33:07 +0800 Subject: [PATCH 4/5] Update slice_concurrent.go (#316) --- slice/slice_concurrent.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slice/slice_concurrent.go b/slice/slice_concurrent.go index b49b55a..2cd0d9b 100644 --- a/slice/slice_concurrent.go +++ b/slice/slice_concurrent.go @@ -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 From 2f9f8b3f3df2d5ad47e116935f7d0d4a794bab0c Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 7 Jul 2025 10:18:05 +0800 Subject: [PATCH 5/5] release v2.3.7 --- README.md | 2 +- README_zh-CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 239eedc..bf6d5df 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
![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) [![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) diff --git a/README_zh-CN.md b/README_zh-CN.md index 7a60211..2c7bb84 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@
![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) [![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)