runnergroup

package module
v0.0.0-...-5864ffe Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 3 Imported by: 30 modules, 35 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

RunnerGroup

GoDoc

RunnerGroup is like sync.WaitGroup, the diffrence is if one task stops, all will be stopped.

❤️ A project by txthinking.com

Install

$ go get github.com/txthinking/runnergroup

Example

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

	"github.com/txthinking/runnergroup"
)

func Example() {
	g := runnergroup.New()

	s := &http.Server{
		Addr: ":9991",
	}
	g.Add(&runnergroup.Runner{
		Start: func() error {
			return s.ListenAndServe()
		},
		Stop: func() error {
			return s.Shutdown(context.Background())
		},
	})

	s1 := &http.Server{
		Addr: ":9992",
	}
	g.Add(&runnergroup.Runner{
		Start: func() error {
			return s1.ListenAndServe()
		},
		Stop: func() error {
			return s1.Shutdown(context.Background())
		},
	})

	go func() {
		time.Sleep(5 * time.Second)
		log.Println(g.Done())
	}()
	log.Println(g.Wait())
	// Output:
}

License

Licensed under The MIT License

Documentation

Overview

Example
package main

import (
	"context"
	"errors"
	"log"
	"net/http"
	"time"

	"github.com/txthinking/runnergroup"
)

func main() {
	g := runnergroup.New()

	s1 := &http.Server{
		Addr: ":9991",
	}
	g.Add(&runnergroup.Runner{
		Start: func() error {
			log.Println("start1")
			return s1.ListenAndServe()
		},
		Stop: func() error {
			log.Println("stop1")
			return s1.Shutdown(context.Background())
		},
	})

	s2 := &http.Server{
		Addr: ":9992",
	}
	g.Add(&runnergroup.Runner{
		Start: func() error {
			log.Println("start2")
			return errors.New("fail")
			// return s2.ListenAndServe()
		},
		Stop: func() error {
			log.Println("stop2")
			return s2.Shutdown(context.Background())
		},
	})

	s3 := &http.Server{
		Addr: ":9993",
	}
	g.Add(&runnergroup.Runner{
		Start: func() error {
			log.Println("start3")
			return s3.ListenAndServe()
		},
		Stop: func() error {
			log.Println("stop3")
			time.Sleep(5 * time.Second)
			return s3.Shutdown(context.Background())
		},
	})

	go func() {
		time.Sleep(3 * time.Second)
		g.Done()
	}()
	log.Println(g.Wait())
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Start []string
	Stop  []string
}

func (*Error) Error

func (e *Error) Error() string

type Runner

type Runner struct {
	// Start is a blocking function.
	Start func() error
	// Stop is not a blocking function, if Stop called, must let Start return.
	Stop func() error
}

Each Start and Stop in Runner will be called once

type RunnerGroup

type RunnerGroup struct {
	Runners []*Runner
	// contains filtered or unexported fields
}

RunnerGroup is like sync.WaitGroup, the diffrence is if one task stops, all will be stopped.

func New

func New() *RunnerGroup

func (*RunnerGroup) Add

func (g *RunnerGroup) Add(r *Runner)

func (*RunnerGroup) Done

func (g *RunnerGroup) Done() error

Call Done to stop all tasks.

func (*RunnerGroup) Wait

func (g *RunnerGroup) Wait() error

Call Wait after all tasks have been added,

Jump to

Keyboard shortcuts

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