{"modulePath":"github.com/AlaudaDevops/pkg","version":"v0.14.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package parallel\n\nPackage parallel used to execute tasks in parallel\n\n## Constants\n\n```go\nconst (\n\t// DefaultConcurrentNum for default concurrent number of parallel\n\tDefaultConcurrentNum = 10\n)\n```\n\n## Functions\n\n```go\nfunc FetchAllPage(ctx context.Context, concurrency int, pageSize int, fetchFun FetchFunc) ([]interface{}, error)\n```\nFetchAllPage get all data concurrently\n\n```go\nfunc PageRequest(ctx context.Context, logName string, concurrency int, pageSize int, f PageRequestFunc) ([]interface{}, error)\n```\nPageRequest is concurrent request paging\n\n## Types\n\n```go\ntype FetchFunc func(ctx context.Context, pageSize, page int) (total int, list interface{}, err error)\n```\nFetchFunc fetch a page of data The return value \\`list\\` must be a slice or panic will occur\n\n```go\ntype PageRequestFunc struct {\n\t// RequestPage for concurrent request paging\n\tRequestPage func(ctx context.Context, pageSize int, page int) (interface{}, error)\n\t// PageResult for get paging information\n\tPageResult func(items interface{}) (total int, currentPageLen int, err error)\n}\n```\nPageRequestFunc is a tool for concurrent processing of pagination\n\n```go\ntype ParallelOptions struct {\n\tFailFast         bool\n\tConcurrencyCount int\n}\n```\n\n```go\ntype ParallelTasks struct {\n\tOptions ParallelOptions\n\n\tLog *zap.SugaredLogger\n\t// contains filtered or unexported fields\n}\n```\nParallelTasks will construct a parallel tasks struct you could execute tasks in parallel eg. result, err :=  P(\"eg1\", f1,f2, f3).Do().Wait() result, err :=  P(\"eg2\", f1,f2, f3).Add(f4).FailFast().Do().Wait()\n\n\tresult, err :=  P(\"eg3\", f1,f2, f3).Context(func()context.Context{\n\t\t\tctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond) // 0.5s will timeout\n\t\t\treturn ctx\n\t}).Do().Wait()\n\n```go\nfunc P(log *zap.SugaredLogger, name string, tasks ...Task) *ParallelTasks\n```\nP will construct ParallelTasks name will be used for log you must care about the variable that referenced by Closure\n\n```go\nfunc (p *ParallelTasks) Add(tasks ...Task) *ParallelTasks\n```\n\n```go\nfunc (p *ParallelTasks) Cancel(cancelReason error)\n```\n\n```go\nfunc (p *ParallelTasks) Context(ctx context.Context) *ParallelTasks\n```\nContext will set context , up to now , task is not support to cancel if you cancel from context, wait will return immediately\n\n```go\nfunc (p *ParallelTasks) Do() *ParallelTasks\n```\nDo will start to execute all task in parallel\n\n```go\nfunc (p *ParallelTasks) FailFast() *ParallelTasks\n```\n\n```go\nfunc (p *ParallelTasks) Name(name string) *ParallelTasks\n```\n\n```go\nfunc (p *ParallelTasks) SetConcurrent(count int) *ParallelTasks\n```\nSetConcurrent set the number of concurrency\n\n```go\nfunc (p *ParallelTasks) SetMaxConcurrent(count int, max int) *ParallelTasks\n```\nSetMaxConcurrent set the number of concurrency. if count is greater than max, max is used.\n\n```go\nfunc (p *ParallelTasks) Wait() ([]interface{}, error)\n```\nWait will wait all task executed, if set fail fast , it will return immediately if any task returns errors up to now , task is not support to cancel you should invoke Do() before invoke Wait() the result of task will be saved in \\[]interface{} if you set failfast and one errors happened, it will return one error if you not set failfase and any errors happened, it will return \\[]error as MultiErrors\n\n```go\ntype Task func() (interface{}, error)\n```\n\n","path":"github.com/AlaudaDevops/pkg/parallel","name":"parallel","synopsis":"Package parallel used to execute tasks in parallel","isRedistributable":true}
