diff --git a/go.mod b/go.mod index d257ef23..86376d02 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( require ( github.com/google/uuid v1.3.0 github.com/prometheus/client_golang v1.14.0 + golang.org/x/sync v0.1.0 ) require ( diff --git a/go.sum b/go.sum index 6860aff8..bde36771 100644 --- a/go.sum +++ b/go.sum @@ -439,14 +439,6 @@ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4d github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ulule/gokvstores v0.1.1-0.20221229151109-3bd12fb72ebe h1:ayWYvm5FWr78c8RYS32fXoZ5DM+sn3ngVeE3CHY3beM= github.com/ulule/gokvstores v0.1.1-0.20221229151109-3bd12fb72ebe/go.mod h1:2buRSW9ZL73BFOkRwBSOVcPJG/JQxcbZPs8fo0TD5X0= -github.com/ulule/gostorages v0.2.5-0.20230314124119-11134a4bce61 h1:Twadj4iaibm+nUX5PvElLeq5YsechTLpITZb/p27ywI= -github.com/ulule/gostorages v0.2.5-0.20230314124119-11134a4bce61/go.mod h1:7yGiGHJ9mE+He6sB42cJaRE4zcselRo4g2GBd8NGEMU= -github.com/ulule/gostorages v0.2.5-0.20230707071807-ba19d4693f53 h1:uzC+DOzybak6pNOK7u0UdTomOEu2wPuCxkLad9WNs44= -github.com/ulule/gostorages v0.2.5-0.20230707071807-ba19d4693f53/go.mod h1:7yGiGHJ9mE+He6sB42cJaRE4zcselRo4g2GBd8NGEMU= -github.com/ulule/gostorages v0.2.5-0.20230707072626-d429b21ca9b4 h1:PTBZrQv/VKCYBZQdbBj5ug+wvJZ+HuYLjAhzzu5PnB4= -github.com/ulule/gostorages v0.2.5-0.20230707072626-d429b21ca9b4/go.mod h1:7yGiGHJ9mE+He6sB42cJaRE4zcselRo4g2GBd8NGEMU= -github.com/ulule/gostorages v0.2.5-0.20230710095702-bfbe5260f605 h1:PYD0HGywjlJ+WxFivejD2zyVRD0rRclPmCHx+HdBbXQ= -github.com/ulule/gostorages v0.2.5-0.20230710095702-bfbe5260f605/go.mod h1:7yGiGHJ9mE+He6sB42cJaRE4zcselRo4g2GBd8NGEMU= github.com/ulule/gostorages v0.2.5-0.20230920134537-c63293fd790c h1:bNM3RCu+JUVpPW3yZ7aWb/CIpW5DO8z0PGuqAquCnTE= github.com/ulule/gostorages v0.2.5-0.20230920134537-c63293fd790c/go.mod h1:nMhvJt6g5Ulmp3Y1M159+xvsaF3EPwdWdvBnjYAWK3o= github.com/urfave/cli v1.22.10 h1:p8Fspmz3iTctJstry1PYS3HVdllxnEzTEsgIgtxTrCk= diff --git a/server/server.go b/server/server.go index 5accb93e..ffd0fa71 100644 --- a/server/server.go +++ b/server/server.go @@ -10,6 +10,7 @@ import ( "github.com/thoas/picfit" "github.com/thoas/picfit/config" loggerpkg "github.com/thoas/picfit/logger" + "golang.org/x/sync/errgroup" ) func New(ctx context.Context, cfg *config.Config) (*HTTPServer, error) { @@ -39,19 +40,31 @@ func Run(ctx context.Context, path string) error { } ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM) defer stop() - go func() { - for range time.Tick(time.Duration(cfg.Options.FreeMemoryInterval) * time.Second) { - loggerpkg.LogMemStats(ctx, "Force free memory", server.processor.Logger) - debug.FreeOSMemory() + + g, _ := errgroup.WithContext(context.Background()) + g.Go(func() error { + ticker := time.Tick(time.Duration(cfg.Options.FreeMemoryInterval) * time.Second) + for { + select { + case <-ticker: + loggerpkg.LogMemStats(ctx, "Force free memory", server.processor.Logger) + debug.FreeOSMemory() + case <-ctx.Done(): + return nil + } } - }() - if err := server.Run(ctx); err != nil { - return err - } + }) - select { // nolint:gosimple - case <-ctx.Done(): - stop() + g.Go(func() error { + if err := server.Run(ctx); err != nil { + return err + } + + return nil + }) + + if err := g.Wait(); err != nil { + return err } return nil