concgroup

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 4 Imported by: 2

README

concgroup

Go Reference CI Coverage Code to Test Ratio Test Execution Time

concgroup provides almost the same features to goroutine groups as golang.org/x/sync/errgroup but ensures sequential run of goroutines with the same group key.

Usage

package main

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

	"github.com/k1LoW/concgroup"
)

func main() {
	cg := new(concgroup.Group)
	var urlgroups = map[string][]string{
		"go": {
			"https://go.dev/",
			"https://go.dev/dl/",
		},
		"google": {
			"http://www.google.com/",
		},
	}
	for key, ug := range urlgroups {
	    key := key
		for _, url := range ug {
			url := url // https://golang.org/doc/faq#closures_and_goroutines
			cg.Go(key, func() error {
				// Fetch URL sequentially by key
				resp, err := http.Get(url)
				if err == nil {
					resp.Body.Close()
				}
				return err
			})
		}
	}
	// Wait for all HTTP fetches to complete.
	if err := cg.Wait(); err == nil {
		fmt.Println("Successfully fetched all URLs.")
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

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

Group is a collection of goroutines like errgroup.Group.

Example

Original example is https://pkg.go.dev/golang.org/x/sync/errgroup#example-Group-JustErrors

package main

import (
	"fmt"
	"net/http"

	"github.com/k1LoW/concgroup"
)

func main() {
	cg := new(concgroup.Group)
	var urlgroups = map[string][]string{
		"go": {
			"https://go.dev/",
			"https://go.dev/dl/",
		},
		"google": {
			"http://www.google.com/",
		},
	}
	for key, ug := range urlgroups {
		key := key
		for _, url := range ug {
			url := url // https://golang.org/doc/faq#closures_and_goroutines
			cg.Go(key, func() error {
				// Fetch URL sequentially by key
				resp, err := http.Get(url)
				if err == nil {
					resp.Body.Close()
				}
				return err
			})
		}
	}
	// Wait for all HTTP fetches to complete.
	if err := cg.Wait(); err == nil {
		fmt.Println("Successfully fetched all URLs.")
	}
}
Output:

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new Group and an associated Context like errgroup.Group.

func (*Group) Go

func (g *Group) Go(key string, f func() error)

Go calls the given function in a new goroutine like errgroup.Group with key.

func (*Group) GoMulti added in v1.1.0

func (g *Group) GoMulti(keys []string, f func() error)

GoMulti calls the given function in a new goroutine like errgroup.Group with multiple key locks.

func (*Group) SetLimit

func (g *Group) SetLimit(n int)

SetLimit limits the number of active goroutines in this group to at most n like errgroup.Group.

func (*Group) TryGo

func (g *Group) TryGo(key string, f func() error) bool

TryGo calls the given function only when the number of active goroutines is currently below the configured limit like errgroup.Group with key.

func (*Group) TryGoMulti added in v1.1.0

func (g *Group) TryGoMulti(keys []string, f func() error) bool

TryGoMulti calls the given function only when the number of active goroutines is currently below the configured limit like errgroup.Group with multiple key locks.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned like errgroup.Group.

Jump to

Keyboard shortcuts

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