Skip to content

Commit

Permalink
ref: use errgroup to handle goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Sep 26, 2023
1 parent 1dd240d commit 50cf3a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
35 changes: 24 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 50cf3a2

Please sign in to comment.