Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new throttler pkg forked from nozzle/throttler #108

Merged
merged 26 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
557c7f2
Initial commit
puerco Mar 7, 2015
ae4180e
Initial commit
derekperkins Mar 7, 2015
79d9eec
Changed throttler.NewThrottler to throttler.New
derekperkins Mar 7, 2015
b008968
Added Coveralls and Codeship integration & badges
derekperkins Mar 7, 2015
da4f04a
Replaced doneChan chan bool with chan struct{}
derekperkins Mar 8, 2015
7d4550d
.Done() now requires an error / nil to be passed into it Added an err…
derekperkins Mar 12, 2015
a5f85e2
Clarified documentation
derekperkins Mar 12, 2015
f66ecdb
Added `Errs` method with multiError handling
puerco Mar 13, 2015
e094420
Fixed example_test.go
puerco Mar 13, 2015
375dc68
Testing tweaks
derekperkins Apr 22, 2015
f0cba5e
Added Batching Feature
puerco May 12, 2015
41f0bd0
add circleci with test coverage
derekperkins Oct 31, 2017
dafebc5
add codecov.io upload
derekperkins Oct 31, 2017
7db6856
Fix data race in Done()
derekperkins Aug 15, 2018
5168f2d
add comments to fix golint errors
derekperkins Aug 16, 2018
a59174a
fix test to call t.Fatal in main test goroutine
derekperkins Aug 16, 2018
5c66484
use atomic.Int32 to eliminate race conditions
derekperkins Aug 16, 2018
7043ed5
add support for SetMaxWorkers
derekperkins Aug 16, 2018
466a4a2
update readme
derekperkins Aug 17, 2018
6e0fe9c
Move throttler to package subdir
puerco Jul 23, 2024
f173561
Remove redundant files from nozzle/throtle import
puerco Jul 23, 2024
ff6ddb6
Add notices, credit notices
puerco Jul 23, 2024
2d65bad
Fix linter for new throttle module
puerco Jul 23, 2024
330b853
Rename multiError type to multiErrors
puerco Jul 23, 2024
fd37b22
Silence linter for rand/v2
puerco Jul 23, 2024
50ded5a
Move files to ./throttle
puerco Jul 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions throttler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Throttler - Intelligent WaitGroups

[![GoDoc](https://pkg.go.dev/sigs.k8s.io/release-utils/throttler?status.svg)](https://pkg.go.dev/sigs.k8s.io/release-utils/throttler?status.svg)

__Note:__ This package was adopted by the Kubernetes RelEng team to continue its
maintenance, it was forked from github.com/nozzle/throttle at
[2ea9822](https://github.com/nozzle/throttler/commit/2ea982251481626167b7f83be1434b5c42540c1a).

Throttler fills the gap between sync.WaitGroup and manually monitoring your
goroutines with channels. The API is almost identical to Wait Groups, but it
allows you to set a max number of workers that can be running simultaneously.
It uses channels internally to block until a job completes by calling Done() or
until all jobs have been completed. It also provides a built in error channel
that captures your goroutine errors and provides access to them as `[]error`
after you exit the loop.

See a fully functional example of the original module on the playground at http://bit.ly/throttler-v3

Compare the Throttler example to the sync.WaitGroup example from http://golang.org/pkg/sync/#example_WaitGroup

### How to use Throttler

```golang
// This example fetches several URLs concurrently,
// using a Throttler to block until all the fetches are complete.
// Compare to http://golang.org/pkg/sync/#example_WaitGroup
func ExampleThrottler() {
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
// Create a new Throttler that will get 2 urls at a time
t := throttler.New(2, len(urls))
for _, url := range urls {
// Launch a goroutine to fetch the URL.
go func(url string) {
// Fetch the URL.
err := http.Get(url)
// Let Throttler know when the goroutine completes
// so it can dispatch another worker
t.Done(err)
}(url)
// Pauses until a worker is available or all jobs have been completed
// Returning the total number of goroutines that have errored
// lets you choose to break out of the loop without starting any more
errorCount := t.Throttle()
}
}
```

### vs How to use a sync.WaitGroup

```golang
// This example fetches several URLs concurrently,
// using a WaitGroup to block until all the fetches are complete.
func ExampleWaitGroup() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}
```
108 changes: 108 additions & 0 deletions throttler/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This package was forked and adapted from the original at
// pkg:golang/github.com/nozzle/throttler@2ea982251481626167b7f83be1434b5c42540c1a
// full commit history has been preserved.

package throttler

import (
"fmt"
"os"
)

type httpPkg struct{}

func (httpPkg) Get(_ string) error { return nil }

var http httpPkg

// This example fetches several URLs concurrently,
// using a WaitGroup to block until all the fetches are complete.
//
//nolint:testableexamples // TODO - Rewrite examples
func ExampleWaitGroup() {
}

// This example fetches several URLs concurrently,
// using a Throttler to block until all the fetches are complete.
// Compare to http://golang.org/pkg/sync/#example_WaitGroup
//
//nolint:testableexamples // TODO - Rewrite examples
func ExampleThrottler() {
urls := []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
// Create a new Throttler that will get 2 urls at a time
t := New(2, len(urls))
for _, url := range urls {
// Launch a goroutine to fetch the URL.
go func(url string) {
// Fetch the URL.
err := http.Get(url)
// Let Throttler know when the goroutine completes
// so it can dispatch another worker
t.Done(err)
}(url)
// Pauses until a worker is available or all jobs have been completed
// Returning the total number of goroutines that have errored
// lets you choose to break out of the loop without starting any more
errorCount := t.Throttle()
if errorCount > 0 {
break
}
}
}

// This example fetches several URLs concurrently,
// using a Throttler to block until all the fetches are complete
// and checks the errors returned.
// Compare to http://golang.org/pkg/sync/#example_WaitGroup
//
//nolint:testableexamples // TODO - Rewrite examples
func ExampleThrottler_errors() {
urls := []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
// Create a new Throttler that will get 2 urls at a time
t := New(2, len(urls))
for _, url := range urls {
// Launch a goroutine to fetch the URL.
go func(url string) {
// Let Throttler know when the goroutine completes
// so it can dispatch another worker
defer t.Done(nil)
// Fetch the URL.
if err := http.Get(url); err != nil {
fmt.Fprintf(os.Stderr, "error fetching %q: %v", url, err)
}
}(url)
// Pauses until a worker is available or all jobs have been completed
t.Throttle()
}

if t.Err() != nil {
// Loop through the errors to see the details
for i, err := range t.Errs() {
fmt.Printf("error #%d: %s", i, err)
}
}
}
190 changes: 190 additions & 0 deletions throttler/throttler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// This package was forked and adapted from the original at
// pkg:golang/github.com/nozzle/throttler@2ea982251481626167b7f83be1434b5c42540c1a
// full commit history has been preserved.

// Package throttler fills the gap between sync.WaitGroup and manually monitoring your goroutines
// with channels. The API is almost identical to Wait Groups, but it allows you to set
// a max number of workers that can be running simultaneously. It uses channels internally
// to block until a job completes by calling Done(err) or until all jobs have been completed.
//
// After exiting the loop where you are using Throttler, you can call the `Err` or `Errs` method to check
// for errors. `Err` will return a single error representative of all the errors Throttler caught. The
// `Errs` method will return all the errors as a slice of errors (`[]error`).
//
// Compare the Throttler example to the sync.WaitGroup example http://golang.org/pkg/sync/#example_WaitGroup
//
// This package was forked and adapted from the original at
// pkg:golang/github.com/nozzle/throttler@2ea982251481626167b7f83be1434b5c42540c1a
// full commit history has been preserved.
//
// See a fully functional example of the original package on the playground at http://bit.ly/throttler-v3
package throttler

import (
"fmt"
"math"
"sync"
"sync/atomic"
)

// Throttler stores all the information about the number of workers, the active
// workers and error information.
type Throttler struct {
maxWorkers int32
workerCount int32
batchingTotal int32
batchSize int32
totalJobs int32
jobsStarted int32
jobsCompleted int32
doneChan chan struct{}
errsMutex *sync.Mutex
errs []error
errorCount int32
}

// New returns a Throttler that will govern the max number of workers and will
// work with the total number of jobs. It panics if maxWorkers < 1.
func New(maxWorkers, totalJobs int) *Throttler {
if maxWorkers < 1 {
panic("maxWorkers has to be at least 1")
}
return &Throttler{
maxWorkers: int32(maxWorkers),
batchSize: 1,
totalJobs: int32(totalJobs),
doneChan: make(chan struct{}, totalJobs),
errsMutex: &sync.Mutex{},
}
}

// NewBatchedThrottler returns a Throttler (just like New), but also enables batching.
func NewBatchedThrottler(maxWorkers, batchingTotal, batchSize int) *Throttler {
totalJobs := int(math.Ceil(float64(batchingTotal) / float64(batchSize)))
t := New(maxWorkers, totalJobs)
t.batchSize = int32(batchSize)
t.batchingTotal = int32(batchingTotal)
return t
}

// SetMaxWorkers lets you change the total number of workers that can run concurrently. NOTE: If
// all workers are currently running, this setting is not guaranteed to take effect until one of them
// completes and Throttle() is called again.
func (t *Throttler) SetMaxWorkers(maxWorkers int) {
if maxWorkers < 1 {
panic("maxWorkers has to be at least 1")
}
atomic.StoreInt32(&t.maxWorkers, int32(maxWorkers))
}

// Throttle works similarly to sync.WaitGroup, except inside your goroutine dispatch
// loop rather than after. It will not block until the number of active workers
// matches the max number of workers designated in the call to NewThrottler or
// all of the jobs have been dispatched. It stops blocking when Done has been called
// as many times as totalJobs.
func (t *Throttler) Throttle() int {
if atomic.LoadInt32(&t.totalJobs) < 1 {
return int(atomic.LoadInt32(&t.errorCount))
}
atomic.AddInt32(&t.jobsStarted, 1)
atomic.AddInt32(&t.workerCount, 1)

// check to see if the current number of workers equals the max number of workers
// if they are equal, wait for one to finish before continuing.
if atomic.LoadInt32(&t.workerCount) == atomic.LoadInt32(&t.maxWorkers) {
atomic.AddInt32(&t.jobsCompleted, 1)
atomic.AddInt32(&t.workerCount, -1)
<-t.doneChan
}

// check to see if all of the jobs have been started, and if so, wait until all
// jobs have been completed before continuing.
if atomic.LoadInt32(&t.jobsStarted) == atomic.LoadInt32(&t.totalJobs) {
for atomic.LoadInt32(&t.jobsCompleted) < atomic.LoadInt32(&t.totalJobs) {
atomic.AddInt32(&t.jobsCompleted, 1)
<-t.doneChan
}
}

return int(atomic.LoadInt32(&t.errorCount))
}

// Done lets Throttler know that a job has been completed so that another worker
// can be activated. If Done is called less times than totalJobs,
// Throttle will block forever.
func (t *Throttler) Done(err error) {
if err != nil {
t.errsMutex.Lock()
t.errs = append(t.errs, err)
atomic.AddInt32(&t.errorCount, 1)
t.errsMutex.Unlock()
}
t.doneChan <- struct{}{}
}

// Err returns an error representative of all errors caught by throttler.
func (t *Throttler) Err() error {
t.errsMutex.Lock()
defer t.errsMutex.Unlock()
if atomic.LoadInt32(&t.errorCount) == 0 {
return nil
}
return multiErrors(t.errs)
}

// Errs returns a slice of any errors that were received from calling Done().
func (t *Throttler) Errs() []error {
t.errsMutex.Lock()
defer t.errsMutex.Unlock()
return t.errs
}

type multiErrors []error

func (te multiErrors) Error() string {
errString := te[0].Error()
if len(te) > 1 {
errString += fmt.Sprintf(" (and %d more errors)", len(te)-1)
}
return errString
}

// BatchStartIndex returns the starting index for the next batch. The job count isn't modified
// until th.Throttle() is called, so if you don't call Throttle before executing this
// again, it will return the same index as before.
func (t *Throttler) BatchStartIndex() int {
return int(atomic.LoadInt32(&t.jobsStarted) * atomic.LoadInt32(&t.batchSize))
}

// BatchEndIndex returns the ending index for the next batch. It either returns the full batch size
// or the remaining amount of jobs. The job count isn't modified
// until th.Throttle() is called, so if you don't call Throttle before executing this
// again, it will return the same index as before.
func (t *Throttler) BatchEndIndex() int {
end := (atomic.LoadInt32(&t.jobsStarted) + 1) * atomic.LoadInt32(&t.batchSize)
if end > atomic.LoadInt32(&t.batchingTotal) {
end = atomic.LoadInt32(&t.batchingTotal)
}
return int(end)
}

// TotalJobs returns the total number of jobs throttler is performing.
func (t *Throttler) TotalJobs() int {
return int(atomic.LoadInt32(&t.totalJobs))
}
Loading