编写一个main函数来测试我下面这个流水线 package main import ( "context" "errors" "fmt" "sync" "time" ) //…
- User
编写一个main函数来测试我下面这个流水线
package main
import ( "context" "errors" "fmt" "sync" "time" )
// Task 定义任务的数据结构 type Task struct { ID int // 原始任务ID(批次ID) Data interface{} // 输入数据 Result interface{} // 处理结果 Retries int // 重试次数 }
// TaskFunc 定义任务处理函数类型,返回多个结果 type TaskFunc func(ctx context.Context, data interface{}) ([]interface{}, error) type CustomFunc func(ctx context.Context, data interface{}, resultChan <-chan interface{}) error
type Layer interface { InitLayer(workerCount int, timeout time.Duration, maxRetries int) Layer ProcessTask(ctx context.Context, task *Task) (interface{}, int, error) GetWorkerNum() int }
// StandardLayer 定义流水线的每一层 type StandardLayer struct { workerCount int timeout time.Duration maxRetries int taskFunc TaskFunc }
func (l *StandardLayer) InitLayer(workerCount int, timeout time.Duration, maxRetries int) Layer { l.workerCount = workerCount l.timeout = timeout l.maxRetries = maxRetries return l }
func (l *StandardLayer) ProcessTask(ctx context.Context, task *Task) (interface{}, int, error) { ctx, cancel := context.WithTimeout(ctx, l.timeout) defer cancel() for retries := task.Retries; retries <= l.maxRetries; retries++ { // 创建带超时的上下文 results, err := l.taskFunc(ctx, task.Data) if err != nil { if retries >= l.maxRetries { // 超过重试次数,记录失败 fmt.Printf("Task ID %d failed after %d retries: %v\n", task.ID, retries, err) return nil, retries, err } // 记录重试信息 fmt.Printf("Task ID %d retry %d due to error: %v\n", task.ID, retries+1, err) continue } return results, retries, nil } // 不应到达此处 return nil, l.maxRetries, errors.New("unreachable code in processTask") }
func (l *StandardLayer) GetWorkerNum() int { return l.workerCount }
// Layer 定义流水线的每一层 type CustomsLayer struct { workerCount int timeout time.Duration maxRetries int taskFunc CustomFunc }
func (l *CustomsLayer) InitLayer(workerCount int, timeout time.Duration, maxRetries int) Layer { l.workerCount = workerCount l.timeout = timeout l.maxRetries = maxRetries return l }
func (l *CustomsLayer) ProcessTask(ctx context.Context, task *Task) (interface{}, int, error) { // 将channel传给下一层 resultChan := make(chan interface{}) go func() { err := l.taskFunc(ctx, task.Data, resultChan) if err != nil {
} }() return resultChan, 0, nil}
func (l *CustomsLayer) GetWorkerNum() int { return l.workerCount }
// Pipeline 定义整个流水线结构 type Pipeline struct { layers []Layer }
// NewPipeline 创建一个新的流水线实例 func NewPipeline() *Pipeline { return &Pipeline{ layers: make([]Layer, 0), } }
// AddLayer 向流水线中添加一层 func (p *Pipeline) AddStandardLayer(taskFunc TaskFunc, workerCount int, timeout time.Duration, maxRetries int) { layer := &StandardLayer{} p.layers = append(p.layers, layer.InitLayer(workerCount, timeout, maxRetries)) }
// AddLayer 向流水线中添加一层 func (p *Pipeline) AddCustomLayer(taskFunc TaskFunc, workerCount int, timeout time.Duration, maxRetries int) { layer := &CustomsLayer{} p.layers = append(p.layers, layer.InitLayer(workerCount, timeout, maxRetries)) }
- Assistant