semerrgroup

package module
v0.0.0-...-746d517 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

README

semerrgroup

ci PkgGoDev codecov Go Report Card

Package semerrgroup is errgroup wrapper with the limitation of the number of concurrent executions by the semaphore.

Most was stolen from errgroup.

Example

package semerrgroup_test

import (
	"context"
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/johejo/semerrgroup"
)

func ExampleLimitedGroup() {
	g, ctx := semerrgroup.WithContext(context.Background(), 2) // only two tasks run in parallel.

	begin := time.Now()
	// run three tasks
	for i := 0; i < 3; i++ {
		g.Go(ctx, func() error {
			time.Sleep(1 * time.Second)
			return nil
		})
	}
	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}
	since := time.Since(begin).Round(time.Second)
	if since != 2*time.Second {
		log.Fatalf("should pass abount 2 seconds, but passed %v", since)
	}
	fmt.Println(since)

	// Output:
	// 2s
}

func ExampleLimitedGroup_cancel_acquisition() {
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()
	g, ctx := semerrgroup.WithContext(ctx, 1)
	g.Go(ctx, func() error {
		fmt.Println("task1 started")
		<-ctx.Done()
		fmt.Println("task1 completed")
		return nil
	})
	g.Go(ctx, func() error {
		// will not start
		fmt.Println("task2 started")
		<-ctx.Done()
		fmt.Println("task2 completed")
		return nil
	})
	err := g.Wait()
	if !errors.Is(err, context.DeadlineExceeded) {
		log.Fatalf("should return context.DeadlintExceeded, but got %v", err)
	}
	fmt.Println("finish")

	// Output:
	// task1 started
	// task1 completed
	// finish
}

License

BSD 3-Clause

Author

Mitsuo Heijo (@johejo)

Documentation

Overview

Package semerrgroup is errgroup wrapper with the limitation of the number of concurrent executions by the semaphore.

Most was stolen from errgroup.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LimitedGroup

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

LimitedGroup is a wrapper for Group with semaphore. A LimitedGroup is a collection of goroutines working on subtasks that are part of the same overall task.

A zero LimitedGroup is valid and does not cancel on error and skip acquire.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/johejo/semerrgroup"
)

func main() {
	g, ctx := semerrgroup.WithContext(context.Background(), 2) // only two tasks run in parallel.

	begin := time.Now()
	// run three tasks
	for i := 0; i < 3; i++ {
		g.Go(ctx, func() error {
			time.Sleep(1 * time.Second)
			return nil
		})
	}
	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}
	since := time.Since(begin).Round(time.Second)
	if since != 2*time.Second {
		log.Fatalf("should pass abount 2 seconds, but passed %v", since)
	}
	fmt.Println(since)

}
Output:

2s
Example (Cancel_acquisition)
package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/johejo/semerrgroup"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()
	g, ctx := semerrgroup.WithContext(ctx, 1)
	g.Go(ctx, func() error {
		fmt.Println("task1 started")
		<-ctx.Done()
		fmt.Println("task1 completed")
		return ctx.Err()
	})
	g.Go(ctx, func() error {
		// will not start
		fmt.Println("task2 started")
		<-ctx.Done()
		fmt.Println("task2 completed")
		return nil
	})
	err := g.Wait()
	if !errors.Is(err, context.DeadlineExceeded) {
		log.Fatalf("should return context.DeadlintExceeded, but got %v", err)
	}
	fmt.Println("finish")

}
Output:

task1 started
task1 completed
finish

func WithContext

func WithContext(ctx context.Context, n int64) (*LimitedGroup, context.Context)

WithContext returns a new LimitedGroup with the given context and weight.

func (*LimitedGroup) Go

func (g *LimitedGroup) Go(ctx context.Context, f func() error)

Go calls the given function in a new goroutine.

Acquisition can be canceled in the given context.

More goroutines than given in WithContext will not start.

func (*LimitedGroup) Wait

func (g *LimitedGroup) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

Jump to

Keyboard shortcuts

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