• Stars
    star
    43
  • Rank 623,081 (Top 13 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A sync.WaitGroup with error handling and concurrency control

go-waitgroup

Build Status Go Report Card Documentation license GitHub version GitHub issues

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 concurrency.

Using it is just like a normal sync.WaitGroup. The only difference is the initialisation. When you use waitgroup.NewWaitGroup, you have the option to specify it's size.

Any int which is bigger than 0 will limit the number of concurrent goroutines. If you specify -1 or 0, all goroutines will run at once (just like a plain sync.WaitGroup).

package main

import (
    "fmt"
    "net/http"

    "github.com/pieterclaerhout/go-waitgroup"
)

func main() {
    
    urls := []string{
        "https://www.easyjet.com/",
        "https://www.skyscanner.de/",
        "https://www.ryanair.com",
        "https://wizzair.com/",
        "https://www.swiss.com/",
    }

    wg := waitgroup.NewWaitGroup(3)

	for _, url := range urls {
		wg.BlockAdd()
		go func(url string) {
			defer wg.Done()
			fmt.Printf("%s: checking\n", url)
			res, err := http.Get(url)
			if err != nil {
				fmt.Println("Error: %v")
			} else {
				defer res.Body.Close()
				fmt.Printf("%s: result: %v\n", url, err)
			}
		}(url)
	}

    wg.Wait()
    fmt.Println("Finished")

}

Using closures

There is also a way to use function closures to make it even more readable:

package main

import (
	"fmt"
	"net/http"

	"github.com/pieterclaerhout/go-waitgroup"
)

func main() {

	urls := []string{
		"https://www.easyjet.com/",
		"https://www.skyscanner.de/",
		"https://www.ryanair.com",
		"https://wizzair.com/",
		"https://www.swiss.com/",
	}

	wg := waitgroup.NewWaitGroup(3)

	for _, url := range urls {

		urlToCheck := url
		wg.Add(func() {
			fmt.Printf("%s: checking\n", urlToCheck)
			res, err := http.Get(urlToCheck)
			if err != nil {
				fmt.Println("Error: %v")
			} else {
				defer res.Body.Close()
				fmt.Printf("%s: result: %v\n", urlToCheck, err)
			}
		})

	}

	wg.Wait()
	fmt.Println("Finished")

}

Handling errors

If you want to handle errors, there is also an ErrorGroup. This uses the same principles as a normal WaitGroup with a small twist.

First of all, you can only add functions which returns just an error.

Second, as soon as one of the queued items fail, the rest will be cancelled:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/pieterclaerhout/go-waitgroup"
)

func main() {

	ctx := context.Background()

	wg, ctx := waitgroup.NewErrorGroup(ctx, tc.size)
	if err != nil {
		fmt.Println("Error: %v")
		os.Exit(1)
	}

	wg.Add(func() error {
		return nil
	})

	wg.Add(func() error {
		return errors.New("An error occurred")
	})

	if err := wg.Wait(); err != nil {
		fmt.Println("Error: %v")
		os.Exit(1)
	}

}

You can also add multiple functions in one step:

package main

import (
	"context"
	"errors"
	"fmt"
	"os"

	"github.com/pieterclaerhout/go-waitgroup"
)

func main() {

	ctx := context.Background()

	wg, ctx := waitgroup.NewErrorGroup(ctx, tc.size)
	if err != nil {
		fmt.Println("Error: %v")
		os.Exit(1)
	}

	wg.Add(
		func() error {
			return nil
		},
		func() error {
			return errors.New("An error occurred")
		},
	)

	if err := wg.Wait(); err != nil {
		fmt.Println("Error: %v")
		os.Exit(1)
	}

}

More Repositories

1

go-james

James is your butler and helps you to create, build, debug, test and run your Go projects
Go
61
star
2

go-xray

Helpers for making the use of reflection easier
Go
27
star
3

export-komoot

Proof-of-concept to export your planned tours from Komoot
Go
24
star
4

go-finance

Finance related Go functions (e.g. exchange rates, VAT number checking, …)
Go
23
star
5

go-log

A logging library with strack traces, object dumping and optional timestamps
Go
10
star
6

go-geoip

A module to make it easier to use the MaxMind GeoIP database in a microservice environment
Go
5
star
7

example-command-output

An example for https://www.yellowduck.be/posts/reading-command-output-line-by-line/
Go
4
star
8

kubeboard

A very basic way to run the Kubernetes Dashboard as a separate app
Go
2
star
9

pytest-as-a-github-action

Running pytest as a GitHub action
Python
2
star
10

example-json-unixtimestamp

An example project for https://www.yellowduck.be/posts/handling-unix-timestamps-in-json/
Go
2
star
11

kotlin-yellowduck-gpx

A GPX library written in Kotlin
Kotlin
2
star
12

jono-indesign-scripts

A couple of scripts used to automate tedious InDesign tasks
JavaScript
2
star
13

go-formatter

A package which allows you to format JSON, SQL, …
Go
2
star
14

detectapplesilicon

Detecting Apple Silicon using Go
Go
2
star
15

go-html

A Go way of generating HTML without writing templates
Go
2
star
16

vscode-snippets

Snippets I'm using for Visual Studio Code
1
star
17

example-apiclient

An example project for https://www.yellowduck.be/posts/mocking-a-http-server/
Go
1
star
18

go-date

A Go library with date related functions
Go
1
star
19

laravel-sanctum-api

PHP
1
star
20

go-jobqueue

Background job queue based on MySQL (for now)
Go
1
star
21

example-jwt

Example with Labstack Echo and JWT authentication
Go
1
star
22

pieterclaerhout

My GitHub profile page.
1
star
23

add-jira-ticket-number

Add Jira ticket number
TypeScript
1
star