mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 01:32:27 +08:00
Compare commits
10 Commits
c175b202de
...
v2.3.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6307d624cb | ||
|
|
2f9f8b3f3d | ||
|
|
db5d9407bb | ||
|
|
55ee000684 | ||
|
|
fc7f2509ca | ||
|
|
a97d27c32e | ||
|
|
c176ba378e | ||
|
|
a3a24fc381 | ||
|
|
9caf2ffb1c | ||
|
|
1a5c31fd02 |
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<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://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/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)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<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://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/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)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -2,10 +2,9 @@
|
|||||||
outline: deep
|
outline: deep
|
||||||
---
|
---
|
||||||
|
|
||||||
# API概述
|
# API 概述
|
||||||
|
|
||||||
<b>lancet(柳叶刀)是一个功能强大、全面、高效、可复用的go语言工具函数库。包含25个包,超过600个工具函数。功能涵盖字符串处理、切片处理、网络、并发、加解密、文件处理、时间/日期、流处理、迭代器等等。</b>
|
|
||||||
|
|
||||||
|
<b>lancet(柳叶刀)是一个功能强大、全面、高效、可复用的 go 语言工具函数库。包含 25 个包,超过 600 个工具函数。功能涵盖字符串处理、切片处理、网络、并发、加解密、文件处理、时间/日期、流处理、迭代器等等。</b>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.package-title {
|
.package-title {
|
||||||
@@ -27,7 +26,7 @@ outline: deep
|
|||||||
display: inline-block;
|
display: inline-block;
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
line-height: 40px;
|
line-height: 40px;
|
||||||
background: #10b981;
|
background: #6cadf5;
|
||||||
border: 1px solid;
|
border: 1px solid;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
@@ -67,4 +66,4 @@ outline: deep
|
|||||||
<div class="package-cell">validator</div>
|
<div class="package-cell">validator</div>
|
||||||
<div class="package-cell">xerror</div>
|
<div class="package-cell">xerror</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ algorithm 算法包实现一些基本算法,sort,search,lrucache。
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/lrucache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lrucache.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ outline: deep
|
|||||||
|
|
||||||
<b>Lancet (Lancet) is a powerful, comprehensive, efficient and reusable go language tool function library. Contains 25 packages, more than 600 utility functions. Functions cover string processing, slice processing, network, concurrency, encryption and decryption, file processing, time/date, stream processing, iterators, and more.</b>
|
<b>Lancet (Lancet) is a powerful, comprehensive, efficient and reusable go language tool function library. Contains 25 packages, more than 600 utility functions. Functions cover string processing, slice processing, network, concurrency, encryption and decryption, file processing, time/date, stream processing, iterators, and more.</b>
|
||||||
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.package-title {
|
.package-title {
|
||||||
color: black;
|
color: black;
|
||||||
@@ -27,7 +26,7 @@ outline: deep
|
|||||||
display: inline-block;
|
display: inline-block;
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
line-height: 40px;
|
line-height: 40px;
|
||||||
background: #059669;
|
background: #6cadf5;
|
||||||
border: 1px solid;
|
border: 1px solid;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
@@ -47,6 +46,7 @@ outline: deep
|
|||||||
<div class="package-cell">cryptor</div>
|
<div class="package-cell">cryptor</div>
|
||||||
<div class="package-cell">datastructure</div>
|
<div class="package-cell">datastructure</div>
|
||||||
<div class="package-cell">datetime</div>
|
<div class="package-cell">datetime</div>
|
||||||
|
<div class="package-cell">eventbus</div>
|
||||||
<div class="package-cell">fileutil</div>
|
<div class="package-cell">fileutil</div>
|
||||||
<div class="package-cell">formatter</div>
|
<div class="package-cell">formatter</div>
|
||||||
<div class="package-cell">function</div>
|
<div class="package-cell">function</div>
|
||||||
@@ -67,4 +67,3 @@ outline: deep
|
|||||||
<div class="package-cell">xerror</div>
|
<div class="package-cell">xerror</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Package algorithm implements some basic algorithm. eg. sort, search.
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/lrucache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lrucache.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -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]+$`)
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -128,16 +128,19 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
if i < config.retryTimes-1 { // Only wait if it's not the last retry
|
||||||
select {
|
select {
|
||||||
case <-time.After(config.backoffStrategy.CalculateInterval()):
|
case <-time.After(config.backoffStrategy.CalculateInterval()):
|
||||||
case <-config.context.Done():
|
case <-config.context.Done():
|
||||||
return errors.New("retry is cancelled")
|
return errors.New("retry is cancelled")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
@@ -146,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.
|
||||||
|
|||||||
@@ -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() {
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user