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

gateway/assets: process each template and ETag hash in parallel #530

Merged
merged 1 commit into from
Dec 30, 2023
Merged
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
54 changes: 30 additions & 24 deletions gateway/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"net"
"strconv"
"strings"
"sync"

"github.com/cespare/xxhash/v2"
"github.com/ipfs/boxo/path"
Expand All @@ -26,8 +27,35 @@
)

func init() {
initAssetsHash()
initTemplates()
tmpls := [...]struct {
result **template.Template
sourceFile string
}{
{&DirectoryTemplate, "directory.html"},
{&DagTemplate, "dag.html"},
{&ErrorTemplate, "error.html"},
}

var wg sync.WaitGroup
wg.Add(len(tmpls))

for _, tmpl := range tmpls {
tmpl := tmpl
go func() {
defer wg.Done()
var err error
*tmpl.result, err = BuildTemplate(assets, tmpl.sourceFile)
if err != nil {
panic(err)

Check warning on line 49 in gateway/assets/assets.go

View check run for this annotation

Codecov / codecov/patch

gateway/assets/assets.go#L49

Added line #L49 was not covered by tests
}
}()
}

initAssetsHash() // reuse the init thread instead of blocking on wg.Wait

// @Jorropo: this is still waiting because I was too lazy to break the API of this public package.
// It sounds better if we would use sync.Once and maybe start goroutines in init().
wg.Wait()
}

func initAssetsHash() {
Expand Down Expand Up @@ -56,28 +84,6 @@
AssetHash = strconv.FormatUint(sum.Sum64(), 32)
}

func initTemplates() {
var err error

// Directory listing template
DirectoryTemplate, err = BuildTemplate(assets, "directory.html")
if err != nil {
panic(err)
}

// DAG Index template
DagTemplate, err = BuildTemplate(assets, "dag.html")
if err != nil {
panic(err)
}

// Error template
ErrorTemplate, err = BuildTemplate(assets, "error.html")
if err != nil {
panic(err)
}
}

type MenuItem struct {
URL string
Title string
Expand Down
Loading