Bump github.com/gookit/config/v2 from 2.2.3 to 2.2.4
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.3 to 2.2.4. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.3...v2.2.4) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
30784affc4
commit
0df009eae0
+35
@@ -0,0 +1,35 @@
|
||||
package syncs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// WaitCloseSignals for some huang program.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// // do something. eg: start a http server
|
||||
//
|
||||
// syncs.WaitCloseSignals(func(sig os.Signal) {
|
||||
// // do something
|
||||
// })
|
||||
func WaitCloseSignals(onClose func(sig os.Signal)) {
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
// block until a signal is received.
|
||||
onClose(<-signals)
|
||||
}
|
||||
|
||||
// Go is a basic promise implementation: it wraps calls a function in a goroutine
|
||||
// and returns a channel which will later return the function's return value.
|
||||
func Go(f func() error) error {
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
ch <- f()
|
||||
}()
|
||||
|
||||
return <-ch
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package syncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// ErrGroup is a collection of goroutines working on subtasks that
|
||||
// are part of the same overall task.
|
||||
//
|
||||
// Refers:
|
||||
//
|
||||
// https://github.com/neilotoole/errgroup
|
||||
// https://github.com/fatih/semgroup
|
||||
type ErrGroup struct {
|
||||
*errgroup.Group
|
||||
}
|
||||
|
||||
// NewCtxErrGroup instance
|
||||
func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context) {
|
||||
egg, ctx1 := errgroup.WithContext(ctx)
|
||||
if len(limit) > 0 && limit[0] > 0 {
|
||||
egg.SetLimit(limit[0])
|
||||
}
|
||||
|
||||
eg := &ErrGroup{Group: egg}
|
||||
return eg, ctx1
|
||||
}
|
||||
|
||||
// NewErrGroup instance
|
||||
func NewErrGroup(limit ...int) *ErrGroup {
|
||||
eg := &ErrGroup{Group: new(errgroup.Group)}
|
||||
|
||||
if len(limit) > 0 && limit[0] > 0 {
|
||||
eg.SetLimit(limit[0])
|
||||
}
|
||||
return eg
|
||||
}
|
||||
|
||||
// Add one or more handler at once
|
||||
func (g *ErrGroup) Add(handlers ...func() error) {
|
||||
for _, handler := range handlers {
|
||||
g.Go(handler)
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package syncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
)
|
||||
|
||||
// SignalHandler returns an actor, i.e. an execute and interrupt func, that
|
||||
// terminates with SignalError when the process receives one of the provided
|
||||
// signals, or the parent context is canceled.
|
||||
//
|
||||
// from https://github.com/oklog/run/blob/master/actors.go
|
||||
func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error)) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
return func() error {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, signals...)
|
||||
defer signal.Stop(c)
|
||||
select {
|
||||
case sig := <-c:
|
||||
return SignalError{Signal: sig}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}, func(error) {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// SignalError is returned by the signal handler's execute function
|
||||
// when it terminates due to a received signal.
|
||||
type SignalError struct {
|
||||
Signal os.Signal
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e SignalError) Error() string {
|
||||
return fmt.Sprintf("received signal %s", e.Signal)
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// Package syncs provides synchronization primitives util functions.
|
||||
package syncs
|
||||
Reference in New Issue
Block a user