Skip to content

Commit

Permalink
Add --worker-delay flag to rate-limit on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Jun 27, 2021
1 parent 3bc3282 commit 30b1897
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ Usage of sequence:
Timeout of the render request (default 1m0s)
-width int
The width of the image (default 1920)
-worker-delay duration
Delay worker startup (default 2s)
```
2 changes: 2 additions & 0 deletions cmd/grafana-image-renderer-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {
startPadding = sequenceCommand.Duration("start-padding", 0, "Duration to add to the start of the frame")
endPadding = sequenceCommand.Duration("end-padding", 0, "Duration to add to the end of the frame")
maxConcurrency = sequenceCommand.Int("max-concurrency", 5, "Maximum number of concurrent render requests")
workerDelay = sequenceCommand.Duration("worker-delay", 2*time.Second, "Delay worker startup")
outDirectory = sequenceCommand.String("out-directory", "frames", "Directory to write rendered frames to")

imageCommand = flag.NewFlagSet("image", flag.ExitOnError)
Expand Down Expand Up @@ -160,6 +161,7 @@ func main() {
Interval: *frameInterval,
StartPadding: *startPadding,
EndPadding: *endPadding,
WorkerDelay: *workerDelay,
MaxConcurrency: *maxConcurrency,
SaveCallback: func(b []byte, n int) error {
filename := filepath.Join(*outDirectory, fmt.Sprintf("%06d.png", n))
Expand Down
8 changes: 6 additions & 2 deletions pkg/sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// RenderFunc is used to define render behavior. You should define the
// call that should be made to the renderer. In its sumplest form, it
// call that should be made to the renderer. In its simplest form, it
// should simply call client.Render with the time parameters passed.
type RenderFunc func(time.Time, time.Time) ([]byte, int, error)

Expand Down Expand Up @@ -35,6 +35,9 @@ type FrameSequencer struct {
// Padding can be added or subtracted from the frame.
StartPadding, EndPadding time.Duration

// Delay before starting each new worker.
WorkerDelay time.Duration

// Maximum number of concurrent render requests.
MaxConcurrency int

Expand Down Expand Up @@ -91,7 +94,8 @@ func (s *FrameSequencer) Sequence(start, end int) {
}
}

func (s *FrameSequencer) renderWorker(in <-chan frame, out chan<- error) {
func (s *FrameSequencer) renderWorker(n int, in <-chan frame, out chan<- error) {
time.Sleep(s.WorkerDelay * time.Duration(n))
for f := range in {
startTime := time.Now()
fmt.Printf("Rendering frame %d\n", f.num)
Expand Down

0 comments on commit 30b1897

Please sign in to comment.