https://github.com/da440dil/go-workgroup
Synchronization for groups of related goroutines
https://github.com/da440dil/go-workgroup
go golang goroutines synchronization workgroup
Last synced: 2 months ago
JSON representation
Synchronization for groups of related goroutines
- Host: GitHub
- URL: https://github.com/da440dil/go-workgroup
- Owner: da440dil
- License: mit
- Created: 2019-03-14T07:57:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-08-19T08:40:25.000Z (10 months ago)
- Last Synced: 2025-08-19T10:44:24.786Z (10 months ago)
- Topics: go, golang, goroutines, synchronization, workgroup
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 79
- Watchers: 2
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-workgroup
[](https://github.com/da440dil/go-workgroup/actions/workflows/go.yml)
[](https://coveralls.io/github/da440dil/go-workgroup?branch=master)
[](https://pkg.go.dev/github.com/da440dil/go-workgroup)
[](https://goreportcard.com/report/github.com/da440dil/go-workgroup)
Synchronization for groups of related goroutines.
## [Example](./examples/server_test.go) HTTP server
```go
srv := http.Server{Addr: "127.0.0.1:8080"}
// Create context to cancel execution after 100 milliseconds,
// increase timeout to manually interrupt execution with SIGINT
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
// Execute each function in its own goroutine
err := workgroup.Run(
workgroup.Server(
func() error {
fmt.Printf("Server listen at %v\n", srv.Addr)
err := srv.ListenAndServe()
fmt.Printf("Server stopped listening with error: %v\n", err)
if err != http.ErrServerClosed {
return err
}
return nil
},
func() error {
fmt.Println("Server is about to shutdown")
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
err := srv.Shutdown(ctx)
fmt.Printf("Server shutdown with error: %v\n", err)
return err
},
),
workgroup.Context(ctx),
workgroup.Signal(),
)
fmt.Printf("Workgroup run stopped with error: %v\n", err)
// Output:
// Server listen at 127.0.0.1:8080
// Server is about to shutdown
// Server stopped listening with error: http: Server closed
// Server shutdown with error:
// Workgroup run stopped with error: context deadline exceeded
```