Skip to content

Commit

Permalink
Merge pull request #364 from projectdiscovery/feat-adapativewaitgroup
Browse files Browse the repository at this point in the history
Adding Adaptive WaitGroup
  • Loading branch information
Mzack9999 authored Mar 11, 2024
2 parents 21f8a55 + 1b8caeb commit 6dfdc53
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/cespare/xxhash v1.1.0
github.com/charmbracelet/glamour v0.6.0
github.com/eapache/channels v1.1.0
github.com/google/go-github/v30 v30.1.0
github.com/hdm/jarm-go v0.0.7
github.com/julienschmidt/httprouter v1.3.0
Expand Down Expand Up @@ -47,6 +48,7 @@ require (
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/gaukas/godicttls v0.0.4 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/eapache/channels v1.1.0 h1:F1taHcn7/F0i8DYqKXJnyhJcVpp2kgFcNePxXtnyu4k=
github.com/eapache/channels v1.1.0/go.mod h1:jMm2qB5Ubtg9zLd+inMZd2/NUvXgzmWXsDaLyQIGfH0=
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/ebitengine/purego v0.4.0 h1:RQVuMIxQPQ5iCGEJvjQ17YOK+1tMKjVau2FUMvXH4HE=
github.com/ebitengine/purego v0.4.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
Expand Down
72 changes: 72 additions & 0 deletions sync/adaptivewaitgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sync

// Extended version of https://github.com/remeh/sizedwaitgroup

import (
"context"
"errors"
"sync"

"github.com/eapache/channels"
)

type AdaptiveGroupOption func(*AdaptiveWaitGroup) error

type AdaptiveWaitGroup struct {
Size int

current *channels.ResizableChannel
wg sync.WaitGroup
}

func WithSize(size int) AdaptiveGroupOption {
return func(wg *AdaptiveWaitGroup) error {
if size < 0 {
return errors.New("size must be positive")
}
wg.Size = size
return nil
}
}

func New(options ...AdaptiveGroupOption) (*AdaptiveWaitGroup, error) {
wg := &AdaptiveWaitGroup{}
for _, option := range options {
if err := option(wg); err != nil {
return nil, err
}
}

wg.current = channels.NewResizableChannel()
wg.current.Resize(channels.BufferCap(wg.Size))
wg.wg = sync.WaitGroup{}
return wg, nil
}

func (s *AdaptiveWaitGroup) Add() {
_ = s.AddWithContext(context.Background())
}

func (s *AdaptiveWaitGroup) AddWithContext(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case s.current.In() <- struct{}{}:
break
}
s.wg.Add(1)
return nil
}

func (s *AdaptiveWaitGroup) Done() {
<-s.current.Out()
s.wg.Done()
}

func (s *AdaptiveWaitGroup) Wait() {
s.wg.Wait()
}

func (s *AdaptiveWaitGroup) Resize(size int) {
s.current.Resize(channels.BufferCap(size))
}

0 comments on commit 6dfdc53

Please sign in to comment.