Skip to content

Commit 7f29c37

Browse files
authored
clipapi: replace tomb with errgroup (#4207)
1 parent 356d311 commit 7f29c37

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

cmd/crowdsec-cli/clipapi/papi.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/fatih/color"
1010
log "github.com/sirupsen/logrus"
1111
"github.com/spf13/cobra"
12-
"gopkg.in/tomb.v2"
12+
"golang.org/x/sync/errgroup"
1313

1414
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/core/args"
1515
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/core/require"
@@ -117,22 +117,22 @@ func (cli *cliPapi) newStatusCmd() *cobra.Command {
117117

118118
func (cli *cliPapi) sync(ctx context.Context, db *database.Client) error {
119119
cfg := cli.cfg()
120-
t := tomb.Tomb{}
121120

122121
apic, err := apiserver.NewAPIC(ctx, cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
123122
if err != nil {
124123
return fmt.Errorf("unable to initialize API client: %w", err)
125124
}
126125

127-
t.Go(func() error { return apic.Push(ctx) })
126+
g, ctx := errgroup.WithContext(ctx)
127+
g.Go(func() error { return apic.Push(ctx) })
128128

129129
papiLogger := cfg.API.Server.NewPAPILogger()
130130
papi, err := apiserver.NewPAPI(apic, db, cfg.API.Server.ConsoleConfig, papiLogger)
131131
if err != nil {
132132
return fmt.Errorf("unable to initialize PAPI client: %w", err)
133133
}
134134

135-
t.Go(func() error { return papi.SyncDecisions(ctx) })
135+
g.Go(func() error { return papi.SyncDecisions(ctx) })
136136

137137
err = papi.PullOnce(ctx, time.Time{}, true)
138138
if err != nil {
@@ -143,7 +143,7 @@ func (cli *cliPapi) sync(ctx context.Context, db *database.Client) error {
143143

144144
apic.Shutdown()
145145
papi.Shutdown()
146-
_ = t.Wait()
146+
_ = g.Wait()
147147
time.Sleep(5 * time.Second) // FIXME: the push done by apic.Push is run inside a sub goroutine, sleep to make sure it's done
148148

149149
return nil

0 commit comments

Comments
 (0)