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.

Overview

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

You might also like...
A small library to detect if an IP address is close to yours

go-geofence A small library to detect if an IP address is close to yours or another of your choosing using https://ipstack.com/ Usage First you will n

A close implementation of JavaScript's Set written in Go

Set The Set struct allows unique values storing of any type. Initializing a Set s := set.New() // Empty Set s := set.New("hi", 45, Person{name: "Gophe

There is a certain amount of work to be done before you can implement the features of your Go powered CLI app

go-project-template-cli There is a certain amount of work to be done before you can implement the features of your Go powered CLI app. A few of those

iflandown runs commands after the wired LAN link is down for a given amount of time.

iflandown iflandown runs commands after the wired LAN link is down for a given amount of time. All/most linux devices should work. Tested on amd64, Ra

This is a close to decentralized RSS3 Network implementation of RSS3 protocol v0.4.0 with full indexing function in Go
This is a close to decentralized RSS3 Network implementation of RSS3 protocol v0.4.0 with full indexing function in Go

This is a close to decentralized RSS3 Network implementation of RSS3 protocol v0.4.0 with full indexing function in Go

grafana-sync Keep your grafana dashboards in sync.

grafana-sync Keep your grafana dashboards in sync. Table of Contents grafana-sync Table of Contents Installing Getting Started Pull Save all dashboard

Simple boilerplate code to get started with building and deploying a serverless CRUD API

Simple boilerplate code to get started with building and deploying a serverless CRUD API with Go, MongoDB and Netlify

Template project to get started with a simple API skeleton in Go and Docker

A template project to create a Docker image for a Go application. The example application exposes an HTTP endpoint through a REST API packaged as a static binary.

Same as fmt.Errorf but with stack trace.

Annotation with stack trace for go1.13 Go 1.13 contains support for error wrapping. Now you can add additional information to an error by wrapping it

x/sync/singleflight but with Go 1.18 generics

singleflight This repo is a hard fork of golang.org/x/sync/singleflight that adds generics to the Group type so that there is no need for type asserti

InkCaller is an API to call Ink. Each new call is independent and can be executing concurrently

inkcaller This library InkCaller is an API to call Ink. Each new call is independent and can be executing concurrently. A call will force the ink stat

goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang.

goRBAC goRBAC provides a lightweight role-based access control implementation in Golang. For the purposes of this package: * an identity has one or mo

Go Getting Started from PluralSight. Simple webapp in golang

Go_WEB_APP Go Getting Started from PluralSight. Simple webapp in golang This is simple Webapp in GO, which has users api to interact with backend This

Training materials and labs for a "Getting Started" level course on COBOL

COBOL Programming Course This project is a set of training materials and labs for COBOL on z/OS. The following books are available within this reposit

vcluster - Create fully functional virtual Kubernetes clusters - Each cluster runs inside a Kubernetes namespace and can be started within seconds
vcluster - Create fully functional virtual Kubernetes clusters - Each cluster runs inside a Kubernetes namespace and can be started within seconds

Website • Quickstart • Documentation • Blog • Twitter • Slack vcluster - Virtual Clusters For Kubernetes Lightweight & Low-Overhead - Based on k3s, bu

Terraform ACI Provider. Started before Cisco's release and stopped when Cisco released its own.

terraform-provider-aci This was before Cisco released their own (I did ask them if they were doing one and they said no !!) :-) I would have liked to

🔑 Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role.
🔑 Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role.

Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role. URLs and Roles are managed as YAML-based

At LinkedIn, we are using this curriculum for onboarding our entry-level talents into the SRE role.
At LinkedIn, we are using this curriculum for onboarding our entry-level talents into the SRE role.

School of SRE In early 2019, we started visiting campuses across India to recruit the best and brightest minds to ensure LinkedIn, and all the service

Role Based Access Control (RBAC) with database persistence

Authority Role Based Access Control (RBAC) Go package with database persistence Install First get authority go get github.com/harranali/authority Next

Comments
  • LICENSE file would help adoption

    LICENSE file would help adoption

    This is a nice piece of code. It would be easier to include/reference in other projects if it had a LICENSE file with the MIT license in it. Lawyers get grumpy about these things.

    opened by danielbprice 1
  • Use math.MaxInt32 instead of hardcoding the value

    Use math.MaxInt32 instead of hardcoding the value

    First of all, thanks for creating and sharing a nice library!

    This pull request is just a nitpick, but it is better to use the the constant defined than hardcoding the value.

    opened by hnakamur 1
  • errgroup

    errgroup

    Am I right in thinking the use case here is very similar to golang.org/x/sync/errgroup ?

    If so, it would be good to add something to the README that mentions why someone might use this library instead of errgroup.

    opened by porjo 0
  • An inappropriate use of `break-select`

    An inappropriate use of `break-select`

    I noticed a break-select in sizewaitgroup.go, which was used in AddWithContext Method, but break keyword can not jump out of the select in Golang. Although it looks like properly worked in this method, but it`s still confusing and misunderstanding.

    opened by faasdfasdfasdfasd 0
Releases(v1.0.0)
Owner
Rémy Mathieu
Rémy Mathieu
Concurrence-go - This project shows the use of concurrence in Golang

This project shows the use of concurrence in Golang. For this purpose, some common problems and their solutions using go are shown.

Lairon Acosta 1 Jan 25, 2022
Speed up the memory allocation and improve the GC performance, especially for dynamic-memory-heavy applications.

Linear Allocator for Golang Goal Speed up the memory allocation and improve the GC performance, especially for dynamic-memory-heavy applications. Pote

crazybie 37 Dec 20, 2022
A sync.WaitGroup with error handling and concurrency control

go-waitgroup How to use An package that allows you to use the constructs of a sync.WaitGroup to create a pool of goroutines and control the concurrenc

Pieter Claerhout 36 Dec 31, 2022
sync.WaitGroup for concurrent use

concwg Description This package provides a version of sync.WaitGroup that allows calling Add and Wait in different goroutines. Motivation sync.WaitGro

Maciek Zając 1 May 20, 2022
Limit-order-book - Limit order books keep records of orders for a given symbol to be traded

Limit Order Book Limit order books keep records of orders for a given symbol to

Michael Bowler 0 Jan 17, 2022
Limits the number of goroutines that are allowed to run concurrently

Golang Concurrency Manager Golang Concurrency Manager package limits the number of goroutines that are allowed to run concurrently. Installation Run t

Bui Thang 61 Dec 12, 2022
Guilherme Biff Zarelli 3 Jun 6, 2022
its the same idea as bruh-bot, but with golang, and add more bots

bruh-bot but more powerful! requirements python go you can used on mac and linux the idea its really simple, can make a lot of bots with the same task

pai 12 Jul 7, 2021
Aquatone is a tool for visual inspection of websites across a large amount of hosts and is convenient for quickly gaining an overview of HTTP-based attack surface.

Aquatone is a tool for visual inspection of websites across a large amount of hosts and is convenient for quickly gaining an overview of HTTP-based attack surface.

Michael Henriksen 5k Jan 6, 2023
port close check scanner. detects open ports, sends alert with slack.

aite9 (port close check scanner) サーバのポートが空いてないことを確認するポートスキャナー たくさんのサーバを管理していると設定ミスで内部利用ポートが外部に公開されてしまっている可能性があり、それに早く気付くためのチェックツールです。 サーバのリストを標準入力で渡すと

Yasushi Ichikawa (ichikaway) 8 Feb 3, 2022