Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-12-18 02:10:30
Commit8ac69385f3d76ca2410977a7b3b262b6c0de1124
Parente530f2c3c78a07dfb1531cfb8274183ba664a68f

Add mutex to diffstat cache map

Diffstat

M src/goit/git.go | 7 +++++++
M src/repo/log.go | 1 -

2 files changed, 7 insertions, 1 deletions

diff --git a/src/goit/git.go b/src/goit/git.go
index 4c3b040..7e0c35c 100644
--- a/src/goit/git.go
+++ b/src/goit/git.go
@@ -13,6 +13,7 @@ import (
 	"os/exec"
 	"strconv"
 	"strings"
+	"sync"
 
 	"github.com/go-chi/chi/v5"
 	"github.com/go-git/go-git/v5/plumbing"
@@ -228,11 +229,14 @@ type DiffStat struct {
 }
 
 var diffs = map[plumbing.Hash][]DiffStat{}
+var diffsLock sync.RWMutex
 
 func DiffStats(c *object.Commit) ([]DiffStat, error) {
+	diffsLock.RLock()
 	if stats, ok := diffs[c.Hash]; ok {
 		return stats, nil
 	}
+	diffsLock.RUnlock()
 
 	from, err := c.Tree()
 	if err != nil {
@@ -310,6 +314,9 @@ func DiffStats(c *object.Commit) ([]DiffStat, error) {
 		stats = append(stats, stat)
 	}
 
+	diffsLock.Lock()
 	diffs[c.Hash] = stats
+	diffsLock.Unlock()
+
 	return stats, nil
 }
diff --git a/src/repo/log.go b/src/repo/log.go
index 23e9954..c2987d2 100644
--- a/src/repo/log.go
+++ b/src/repo/log.go
@@ -91,7 +91,6 @@ func HandleLog(w http.ResponseWriter, r *http.Request) {
 	} else if err := iter.ForEach(func(c *object.Commit) error {
 		var files, additions, deletions int
 
-		/* TODO speed this up or cache it, diff calculations are quite slow */
 		if stats, err := goit.DiffStats(c); err != nil {
 			log.Println("[/repo/log]", err.Error())
 		} else if tpath != "" {