forked from TrueCloudLab/frostfs-node
Initial commit
Initial public review release v0.10.0
This commit is contained in:
commit
dadfd90dcd
276 changed files with 46331 additions and 0 deletions
59
lib/fix/catch.go
Normal file
59
lib/fix/catch.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (a *app) Catch(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if a.log == nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
a.log.Fatal("Can't run app",
|
||||
zap.Error(err))
|
||||
}
|
||||
|
||||
// CatchTrace catch errors for debugging
|
||||
// use that function just for debug your application.
|
||||
func (a *app) CatchTrace(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// digging into the root of the problem
|
||||
for {
|
||||
var (
|
||||
ok bool
|
||||
v = reflect.ValueOf(err)
|
||||
fn reflect.Value
|
||||
)
|
||||
|
||||
if v.Type().Kind() != reflect.Struct {
|
||||
break
|
||||
}
|
||||
|
||||
if !v.FieldByName("Reason").IsValid() {
|
||||
break
|
||||
}
|
||||
|
||||
if v.FieldByName("Func").IsValid() {
|
||||
fn = v.FieldByName("Func")
|
||||
}
|
||||
|
||||
fmt.Printf("Place: %#v\nReason: %s\n\n", fn, err)
|
||||
|
||||
if err, ok = v.FieldByName("Reason").Interface().(error); !ok {
|
||||
err = v.Interface().(error)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
panic(err)
|
||||
}
|
53
lib/fix/config/config.go
Normal file
53
lib/fix/config/config.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Params groups the parameters of configuration.
|
||||
type Params struct {
|
||||
File string
|
||||
Type string
|
||||
Prefix string
|
||||
Name string
|
||||
Version string
|
||||
|
||||
AppDefaults func(v *viper.Viper)
|
||||
}
|
||||
|
||||
// NewConfig is a configuration tool's constructor.
|
||||
func NewConfig(p Params) (v *viper.Viper, err error) {
|
||||
v = viper.New()
|
||||
v.SetEnvPrefix(p.Prefix)
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
v.SetDefault("app.name", p.Name)
|
||||
v.SetDefault("app.version", p.Version)
|
||||
|
||||
if p.AppDefaults != nil {
|
||||
p.AppDefaults(v)
|
||||
}
|
||||
|
||||
if p.fromFile() {
|
||||
v.SetConfigFile(p.File)
|
||||
v.SetConfigType(p.safeType())
|
||||
|
||||
err = v.ReadInConfig()
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
||||
func (p Params) fromFile() bool {
|
||||
return p.File != ""
|
||||
}
|
||||
|
||||
func (p Params) safeType() string {
|
||||
if p.Type == "" {
|
||||
p.Type = "yaml"
|
||||
}
|
||||
return strings.ToLower(p.Type)
|
||||
}
|
112
lib/fix/fix.go
Normal file
112
lib/fix/fix.go
Normal file
|
@ -0,0 +1,112 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/lib/fix/config"
|
||||
"github.com/nspcc-dev/neofs-node/lib/fix/logger"
|
||||
"github.com/nspcc-dev/neofs-node/lib/fix/module"
|
||||
"github.com/nspcc-dev/neofs-node/misc"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/dig"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
// App is an interface of executable application.
|
||||
App interface {
|
||||
Run() error
|
||||
RunAndCatch()
|
||||
}
|
||||
|
||||
app struct {
|
||||
err error
|
||||
log *zap.Logger
|
||||
di *dig.Container
|
||||
runner interface{}
|
||||
}
|
||||
|
||||
// Settings groups the application parameters.
|
||||
Settings struct {
|
||||
File string
|
||||
Type string
|
||||
Name string
|
||||
Prefix string
|
||||
Build string
|
||||
Version string
|
||||
Runner interface{}
|
||||
|
||||
AppDefaults func(v *viper.Viper)
|
||||
}
|
||||
)
|
||||
|
||||
func (a *app) RunAndCatch() {
|
||||
err := a.Run()
|
||||
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
|
||||
if ok, _ := strconv.ParseBool(misc.Debug); ok {
|
||||
a.CatchTrace(err)
|
||||
}
|
||||
|
||||
a.Catch(err)
|
||||
}
|
||||
|
||||
func (a *app) Run() error {
|
||||
if a.err != nil {
|
||||
return a.err
|
||||
}
|
||||
|
||||
// setup app logger:
|
||||
if err := a.di.Invoke(func(l *zap.Logger) {
|
||||
a.log = l
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return a.di.Invoke(a.runner)
|
||||
}
|
||||
|
||||
// New is an application constructor.
|
||||
func New(s *Settings, mod module.Module) App {
|
||||
var (
|
||||
a app
|
||||
err error
|
||||
)
|
||||
|
||||
a.di = dig.New(dig.DeferAcyclicVerification())
|
||||
a.runner = s.Runner
|
||||
|
||||
if s.Prefix == "" {
|
||||
s.Prefix = s.Name
|
||||
}
|
||||
|
||||
mod = mod.Append(
|
||||
module.Module{
|
||||
{Constructor: logger.NewLogger},
|
||||
{Constructor: NewGracefulContext},
|
||||
{Constructor: func() (*viper.Viper, error) {
|
||||
return config.NewConfig(config.Params{
|
||||
File: s.File,
|
||||
Type: s.Type,
|
||||
Prefix: strings.ToUpper(s.Prefix),
|
||||
Name: s.Name,
|
||||
Version: fmt.Sprintf("%s(%s)", s.Version, s.Build),
|
||||
|
||||
AppDefaults: s.AppDefaults,
|
||||
})
|
||||
}},
|
||||
})
|
||||
|
||||
if err = module.Provide(a.di, mod); err != nil {
|
||||
a.err = err
|
||||
}
|
||||
|
||||
return &a
|
||||
}
|
26
lib/fix/grace.go
Normal file
26
lib/fix/grace.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// NewGracefulContext returns graceful context.
|
||||
func NewGracefulContext(l *zap.Logger) context.Context {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
go func() {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||
sig := <-ch
|
||||
l.Info("received signal",
|
||||
zap.String("signal", sig.String()))
|
||||
cancel()
|
||||
}()
|
||||
|
||||
return ctx
|
||||
}
|
90
lib/fix/logger/logger.go
Normal file
90
lib/fix/logger/logger.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
const (
|
||||
formatJSON = "json"
|
||||
formatConsole = "console"
|
||||
|
||||
defaultSamplingInitial = 100
|
||||
defaultSamplingThereafter = 100
|
||||
)
|
||||
|
||||
func safeLevel(lvl string) zap.AtomicLevel {
|
||||
switch strings.ToLower(lvl) {
|
||||
case "debug":
|
||||
return zap.NewAtomicLevelAt(zap.DebugLevel)
|
||||
case "warn":
|
||||
return zap.NewAtomicLevelAt(zap.WarnLevel)
|
||||
case "error":
|
||||
return zap.NewAtomicLevelAt(zap.ErrorLevel)
|
||||
case "fatal":
|
||||
return zap.NewAtomicLevelAt(zap.FatalLevel)
|
||||
case "panic":
|
||||
return zap.NewAtomicLevelAt(zap.PanicLevel)
|
||||
default:
|
||||
return zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
}
|
||||
}
|
||||
|
||||
// NewLogger is a logger's constructor.
|
||||
func NewLogger(v *viper.Viper) (*zap.Logger, error) {
|
||||
c := zap.NewProductionConfig()
|
||||
|
||||
c.OutputPaths = []string{"stdout"}
|
||||
c.ErrorOutputPaths = []string{"stdout"}
|
||||
|
||||
if v.IsSet("logger.sampling") {
|
||||
c.Sampling = &zap.SamplingConfig{
|
||||
Initial: defaultSamplingInitial,
|
||||
Thereafter: defaultSamplingThereafter,
|
||||
}
|
||||
|
||||
if val := v.GetInt("logger.sampling.initial"); val > 0 {
|
||||
c.Sampling.Initial = val
|
||||
}
|
||||
|
||||
if val := v.GetInt("logger.sampling.thereafter"); val > 0 {
|
||||
c.Sampling.Thereafter = val
|
||||
}
|
||||
}
|
||||
|
||||
// logger level
|
||||
c.Level = safeLevel(v.GetString("logger.level"))
|
||||
traceLvl := safeLevel(v.GetString("logger.trace_level"))
|
||||
|
||||
// logger format
|
||||
switch f := v.GetString("logger.format"); strings.ToLower(f) {
|
||||
case formatConsole:
|
||||
c.Encoding = formatConsole
|
||||
default:
|
||||
c.Encoding = formatJSON
|
||||
}
|
||||
|
||||
// logger time
|
||||
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
|
||||
l, err := c.Build(
|
||||
// enable trace only for current log-level
|
||||
zap.AddStacktrace(traceLvl))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if v.GetBool("logger.no_disclaimer") {
|
||||
return l, nil
|
||||
}
|
||||
|
||||
name := v.GetString("app.name")
|
||||
version := v.GetString("app.version")
|
||||
|
||||
return l.With(
|
||||
zap.String("app_name", name),
|
||||
zap.String("app_version", version)), nil
|
||||
}
|
35
lib/fix/module/module.go
Normal file
35
lib/fix/module/module.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package module
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type (
|
||||
// Module type
|
||||
Module []*Provider
|
||||
|
||||
// Provider struct
|
||||
Provider struct {
|
||||
Constructor interface{}
|
||||
Options []dig.ProvideOption
|
||||
}
|
||||
)
|
||||
|
||||
// Append module to target module and return new module
|
||||
func (m Module) Append(mods ...Module) Module {
|
||||
var result = m
|
||||
for _, mod := range mods {
|
||||
result = append(result, mod...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Provide set providers functions to DI container
|
||||
func Provide(dic *dig.Container, providers Module) error {
|
||||
for _, p := range providers {
|
||||
if err := dic.Provide(p.Constructor, p.Options...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
46
lib/fix/services.go
Normal file
46
lib/fix/services.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type (
|
||||
// Service interface
|
||||
Service interface {
|
||||
Start(context.Context)
|
||||
Stop()
|
||||
}
|
||||
|
||||
combiner []Service
|
||||
)
|
||||
|
||||
var _ Service = (combiner)(nil)
|
||||
|
||||
// NewServices creates single runner.
|
||||
func NewServices(items ...Service) Service {
|
||||
var svc = make(combiner, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
svc = append(svc, item)
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
// Start all services.
|
||||
func (c combiner) Start(ctx context.Context) {
|
||||
for _, svc := range c {
|
||||
svc.Start(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop all services.
|
||||
func (c combiner) Stop() {
|
||||
for _, svc := range c {
|
||||
svc.Stop()
|
||||
}
|
||||
}
|
114
lib/fix/web/http.go
Normal file
114
lib/fix/web/http.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
httpParams struct {
|
||||
Key string
|
||||
Viper *viper.Viper
|
||||
Logger *zap.Logger
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
httpServer struct {
|
||||
name string
|
||||
started *int32
|
||||
logger *zap.Logger
|
||||
shutdownTTL time.Duration
|
||||
server server
|
||||
}
|
||||
)
|
||||
|
||||
func (h *httpServer) Start(ctx context.Context) {
|
||||
if h == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !atomic.CompareAndSwapInt32(h.started, 0, 1) {
|
||||
h.logger.Info("http: already started",
|
||||
zap.String("server", h.name))
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := h.server.serve(ctx); err != nil {
|
||||
if err != http.ErrServerClosed {
|
||||
h.logger.Error("http: could not start server",
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (h *httpServer) Stop() {
|
||||
if h == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !atomic.CompareAndSwapInt32(h.started, 1, 0) {
|
||||
h.logger.Info("http: already stopped",
|
||||
zap.String("server", h.name))
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), h.shutdownTTL)
|
||||
defer cancel()
|
||||
|
||||
h.logger.Debug("http: try to stop server",
|
||||
zap.String("server", h.name))
|
||||
|
||||
if err := h.server.shutdown(ctx); err != nil {
|
||||
h.logger.Error("http: could not stop server",
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
const defaultShutdownTTL = 30 * time.Second
|
||||
|
||||
func newHTTPServer(p httpParams) *httpServer {
|
||||
var (
|
||||
address string
|
||||
shutdown time.Duration
|
||||
)
|
||||
|
||||
if address = p.Viper.GetString(p.Key + ".address"); address == "" {
|
||||
p.Logger.Info("Empty bind address, skip",
|
||||
zap.String("server", p.Key))
|
||||
return nil
|
||||
}
|
||||
if p.Handler == nil {
|
||||
p.Logger.Info("Empty handler, skip",
|
||||
zap.String("server", p.Key))
|
||||
return nil
|
||||
}
|
||||
|
||||
p.Logger.Info("Create http.Server",
|
||||
zap.String("server", p.Key),
|
||||
zap.String("address", address))
|
||||
|
||||
if shutdown = p.Viper.GetDuration(p.Key + ".shutdown_ttl"); shutdown <= 0 {
|
||||
shutdown = defaultShutdownTTL
|
||||
}
|
||||
|
||||
return &httpServer{
|
||||
name: p.Key,
|
||||
started: new(int32),
|
||||
logger: p.Logger,
|
||||
shutdownTTL: shutdown,
|
||||
server: newServer(params{
|
||||
Address: address,
|
||||
Name: p.Key,
|
||||
Config: p.Viper,
|
||||
Logger: p.Logger,
|
||||
Handler: p.Handler,
|
||||
}),
|
||||
}
|
||||
}
|
32
lib/fix/web/metrics.go
Normal file
32
lib/fix/web/metrics.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Metrics is an interface of metric tool.
|
||||
type Metrics interface {
|
||||
Start(ctx context.Context)
|
||||
Stop()
|
||||
}
|
||||
|
||||
const metricsKey = "metrics"
|
||||
|
||||
// NewMetrics is a metric tool's constructor.
|
||||
func NewMetrics(l *zap.Logger, v *viper.Viper) Metrics {
|
||||
if !v.GetBool(metricsKey + ".enabled") {
|
||||
l.Debug("metrics server disabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
return newHTTPServer(httpParams{
|
||||
Key: metricsKey,
|
||||
Viper: v,
|
||||
Logger: l,
|
||||
Handler: promhttp.Handler(),
|
||||
})
|
||||
}
|
44
lib/fix/web/pprof.go
Normal file
44
lib/fix/web/pprof.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expvar"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Profiler is an interface of profiler.
|
||||
type Profiler interface {
|
||||
Start(ctx context.Context)
|
||||
Stop()
|
||||
}
|
||||
|
||||
const profilerKey = "pprof"
|
||||
|
||||
// NewProfiler is a profiler's constructor.
|
||||
func NewProfiler(l *zap.Logger, v *viper.Viper) Profiler {
|
||||
if !v.GetBool(profilerKey + ".enabled") {
|
||||
l.Debug("pprof server disabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/debug/vars", expvar.Handler())
|
||||
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
|
||||
return newHTTPServer(httpParams{
|
||||
Key: profilerKey,
|
||||
Viper: v,
|
||||
Logger: l,
|
||||
Handler: mux,
|
||||
})
|
||||
}
|
62
lib/fix/web/server.go
Normal file
62
lib/fix/web/server.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
// Server is an interface of server.
|
||||
server interface {
|
||||
serve(ctx context.Context) error
|
||||
shutdown(ctx context.Context) error
|
||||
}
|
||||
|
||||
contextServer struct {
|
||||
logger *zap.Logger
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
params struct {
|
||||
Address string
|
||||
Name string
|
||||
Config *viper.Viper
|
||||
Logger *zap.Logger
|
||||
Handler http.Handler
|
||||
}
|
||||
)
|
||||
|
||||
func newServer(p params) server {
|
||||
return &contextServer{
|
||||
logger: p.Logger,
|
||||
server: &http.Server{
|
||||
Addr: p.Address,
|
||||
Handler: p.Handler,
|
||||
ReadTimeout: p.Config.GetDuration(p.Name + ".read_timeout"),
|
||||
ReadHeaderTimeout: p.Config.GetDuration(p.Name + ".read_header_timeout"),
|
||||
WriteTimeout: p.Config.GetDuration(p.Name + ".write_timeout"),
|
||||
IdleTimeout: p.Config.GetDuration(p.Name + ".idle_timeout"),
|
||||
MaxHeaderBytes: p.Config.GetInt(p.Name + ".max_header_bytes"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *contextServer) serve(ctx context.Context) error {
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
if err := cs.server.Close(); err != nil {
|
||||
cs.logger.Info("something went wrong",
|
||||
zap.Error(err))
|
||||
}
|
||||
}()
|
||||
|
||||
return cs.server.ListenAndServe()
|
||||
}
|
||||
|
||||
func (cs *contextServer) shutdown(ctx context.Context) error {
|
||||
return cs.server.Shutdown(ctx)
|
||||
}
|
79
lib/fix/worker/worker.go
Normal file
79
lib/fix/worker/worker.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
// Workers is an interface of worker tool.
|
||||
Workers interface {
|
||||
Start(context.Context)
|
||||
Stop()
|
||||
|
||||
Add(Job Handler)
|
||||
}
|
||||
|
||||
workers struct {
|
||||
cancel context.CancelFunc
|
||||
started *int32
|
||||
wg *sync.WaitGroup
|
||||
jobs []Handler
|
||||
}
|
||||
|
||||
// Handler is a worker's handling function.
|
||||
Handler func(ctx context.Context)
|
||||
|
||||
// Jobs is a map of worker names to handlers.
|
||||
Jobs map[string]Handler
|
||||
|
||||
// Job groups the parameters of worker's job.
|
||||
Job struct {
|
||||
Disabled bool
|
||||
Immediately bool
|
||||
Timer time.Duration
|
||||
Ticker time.Duration
|
||||
Handler Handler
|
||||
}
|
||||
)
|
||||
|
||||
// New is a constructor of workers.
|
||||
func New() Workers {
|
||||
return &workers{
|
||||
started: new(int32),
|
||||
wg: new(sync.WaitGroup),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *workers) Add(job Handler) {
|
||||
w.jobs = append(w.jobs, job)
|
||||
}
|
||||
|
||||
func (w *workers) Stop() {
|
||||
if !atomic.CompareAndSwapInt32(w.started, 1, 0) {
|
||||
// already stopped
|
||||
return
|
||||
}
|
||||
|
||||
w.cancel()
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
func (w *workers) Start(ctx context.Context) {
|
||||
if !atomic.CompareAndSwapInt32(w.started, 0, 1) {
|
||||
// already started
|
||||
return
|
||||
}
|
||||
|
||||
ctx, w.cancel = context.WithCancel(ctx)
|
||||
for _, job := range w.jobs {
|
||||
w.wg.Add(1)
|
||||
|
||||
go func(handler Handler) {
|
||||
defer w.wg.Done()
|
||||
handler(ctx)
|
||||
}(job)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue