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

Allow adjustable number of rendering workers via MAX_WORKERS envar #306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"fmt"
"io"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"sync"
"text/template"

Expand Down Expand Up @@ -72,6 +75,15 @@ func new(g grafana.Client, dashName string, time grafana.TimeRange, texTemplate
return &report{g, time, texTemplate, dashName, tmpDir, ""}
}

func (rep *report) getWorkerNum() int {
var (
desiredWorkers, _ = strconv.Atoi(os.Getenv("MAX_WORKERS"))
maxWorkers = runtime.NumCPU()
)
desiredWorkers = int(math.Max(1, math.Min(float64(desiredWorkers), float64(maxWorkers))))
return desiredWorkers
}

// Generate returns the report.pdf file. After reading this file it should be Closed()
// After closing the file, call report.Clean() to delete the file as well the temporary build files
func (rep *report) Generate() (pdf io.ReadCloser, err error) {
Expand Down Expand Up @@ -130,6 +142,10 @@ func (rep *report) texPath() string {
}

func (rep *report) renderPNGsParallel(dash grafana.Dashboard) error {
var (
wg sync.WaitGroup
workers = rep.getWorkerNum()
)
//buffer all panels on a channel
panels := make(chan grafana.Panel, len(dash.Panels))
for _, p := range dash.Panels {
Expand All @@ -140,8 +156,7 @@ func (rep *report) renderPNGsParallel(dash grafana.Dashboard) error {
//fetch images in parrallel form Grafana sever.
//limit concurrency using a worker pool to avoid overwhelming grafana
//for dashboards with many panels.
var wg sync.WaitGroup
workers := 5

wg.Add(workers)
errs := make(chan error, len(dash.Panels)) //routines can return errors on a channel
for i := 0; i < workers; i++ {
Expand Down