sizedwaitgroup

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2018 License: MIT Imports: 3 Imported by: 0

README

SizedWaitGroup

GoDoc

SizedWaitGroup has the same role and API as sync.WaitGroup but it adds a limit of the amount of goroutines started concurrently.

SizedWaitGroup adds the feature of limiting the maximum number of concurrently started routines. It could for example be used to start multiples routines querying a database but without sending too much queries in order to not overload the given database.

Example

package main

import (
        "fmt"
        "math/rand"
        "time"

        "github.com/remeh/sizedwaitgroup"
)

func main() {
        rand.Seed(time.Now().UnixNano())

        // Typical use-case:
        // 50 queries must be executed as quick as possible
        // but without overloading the database, so only
        // 8 routines should be started concurrently.
        swg := sizedwaitgroup.New(8)
        for i := 0; i < 50; i++ {
                swg.Add()
                go func(i int) {
                        defer swg.Done()
                        query(i)
                }(i)
        }

        swg.Wait()
}

func query(i int) {
        fmt.Println(i)
        ms := i + 500 + rand.Intn(500)
        time.Sleep(time.Duration(ms) * time.Millisecond)
}

License

MIT

Rémy Mathieu © 2016

Documentation

Overview

SizedWaitGroup adds the feature of limiting the maximum number of concurrently started routines. It could for example be used to start multiples routines querying a database but without sending too much queries in order to not overload the given database.

Rémy Mathieu © 2016

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SizedWaitGroup

type SizedWaitGroup struct {
	Size int
	// contains filtered or unexported fields
}

SizedWaitGroup has the same role and close to the same API as the Golang sync.WaitGroup but adds a limit of the amount of goroutines started concurrently.

func New

func New(limit int) SizedWaitGroup

New creates a SizedWaitGroup. The limit parameter is the maximum amount of goroutines which can be started concurrently.

func (*SizedWaitGroup) Add

func (s *SizedWaitGroup) Add()

Add increments the internal WaitGroup counter. It can be blocking if the limit of spawned goroutines has been reached. It will stop blocking when Done is been called.

See sync.WaitGroup documentation for more information.

func (*SizedWaitGroup) AddWithContext

func (s *SizedWaitGroup) AddWithContext(ctx context.Context) error

AddWithContext increments the internal WaitGroup counter. It can be blocking if the limit of spawned goroutines has been reached. It will stop blocking when Done is been called, or when the context is canceled. Returns nil on success or an error if the context is canceled before the lock is acquired.

See sync.WaitGroup documentation for more information.

func (*SizedWaitGroup) Done

func (s *SizedWaitGroup) Done()

Done decrements the SizedWaitGroup counter. See sync.WaitGroup documentation for more information.

func (*SizedWaitGroup) Wait

func (s *SizedWaitGroup) Wait()

Wait blocks until the SizedWaitGroup counter is zero. See sync.WaitGroup documentation for more information.

Jump to

Keyboard shortcuts

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