Optimize multiple file rendering (#189)

This commit is contained in:
Thomas Miceli 2024-01-01 23:40:56 +01:00
parent b3a856a05e
commit 5058ca8f27
2 changed files with 31 additions and 22 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/db"
"github.com/thomiceli/opengist/internal/git" "github.com/thomiceli/opengist/internal/git"
"sync"
) )
type RenderedFile struct { type RenderedFile struct {
@ -67,17 +68,36 @@ func HighlightFile(file *git.File) (RenderedFile, error) {
return rendered, err return rendered, err
} }
func HighlightFiles(files []*git.File) ([]RenderedFile, error) { func HighlightFiles(files []*git.File) []RenderedFile {
renderedFiles := make([]RenderedFile, 0, len(files)) const numWorkers = 10
for _, file := range files { jobs := make(chan int, numWorkers)
rendered, err := HighlightFile(file) renderedFiles := make([]RenderedFile, len(files))
var wg sync.WaitGroup
worker := func() {
for idx := range jobs {
rendered, err := HighlightFile(files[idx])
if err != nil { if err != nil {
log.Warn().Err(err).Msg("Error rendering gist preview for " + file.Filename) log.Error().Err(err).Msg("Error rendering gist preview for " + files[idx].Filename)
} }
renderedFiles = append(renderedFiles, rendered) renderedFiles[idx] = rendered
}
wg.Done()
} }
return renderedFiles, nil for i := 0; i < numWorkers; i++ {
wg.Add(1)
go worker()
}
for i := range files {
jobs <- i
}
close(jobs)
wg.Wait()
return renderedFiles
} }
func HighlightGistPreview(gist *db.Gist) (RenderedGist, error) { func HighlightGistPreview(gist *db.Gist) (RenderedGist, error) {

View file

@ -294,10 +294,7 @@ func gistIndex(ctx echo.Context) error {
return errorRes(500, "Error fetching files", err) return errorRes(500, "Error fetching files", err)
} }
renderedFiles, err := render.HighlightFiles(files) renderedFiles := render.HighlightFiles(files)
if err != nil {
return errorRes(500, "Error rendering files", err)
}
setData(ctx, "page", "code") setData(ctx, "page", "code")
setData(ctx, "commit", revision) setData(ctx, "commit", revision)
@ -314,11 +311,7 @@ func gistJson(ctx echo.Context) error {
return errorRes(500, "Error fetching files", err) return errorRes(500, "Error fetching files", err)
} }
renderedFiles, err := render.HighlightFiles(files) renderedFiles := render.HighlightFiles(files)
if err != nil {
return errorRes(500, "Error rendering files", err)
}
setData(ctx, "files", renderedFiles) setData(ctx, "files", renderedFiles)
htmlbuf := bytes.Buffer{} htmlbuf := bytes.Buffer{}
@ -362,11 +355,7 @@ func gistJs(ctx echo.Context) error {
return errorRes(500, "Error fetching files", err) return errorRes(500, "Error fetching files", err)
} }
renderedFiles, err := render.HighlightFiles(files) renderedFiles := render.HighlightFiles(files)
if err != nil {
return errorRes(500, "Error rendering files", err)
}
setData(ctx, "files", renderedFiles) setData(ctx, "files", renderedFiles)
htmlbuf := bytes.Buffer{} htmlbuf := bytes.Buffer{}