Skip to content

Commit a89d7b0

Browse files
committed
🥅 Cache lock
Signed-off-by: Muhammed Hussain Karimi <[email protected]>
1 parent 905c865 commit a89d7b0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"path/filepath"
3030
"strconv"
3131
"strings"
32+
"sync"
3233
"time"
3334

3435
"github.com/brpaz/echozap"
@@ -218,15 +219,17 @@ func start(_ *cobra.Command, _ []string) {
218219
if err := os.MkdirAll(uiCacheDir, 0755); err != nil {
219220
log.Logger.Panic("Failed to create UI cache directory", zap.Error(err))
220221
}
222+
cacheLock := sync.Mutex{}
221223
uiGroup.GET("/*", func(c echo.Context) error {
222224
prefix := filepath.Join("/"+strings.TrimPrefix(cfg.BaseURI, "/"), "/ui/")
223225
filePath := strings.TrimPrefix(strings.TrimPrefix(c.Request().URL.Path, prefix), "/")
224226
fileNameMd5 := md5.Sum([]byte(filePath))
225227
fileNameHash := hex.EncodeToString(fileNameMd5[:])
226228
cachedFilePath := filepath.Join(uiCacheDir, fileNameHash)
227-
228229
modifiedContent, err := os.ReadFile(cachedFilePath)
229230
if err != nil {
231+
cacheLock.Lock()
232+
defer cacheLock.Unlock()
230233
file, err := ui.MainFS.Open(filePath)
231234
if err != nil {
232235
filePath = "index.html"

0 commit comments

Comments
 (0)