Skip to content

Commit

Permalink
Cancel context if finish syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Apr 20, 2020
1 parent eea13dc commit 5a0cc39
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/check_quick.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
}

func runCheckQuickCmd(cmd *cobra.Command, args []string) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())

fetcher := fetcher.New(
ctx,
Expand Down Expand Up @@ -87,6 +87,7 @@ func runCheckQuickCmd(cmd *cobra.Command, args []string) {
g.Go(func() error {
return syncer.Sync(
ctx,
cancel,
StartIndex,
EndIndex,
)
Expand Down
6 changes: 5 additions & 1 deletion internal/syncer/stateless_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ func (s *StatelessSyncer) nextSyncableRange(

func (s *StatelessSyncer) Sync(
ctx context.Context,
cancel context.CancelFunc,
startIndex int64,
endIndex int64,
) error {
defer cancel()

currIndex := startIndex
for ctx.Err() == nil {
newCurrIndex, stopIndex, halt, err := s.nextSyncableRange(
Expand All @@ -128,7 +131,7 @@ func (s *StatelessSyncer) Sync(
return err
}
if halt {
return nil
break
}

err = s.SyncRange(ctx, newCurrIndex, stopIndex)
Expand All @@ -138,5 +141,6 @@ func (s *StatelessSyncer) Sync(

currIndex = stopIndex
}
log.Printf("Finished sycning %d-%d\n", startIndex, endIndex)
return nil
}

0 comments on commit 5a0cc39

Please sign in to comment.