Documentation
¶
Overview ¶
package group provides a way to manage the lifecycle of a group of goroutines.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type G ¶
type G struct {
// contains filtered or unexported fields
}
G manages the lifetime of a set of goroutines from a common context. The first goroutine in the group to return will cause the context to be canceled, terminating the remaining goroutines.
func (*G) Add ¶
add adds a new goroutine to the group. The goroutine should exit when the context passed to it is canceled.
func (*G) Wait ¶
wait waits for all goroutines in the group to exit. If any of the goroutines fail with an error, wait will return the first error. Wait waits for all goroutines in the group to exit. If any of the goroutines fail with an error, Wait will return the first error.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/pkg/group"
)
func main() {
// A Group's zero value is ready to use.
var g group.G
// Add a goroutine to the group.
g.Add(func(c context.Context) error {
select {
case <-c.Done():
return c.Err()
case <-time.After(1 * time.Second):
return errors.New("timed out")
}
})
// Wait for all goroutines to finish.
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output: timed out
Example (With_context_cancel) ¶
package main
import (
"context"
"fmt"
"time"
"github.com/pkg/group"
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond))
// pass WithContext option to New to use the provided context.
g := group.New(group.WithContext(ctx))
// Add a goroutine to the group.
g.Add(func(c context.Context) error {
select {
case <-c.Done():
return c.Err()
}
})
// Cancel the context.
cancel()
// Wait for all goroutines to finish.
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output: context canceled
Example (With_http_shutdown) ¶
package main
import (
"context"
"fmt"
"net"
"net/http"
"time"
"github.com/pkg/group"
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond))
defer cancel()
g := group.New(group.WithContext(ctx))
g.Add(func(ctx context.Context) error {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return err
}
svr := http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello, world!")
})}
go func() {
svr.Serve(l)
}()
<-ctx.Done() // wait for group to stop
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // five seconds graceful timeout
defer cancel()
return svr.Shutdown(shutdownCtx)
})
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output:
Example (With_panic) ¶
package main
import (
"context"
"fmt"
"github.com/pkg/group"
)
func main() {
// A Group's zero value is ready to use.
var g group.G
// Add a goroutine to the group.
g.Add(func(c context.Context) error {
panic("boom")
})
// Wait for all goroutines to finish.
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output: panic: boom
Example (With_shutdown) ¶
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/pkg/group"
)
func main() {
// A Group's zero value is ready to use.
var g group.G
shutdown := make(chan struct{})
// Add a goroutine to the group.
g.Add(func(c context.Context) error {
select {
case <-c.Done():
return errors.New("stopped")
case <-shutdown:
return errors.New("shutdown")
}
})
time.AfterFunc(100*time.Millisecond, func() {
close(shutdown)
})
// Wait for all goroutines to finish.
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output: shutdown
Example (With_signal) ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/pkg/group"
)
func main() {
ctx := context.Background()
ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
g := group.New(group.WithContext(ctx))
g.Add(MainHTTPServer)
g.Add(DebugHTTPServer)
g.Add(AsyncLogger)
<-time.After(100 * time.Millisecond)
// simulate ^C
proc, _ := os.FindProcess(os.Getpid())
proc.Signal(os.Interrupt)
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
func MainHTTPServer(ctx context.Context) error {
fmt.Println("main http server started")
defer fmt.Println("main http server stopped")
<-ctx.Done()
return ctx.Err()
}
func DebugHTTPServer(ctx context.Context) error {
fmt.Println("debug http server started")
defer fmt.Println("debug http server stopped")
<-ctx.Done()
return ctx.Err()
}
func AsyncLogger(ctx context.Context) error {
fmt.Println("async logger started")
defer fmt.Println("async logger stopped")
<-ctx.Done()
return ctx.Err()
}
Output: async logger started debug http server started main http server started async logger stopped main http server stopped debug http server stopped context canceled
Example (With_startup_error) ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/pkg/group"
)
func main() {
// A Group's zero value is ready to use.
var g group.G
// Add a goroutine to the group.
g.Add(func(_ context.Context) error {
return errors.New("startup error")
})
// Wait for all goroutines to finish, in this case it will return the startup error.
if err := g.Wait(); err != nil {
fmt.Println(err)
}
}
Output: startup error