Skip to content

Commit

Permalink
Add Task Retry feature documentation and unit test
Browse files Browse the repository at this point in the history
- Added English and Chinese documentation for the Task Retry feature in README.md and README_zh.md respectively.
- Included an example of how to use the Task Retry feature in GoPool.
- Added a unit test for the Task Retry feature in gopool_test.go.
- The unit test checks that a task is retried the correct number of times.

Signed-off-by: Daniel Hu <tao.hu@merico.dev>
  • Loading branch information
daniel-hutao committed Jul 27, 2023
1 parent 9dde896 commit d61e43f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -231,6 +231,37 @@ func main() {

In this example, if a task returns a result, the result will be printed to the console.

## Task Retry

GoPool supports task retry. If a task fails, it can be retried for a specified number of times. This feature can be enabled by setting the `WithRetryCount` option when creating the pool.

Here is an example of how to use GoPool with task retry:

```go
package main

import (
"errors"
"fmt"

"github.com/devchat-ai/gopool"
)

func main() {
pool := gopool.NewGoPool(100, gopool.WithRetryCount(3))
defer pool.Release()

for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
return nil, errors.New("task error")
})
}
pool.Wait()
}
```

In this example, if a task fails, it will be retried up to 3 times.

## Performance Testing

We have conducted several performance tests to evaluate the efficiency and performance of GoPool. Here are the results:
Expand Down
35 changes: 33 additions & 2 deletions README_zh.md
Expand Up @@ -31,7 +31,7 @@ GoPool 是一个用 Golang 实现的高性能、功能丰富、易于使用的

- [x] **动态工作器调整**:GoPool 可以根据任务数量和系统负载动态调整工作器的数量。

- [x] **优雅的关闭**:GoPool 可以优雅地关闭。当没有更多的任务或收到关闭信号时,它会停止接受新的任务,并等待所有进行中的任务完成后再关闭。
- [x] **优雅关闭**:GoPool 可以优雅地关闭。当没有更多的任务或收到关闭信号时,它会停止接受新的任务,并等待所有进行中的任务完成后再关闭。

- [x] **任务错误处理**:GoPool 可以处理任务执行过程中出现的错误。

Expand All @@ -41,7 +41,7 @@ GoPool 是一个用 Golang 实现的高性能、功能丰富、易于使用的

- [x] **任务重试**:GoPool 为失败的任务提供了重试机制。

- [x] **锁定定制**:GoPool 支持不同类型的锁。你可以使用内置的`sync.Mutex`或自定义锁,如`spinlock.SpinLock`
- [x] **锁定制**:GoPool 支持不同类型的锁。你可以使用内置的`sync.Mutex`或自定义锁,如`spinlock.SpinLock`

- [ ] **任务优先级**:GoPool 支持任务优先级。优先级更高的任务会被优先处理。

Expand Down Expand Up @@ -231,6 +231,37 @@ func main() {

在这个示例中,如果一个任务返回一个结果,结果将被打印到控制台。

## 任务重试

GoPool 支持任务重试。如果任务失败,可以重试指定的次数。可以通过在创建池时设置 `WithRetryCount` 选项来启用此功能。

以下是如何使用带有任务重试的 GoPool 的示例:

```go
package main

import (
"errors"
"fmt"

"github.com/devchat-ai/gopool"
)

func main() {
pool := gopool.NewGoPool(100, gopool.WithRetryCount(3))
defer pool.Release()

for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
return nil, errors.New("task error")
})
}
pool.Wait()
}
```

在这个示例中,如果任务失败,它将重试最多3次。

## 性能测试

我们进行了几个性能测试来评估 GoPool 的效率和性能。以下是结果:
Expand Down
23 changes: 23 additions & 0 deletions gopool_test.go
Expand Up @@ -125,3 +125,26 @@ func TestGoPoolWithResult(t *testing.T) {
}
pool.Wait()
}

func TestGoPoolWithRetry(t *testing.T) {
var retryCount = 3
var taskError = errors.New("task error")
var taskRunCount = 0

pool := NewGoPool(100, WithRetryCount(retryCount))
defer pool.Release()

pool.AddTask(func() (interface{}, error) {
taskRunCount++
if taskRunCount <= retryCount {
return nil, taskError
}
return nil, nil
})

pool.Wait()

if taskRunCount != retryCount+1 {
t.Errorf("Expected task to run %v times, but it ran %v times", retryCount+1, taskRunCount)
}
}

0 comments on commit d61e43f

Please sign in to comment.