sizedwaitgroup

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

  • Owner: remeh/sizedwaitgroup
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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

Copyright

Rémy Mathieu © 2016

Main metrics

Overview
Name With Ownerremeh/sizedwaitgroup
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2016-11-19 13:49:25
Pushed At2022-03-17 13:12:43
Last Commit At2022-03-17 14:12:43
Release Count1
Last Release Namev1.0.0 (Posted on )
First Release Namev1.0.0 (Posted on )
用户参与
Stargazers Count426
Watchers Count6
Fork Count46
Commits Count21
Has Issues Enabled
Issues Count6
Issue Open Count4
Pull Requests Count4
Pull Requests Open Count2
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private