Help us shape the future of Go! Take the 2026 Go Developer Survey

throttle

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 2 Imported by: 0

README

Throttle

A golang package that gives you the ability to throttle concurrent functions around a limited resource.

Package Usage


import (
  "fmt"
  "time"

  "github.com/prophittcorey/throttle"
)

func main() {
	executor := throttle.New(runtime.NumCPU())

	start := time.Now()

	for i := 1; i < 100; i++ {
		executor.Run(func() {
			time.Sleep(1 * time.Second)
		})
	}

	executor.Wait()

	fmt.Printf("Finished in %s\n", time.Since(start))
}

License

The source code for this repository is licensed under the MIT license, which you can find in the LICENSE file.

Documentation

Overview

Package throttle implements a data structure to help limit concurrency around a fixed resource limit without excessive blocking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor

type Executor struct {
	sync.WaitGroup
	// contains filtered or unexported fields
}

Executor is the monitor for executing functions. The executor will ensure only a fixed number of functions are fun concurrently and will block as needed.

func New

func New(resourcelimit int) *Executor

New creates a new Executor that is ready to be used.

func (*Executor) Run

func (q *Executor) Run(f func())

Run executes a function and ensures concurrency is throttled.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL