sync

package
v0.0.88 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package sync provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WaitGroup

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

WaitGroup is a datastructure that waits for a collection of goroutines to finish. See sync.WaitGroup

Use New to get a valid Waitgroup

Example (JustErrors)

JustErrors illustrates the use of a WaitGroup in place of a sync.WaitGroup to simplify goroutine counting and error handling. This example is derived from the sync.WaitGroup example at https://golang.org/pkg/sync/#example_WaitGroup.

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/komuw/ong/sync"
)

func main() {
	wg, ctx := sync.New(context.Background(), 2)

	urls := []string{
		"http://www.example.org/",
		"http://www.example.com/",
		"http://www.nonExistentDomainName.com/",
	}

	funcs := []func() error{}
	for _, url := range urls {
		url := url // https://golang.org/doc/faq#closures_and_goroutines
		funcs = append(
			funcs,
			func() error {
				// Fetch the URL.
				ct, cancel := context.WithTimeout(ctx, 4*time.Second)
				defer cancel()

				req, err := http.NewRequestWithContext(ct, http.MethodGet, url, nil)
				if err != nil {
					return err
				}
				resp, err := http.DefaultClient.Do(req)
				if err != nil {
					return err
				}
				defer resp.Body.Close()
				return err
			},
		)
	}

	funcs = append(
		funcs,
		func() error {
			return nil
		},
	)

	err := wg.Go(funcs...)
	fmt.Printf("\n\t err: %v. cause: %v\n\n", err, context.Cause(ctx))
}
Output:

func New

func New(ctx context.Context, n int) (*WaitGroup, context.Context)

New returns a valid WaitGroup and a context(derived from ctx). The WaitGroup limits the number of active goroutines to at most n.

The derived Context is canceled the first time a function passed to Go returns an error or the first time Go returns, whichever occurs first.

n limits the number of active goroutines in this WaitGroup. If n is <=0, it indicates no limit.

func (*WaitGroup) Go

func (w *WaitGroup) Go(funcs ...func() error) error

Go calls each of the given functions in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the WaitGroup exceeding the configured limit.

It also blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them. The first call to return an error cancels the WaitGroup's context.

If called concurrently, it will block until the previous call returns.

Jump to

Keyboard shortcuts

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