Skip to content

Commit

Permalink
add options for GenericParallelContainers function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Stepanov committed Jun 15, 2022
1 parent 2011543 commit 2b6fba1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
)

const (
MaxGenericWorkers = 8
defaultWorkersCount = 8
)

type GenericParallelErrors struct {
Errors []GenericContainerRequestError
}

func (gpe GenericParallelErrors) Error() string {
return fmt.Sprintf("%v", gpe.Errors)
// GenericParallelOptions represents additional options for running
// * WorkersCount - count of parallel workers. if field empty(zero), default value will be 'defaultWorkersCount'
type GenericParallelOptions struct {
WorkersCount int
}

// GenericContainerRequestError represents error from parallel request
Expand All @@ -24,6 +22,14 @@ type GenericContainerRequestError struct {
Error error
}

type GenericParallelErrors struct {
Errors []GenericContainerRequestError
}

func (gpe GenericParallelErrors) Error() string {
return fmt.Sprintf("%v", gpe.Errors)
}

func genericContainerRunner(
ctx context.Context,
requests <-chan GenericContainerRequest,
Expand All @@ -45,8 +51,12 @@ func genericContainerRunner(
}

// GenericParallelContainers creates a generic containers with parameters in parallel mode
func GenericParallelContainers(ctx context.Context, reqs []GenericContainerRequest) ([]Container, error) {
tasksChanSize := MaxGenericWorkers
func GenericParallelContainers(ctx context.Context, reqs []GenericContainerRequest, opt GenericParallelOptions) ([]Container, error) {
if opt.WorkersCount == 0 {
opt.WorkersCount = defaultWorkersCount
}

tasksChanSize := opt.WorkersCount
if tasksChanSize > len(reqs) {
tasksChanSize = len(reqs)
}
Expand Down
2 changes: 1 addition & 1 deletion parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGenericParallelContainers(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res, err := GenericParallelContainers(context.Background(), tc.reqs)
res, err := GenericParallelContainers(context.Background(), tc.reqs, GenericParallelOptions{})

if err != nil && tc.expErrors > 0 {
e, _ := err.(GenericParallelErrors)
Expand Down

0 comments on commit 2b6fba1

Please sign in to comment.