Skip to content

Commit

Permalink
zero based
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Apr 28, 2024
1 parent 83fd398 commit 06e9b1b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sync/adaptivewaitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type AdaptiveWaitGroup struct {
}

func WithSize(size int) AdaptiveGroupOption {
// size is 0 based
size = zeroBased(size)
return func(wg *AdaptiveWaitGroup) error {
if size < 0 {
return errors.New("size must be positive")
Expand Down Expand Up @@ -85,6 +87,9 @@ func (s *AdaptiveWaitGroup) Resize(ctx context.Context, size int) error {
s.mu.Lock()
defer s.mu.Unlock()

// size is 0 based
size = zeroBased(size)

// Resize the semaphore with the provided context and handle any errors
if err := s.sem.Resize(ctx, int64(size)); err != nil {
return err
Expand All @@ -96,3 +101,10 @@ func (s *AdaptiveWaitGroup) Resize(ctx context.Context, size int) error {
func (s *AdaptiveWaitGroup) Current() int {
return int(s.current.Load())
}

func zeroBased(size int) int {
if size > 1 {
size = size - 1
}
return size
}

0 comments on commit 06e9b1b

Please sign in to comment.