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 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) Wait ¶
func (g *RunnerGroup) Wait() error
Call Wait after all tasks have been added,
Click to show internal directories.
Click to hide internal directories.