mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
refactor: use sync.OnceValue
This commit is contained in:
committed by
Vincent Bernat
parent
d2536398e9
commit
f3ebff6ce5
@@ -14,28 +14,18 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
//go:embed data/embed.zip
|
||||||
//go:embed data/embed.zip
|
var embeddedZip []byte
|
||||||
embeddedZip []byte
|
|
||||||
data fs.FS
|
var dataOnce = sync.OnceValue(func() *zip.Reader {
|
||||||
dataOnce sync.Once
|
r, err := zip.NewReader(bytes.NewReader(embeddedZip), int64(len(embeddedZip)))
|
||||||
dataReady chan struct{}
|
if err != nil {
|
||||||
)
|
panic(fmt.Sprintf("cannot read embedded archive: %s", err))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
})
|
||||||
|
|
||||||
// Data returns a filesystem with the files contained in the embedded archive.
|
// Data returns a filesystem with the files contained in the embedded archive.
|
||||||
func Data() fs.FS {
|
func Data() fs.FS {
|
||||||
dataOnce.Do(func() {
|
return dataOnce()
|
||||||
r, err := zip.NewReader(bytes.NewReader(embeddedZip), int64(len(embeddedZip)))
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("cannot read embedded archive: %s", err))
|
|
||||||
}
|
|
||||||
data = r
|
|
||||||
close(dataReady)
|
|
||||||
})
|
|
||||||
<-dataReady
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dataReady = make(chan struct{})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user