From ae180510c0fff3ae11ecc03678e1557311bb28bb Mon Sep 17 00:00:00 2001 From: Lucas Rodriguez Date: Wed, 15 May 2024 14:57:07 -0500 Subject: [PATCH] chore: remove concurrency tools from helpers (#86) ## Description Zarf was the only user of these tools, but we have since refactored our code to use `errgroups` rather than `ConcurrencyTools` https://github.com/defenseunicorns/zarf/pull/2460/files#diff-efb99da1ff0c60478a053ac9f6acfa8eba6237adc87cd430cdbf3b5a32d1b16c ## Related Issue Fixes #70 --- helpers/concurrency.go | 85 ------------------------------------------ helpers/files.go | 1 + 2 files changed, 1 insertion(+), 85 deletions(-) delete mode 100644 helpers/concurrency.go diff --git a/helpers/concurrency.go b/helpers/concurrency.go deleted file mode 100644 index 081752d2..00000000 --- a/helpers/concurrency.go +++ /dev/null @@ -1,85 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2024-Present Defense Unicorns - -// Package helpers provides generic helper functions -package helpers - -import ( - "context" -) - -// ConcurrencyTools is a struct that contains channels and a context for use in concurrent routines -type ConcurrencyTools[P any, E any] struct { - ProgressChan chan P - ErrorChan chan E - context context.Context - Cancel context.CancelFunc - routineCount int -} - -// NewConcurrencyTools creates a new ConcurrencyTools struct -// -// Length is the number of iterations that will be performed concurrently -func NewConcurrencyTools[P any, E any](length int) *ConcurrencyTools[P, E] { - ctx, cancel := context.WithCancel(context.TODO()) - - progressChan := make(chan P, length) - - errorChan := make(chan E, length) - - concurrencyTools := ConcurrencyTools[P, E]{ - ProgressChan: progressChan, - ErrorChan: errorChan, - context: ctx, - Cancel: cancel, - routineCount: length, - } - - return &concurrencyTools -} - -// IsDone returns true if the context is done. -func (ct *ConcurrencyTools[P, E]) IsDone() bool { - ctx := ct.context - select { - case <-ctx.Done(): - return true - default: - return false - } -} - -// WaitWithProgress waits for all routines to finish -// -// onProgress is a callback function that is called when a routine sends a progress update -// -// onError is a callback function that is called when a routine sends an error -func (ct *ConcurrencyTools[P, E]) WaitWithProgress(onProgress func(P, int), onError func(E) error) error { - for i := 0; i < ct.routineCount; i++ { - select { - case err := <-ct.ErrorChan: - ct.Cancel() - errResult := onError(err) - return errResult - case progress := <-ct.ProgressChan: - onProgress(progress, i) - } - } - return nil -} - -// WaitWithoutProgress waits for all routines to finish without a progress callback -// -// onError is a callback function that is called when a routine sends an error -func (ct *ConcurrencyTools[P, E]) WaitWithoutProgress(onError func(E) error) error { - for i := 0; i < ct.routineCount; i++ { - select { - case err := <-ct.ErrorChan: - ct.Cancel() - errResult := onError(err) - return errResult - case <-ct.ProgressChan: - } - } - return nil -} diff --git a/helpers/files.go b/helpers/files.go index 7e2f97d2..39f49d33 100644 --- a/helpers/files.go +++ b/helpers/files.go @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2024-Present Defense Unicorns +// Package helpers provides common helper functions for Go. package helpers import (