Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create additional goroutine for cleanup #395

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions service/datasetworker/datasetworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,23 @@ func (w *Thread) Start(ctx context.Context) ([]service.Done, service.Fail, error
done := make(chan struct{})
fail := make(chan error)
go func() {
defer cancel()
defer close(done)
w.run(ctx, fail)
w.logger.Info("worker thread finished")
}()
cancel()

cleanupDone := make(chan struct{})
go func() {
defer close(cleanupDone)
<-ctx.Done()
ctx2, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ctxCleanup, cancelCleanup := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCleanup()
//nolint:contextcheck
err := w.cleanup(ctx2)
err := w.cleanup(ctxCleanup)
if err != nil {
w.logger.Errorw("failed to cleanup", "error", err)
} else {
w.logger.Info("cleanup complete")
}
}()

return []service.Done{done, healthcheckDone, healthcheckCleanupDone, cleanupDone}, fail, nil
return []service.Done{done, healthcheckDone, healthcheckCleanupDone}, fail, nil
}

func (w *Thread) Name() string {
Expand Down Expand Up @@ -261,6 +256,7 @@ func (w *Thread) run(ctx context.Context, errChan chan error) {
jobTypes = append(jobTypes, model.Pack)
}

var timer *time.Timer
interval := w.config.MinInterval
for {
workCtx, workCancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -329,10 +325,16 @@ func (w *Thread) run(ctx context.Context, errChan chan error) {
}
loop:
w.logger.Infof("sleeping for %s", interval)
if timer == nil {
timer = time.NewTimer(interval)
} else {
timer.Reset(interval)
}
select {
case <-ctx.Done():
timer.Stop()
return
case <-time.After(interval):
case <-timer.C:
interval *= 2
if interval > w.config.MaxInterval {
interval = w.config.MaxInterval
Expand Down