limiter

package module
v0.0.0-...-3d4b2b3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 3 Imported by: 29

README

go lang goroutine concurrency limiter

builds

Build Status

Example

limit the number of concurrent go routines to 10:

  import "github.com/korovkin/limiter"

  ...

  limit := limiter.NewConcurrencyLimiter(10)
  defer limit.WaitAndClose()

  for i := 0; i < 1000; i++ {
    limit.Execute(func() {
      // do some work
    })
  }

Real World Example:

  import "github.com/korovkin/limiter"

  ...

  limiter := limiter.NewConcurrencyLimiter(10)

  httpGoogle := int(0)
  limiter.Execute(func() {
    resp, err := http.Get("https://www.google.com/")
    Expect(err).To(BeNil())
    defer resp.Body.Close()
    httpGoogle = resp.StatusCode
  })

  httpApple := int(0)
  limiter.Execute(func() {
    resp, err := http.Get("https://www.apple.com/")
    Expect(err).To(BeNil())
    defer resp.Body.Close()
    httpApple = resp.StatusCode
  })

  limiter.WaitAndClose()

  log.Println("httpGoogle:", httpGoogle)
  log.Println("httpApple:", httpApple)

Concurrent IO with Error tracking:

  import "github.com/korovkin/limiter"
  ...
	a := errors.New("error a")
	b := errors.New("error b")

	concurrently := limiter.NewConcurrencyLimiterForIO(limiter.DefaultConcurrencyLimitIO)
	concurrently.Execute(func() {
		// Do some really slow IO ...
		// keep the error:
		concurrently.FirstErrorStore(a)
	})
	concurrently.Execute(func() {
		// Do some really slow IO ...
		// keep the error:
		concurrently.FirstErrorStore(b)
	})
	concurrently.WaitAndClose()

	firstErr := concurrently.FirstErrorGet()
	Expect(firstErr == a || firstErr == b).To(BeTrue())

Documentation

Index

Constants

View Source
const (
	DefaultConcurrencyLimitIO = 4
)
View Source
const (
	// DefaultLimit is the default concurrency limit
	DefaultLimit = 100
)

Variables

View Source
var (
	// appending a callback to a closed limiter
	ErrorClosed          = errors.New("limiter closed")
	ErrorSubroutinePanic = errors.New("goroutine panic")
)

Functions

This section is empty.

Types

type ConcurrencyLimiter

type ConcurrencyLimiter struct {
	RecoverPanics bool // recover from panics in the subroutines (keeping the process running)
	// contains filtered or unexported fields
}

ConcurrencyLimiter object

func NewConcurrencyLimiter

func NewConcurrencyLimiter(limit int) *ConcurrencyLimiter

NewConcurrencyLimiter allocates a new ConcurrencyLimiter

func (*ConcurrencyLimiter) Execute

func (c *ConcurrencyLimiter) Execute(job func()) (int, error)

Execute adds a function to the execution queue. if num of go routines allocated by this instance is < limit launch a new go routine to execute job else wait until a go routine becomes available

func (*ConcurrencyLimiter) ExecuteWithTicket

func (c *ConcurrencyLimiter) ExecuteWithTicket(job func(ticket int)) (int, error)

ExecuteWithTicket adds a job into an execution queue and returns a ticket id. if num of go routines allocated by this instance is < limit launch a new go routine to execute job else wait until a go routine becomes available

func (*ConcurrencyLimiter) GetNumInProgress

func (c *ConcurrencyLimiter) GetNumInProgress() int32

GetNumInProgress returns a (racy) counter of how many go routines are active right now

func (*ConcurrencyLimiter) WaitAndClose

func (c *ConcurrencyLimiter) WaitAndClose() error

WaitAndClose will block until all the previously Executed jobs completed running. New tasks won't be allow

IMPORTANT: calling the Wait function while keep calling Execute leads to

un-desired race conditions

type Concurrently

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

Concurrently - execute tasks (IO) concurrently, keep track of the first error atomically

func NewConcurrencyLimiterForIO

func NewConcurrencyLimiterForIO(limit int) *Concurrently

func (*Concurrently) Execute

func (c *Concurrently) Execute(job func()) (int, error)

func (*Concurrently) FirstErrorGet

func (c *Concurrently) FirstErrorGet() error

func (*Concurrently) FirstErrorStore

func (c *Concurrently) FirstErrorStore(e error) (bool, error)

func (*Concurrently) WaitAndClose

func (c *Concurrently) WaitAndClose() error

Jump to

Keyboard shortcuts

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