From 5137bd3a1d8c851d3c13fa3aaa1d468dde555333 Mon Sep 17 00:00:00 2001 From: Ian Davis Date: Tue, 27 Oct 2020 12:17:36 +0000 Subject: [PATCH] feat: make delay between tasks configurable --- commands/run.go | 10 +++++++++- schedule/scheduler.go | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/commands/run.go b/commands/run.go index fca05a1a3..5dc5672e6 100644 --- a/commands/run.go +++ b/commands/run.go @@ -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 @@ -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") { diff --git a/schedule/scheduler.go b/schedule/scheduler.go index f53b17828..dcbd1a235 100644 --- a/schedule/scheduler.go +++ b/schedule/scheduler.go @@ -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. @@ -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