mirror of
https://github.com/thomiceli/opengist.git
synced 2024-12-23 04:52:40 +00:00
Optimize multiple file rendering (#189)
This commit is contained in:
parent
b3a856a05e
commit
5058ca8f27
2 changed files with 31 additions and 22 deletions
|
@ -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))
|
||||||
if err != nil {
|
var wg sync.WaitGroup
|
||||||
log.Warn().Err(err).Msg("Error rendering gist preview for " + file.Filename)
|
|
||||||
|
worker := func() {
|
||||||
|
for idx := range jobs {
|
||||||
|
rendered, err := HighlightFile(files[idx])
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error rendering gist preview for " + files[idx].Filename)
|
||||||
|
}
|
||||||
|
renderedFiles[idx] = rendered
|
||||||
}
|
}
|
||||||
renderedFiles = append(renderedFiles, 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) {
|
||||||
|
|
|
@ -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{}
|
||||||
|
|
Loading…
Reference in a new issue