sync

package
v0.0.92 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 6 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

func New

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

New returns a group and a context(derived from ctx). A group waits for a collection of goroutines to finish. It has almost similar semantics to sync.WaitGroup. It limits the number of active goroutines to at most n.

The derived Context is canceled the first time Go returns. Unlike golang.org/x/sync/errgroup.Group it is not cancelled the first time a function passed to Go returns an error or panics.

n limits the number of active goroutines in this group. If n is negative, the limit is set to runtime.NumCPU

Example (JustErrors)

JustErrors illustrates the use of a group 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:

Types

type PanicError added in v0.0.92

type PanicError struct {
	Recovered error
	Stack     []byte
}

A PanicError wraps an error recovered from an unhandled panic when calling a function passed to Go.

func (PanicError) Error added in v0.0.92

func (p PanicError) Error() string

func (PanicError) Unwrap added in v0.0.92

func (p PanicError) Unwrap() error

type PanicValue added in v0.0.92

type PanicValue struct {
	Recovered interface{}
	Stack     []byte
}

A PanicValue wraps a value that does not implement the error interface recovered from an unhandled panic when calling a function passed to Go.

func (PanicValue) String added in v0.0.92

func (p PanicValue) String() string

Jump to

Keyboard shortcuts

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