Skip to content

Commit

Permalink
polygon/sync: refactor Service.Run with errgroup (#10351)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored May 15, 2024
1 parent b4a4058 commit 99f15f8
Showing 1 changed file with 11 additions and 50 deletions.
61 changes: 11 additions & 50 deletions polygon/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

lru "github.com/hashicorp/golang-lru/arc/v2"
"github.com/ledgerwatch/log/v3"
"golang.org/x/sync/errgroup"

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -108,57 +109,17 @@ func NewService(
}
}

func (s *service) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
func (s *service) Run(parentCtx context.Context) error {
group, ctx := errgroup.WithContext(parentCtx)

var serviceErr error

go func() {
s.p2pService.Run(ctx)
}()

go func() {
err := s.store.Run(ctx)
if (err != nil) && (ctx.Err() == nil) {
serviceErr = err
cancel()
}
}()

go func() {
err := s.events.Run(ctx)
if (err != nil) && (ctx.Err() == nil) {
serviceErr = err
cancel()
}
}()

go func() {
// TODO: remove when heimdall.NewService is functional
if s.heimdallService == nil {
return
}
err := s.heimdallService.Run(ctx)
if (err != nil) && (ctx.Err() == nil) {
serviceErr = err
cancel()
}
}()

go func() {
err := s.sync.Run(ctx)
if (err != nil) && (ctx.Err() == nil) {
serviceErr = err
cancel()
}
}()

<-ctx.Done()

if serviceErr != nil {
return serviceErr
group.Go(func() error { s.p2pService.Run(ctx); return nil })
group.Go(func() error { return s.store.Run(ctx) })
group.Go(func() error { return s.events.Run(ctx) })
// TODO: remove the check when heimdall.NewService is functional
if s.heimdallService != nil {
group.Go(func() error { return s.heimdallService.Run(ctx) })
}
group.Go(func() error { return s.sync.Run(ctx) })

return ctx.Err()
return group.Wait()
}

0 comments on commit 99f15f8

Please sign in to comment.