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

feat: make delay between tasks configurable #151

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ var Run = &cli.Command{
Usage: "Lease time for the chain economics processor",
EnvVars: []string{"VISOR_CHAINECONOMICS_LEASE"},
},

&cli.DurationFlag{
Name: "task-delay",
Aliases: []string{"td"},
Value: 500 * time.Millisecond,
Usage: "Base time to wait between starting tasks (jitter is added)",
EnvVars: []string{"VISOR_TASK_DELAY"},
},
},
Action: func(cctx *cli.Context) error {
// Validate flags
Expand Down Expand Up @@ -246,7 +254,7 @@ var Run = &cli.Command{
}
}()

scheduler := schedule.NewScheduler()
scheduler := schedule.NewScheduler(cctx.Duration("task-delay"))

// Add one indexing task to follow the chain head
if cctx.Bool("indexhead") {
Expand Down
15 changes: 11 additions & 4 deletions schedule/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ type Locker interface {
Unlock(context.Context) error
}

func NewScheduler() *Scheduler {
return &Scheduler{}
func NewScheduler(taskDelay time.Duration) *Scheduler {
// Enforce a minimum delay
if taskDelay == 0 {
taskDelay = 100 * time.Millisecond
}
return &Scheduler{
taskDelay: taskDelay,
}
}

type Scheduler struct {
tasks []TaskConfig
tasks []TaskConfig
taskDelay time.Duration
}

// Add add a task config to the scheduler. This must not be called after Run.
Expand Down Expand Up @@ -147,7 +154,7 @@ func (s *Scheduler) Run(ctx context.Context) error {
default:
}
// A little jitter between tasks to reduce thundering herd effects on api
wait.SleepWithJitter(500*time.Millisecond, 2)
wait.SleepWithJitter(s.taskDelay, 2)
}

// Wait until the context is done or all tasks have been completed
Expand Down