workpool

package module
v0.0.0-...-b6c2fa5 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 3 Imported by: 81

README

workpool

Import this package via code.cloudfoundry.org/workpool.

Use a WorkPool to perform units of work concurrently at a maximum rate. The worker goroutines will increase to the maximum number of workers as work requires it, and gradually decrease to 0 if unused.

A Throttler performs a specified batch of work at a given maximum rate, internally creating a WorkPool and then stopping it when done.

Reporting issues and requesting features

Please report all issues and feature requests in cloudfoundry/diego-release.

Example

type RateLimitingHandler struct {
	backend http.Handler
	pool    *workpool.WorkPool
}

// Ensures that the backend handler never processes more than maxInFlight requests at a time
func NewRateLimitingHandler(backend http.Handler, maxInFlight int) (*RateLimitingHandler, error) {
	pool, err := workpool.NewWorkPool(maxInFlight)
	if err != nil {
		return nil, err
	}

	return &RateLimitingHandler{
		backend: backend,
		pool:    pool,
	}, nil
}

func (rh *RateLimitingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	rh.pool.Submit(func() {
		rh.backend.ServeHTTP(w, req)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Throttler

type Throttler struct {
	// contains filtered or unexported fields
}

func NewThrottler

func NewThrottler(maxWorkers int, works []func()) (*Throttler, error)

func (*Throttler) Work

func (t *Throttler) Work()

type WorkPool

type WorkPool struct {
	// contains filtered or unexported fields
}

func NewWorkPool

func NewWorkPool(maxWorkers int) (*WorkPool, error)

func (*WorkPool) Stop

func (w *WorkPool) Stop()

func (*WorkPool) Submit

func (w *WorkPool) Submit(work func())

Jump to

Keyboard shortcuts

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