Goit

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

AuthorJakob Wakeling <[email protected]>
Date2023-10-23 03:00:35
Commit5166d87cbc742c010bc82fbab661b3edad09c35b
Parent410f65bf45bdb8f9a277fe7da1ddb29f30fbd3cb

Add debug logging for mutex locking and unlocking

Diffstat

M .vscode/launch.json | 1 +
M main.go | 4 ++++
M src/auth.go | 15 ++++++++++++++-
A src/log.go | 13 +++++++++++++
M src/user/sessions.go | 6 ++++++

5 files changed, 38 insertions, 1 deletions

diff --git a/.vscode/launch.json b/.vscode/launch.json
index ca1ddc7..d743663 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -8,6 +8,7 @@
 			"program": "${workspaceFolder}/main.go",
 			"cwd": "${workspaceFolder}/bin",
 			"mode": "debug",
+			"args": ["--debug"],
 		},
 	],
 }
diff --git a/main.go b/main.go
index eb37c8b..94a1342 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@
 package main
 
 import (
+	"flag"
 	"log"
 	"net/http"
 	"strings"
@@ -18,6 +19,9 @@ import (
 )
 
 func main() {
+	flag.BoolVar(&goit.Debug, "debug", false, "Enable debug logging")
+	flag.Parse()
+
 	log.Println("Starting Goit", res.Version)
 
 	if err := goit.Goit(goit.ConfPath()); err != nil {
diff --git a/src/auth.go b/src/auth.go
index 27158d0..bdd0710 100644
--- a/src/auth.go
+++ b/src/auth.go
@@ -39,12 +39,16 @@ func NewSession(uid int64, ip string, expiry time.Time) (Session, error) {
 	var s = Session{Token: t, Ip: util.If(Conf.IpSessions, ip, ""), Seen: time.Now(), Expiry: expiry}
 
 	SessionsMutex.Lock()
+	Debugln("[goit.NewSession] SessionsMutex lock")
+
 	if Sessions[uid] == nil {
 		Sessions[uid] = []Session{}
 	}
 
 	Sessions[uid] = append(Sessions[uid], s)
+
 	SessionsMutex.Unlock()
+	Debugln("[goit.EndSession] SessionsMutex unlock")
 
 	return s, nil
 }
@@ -52,7 +56,9 @@ func NewSession(uid int64, ip string, expiry time.Time) (Session, error) {
 /* End a user session. */
 func EndSession(uid int64, token string) {
 	SessionsMutex.Lock()
+	Debugln("[goit.EndSession] SessionsMutex lock")
 	defer SessionsMutex.Unlock()
+	defer Debugln("[goit.EndSession] SessionsMutex unlock")
 
 	if Sessions[uid] == nil {
 		return
@@ -75,6 +81,8 @@ func CleanupSessions() {
 	var n int = 0
 
 	SessionsMutex.Lock()
+	Debugln("[goit.CleanupSessions] SessionsMutex lock")
+
 	for uid, v := range Sessions {
 		var i = 0
 		for _, s := range v {
@@ -92,7 +100,9 @@ func CleanupSessions() {
 			Sessions[uid] = v[:i]
 		}
 	}
+
 	SessionsMutex.Unlock()
+	Debugln("[goit.CleanupSessions] SessionsMutex unlock")
 
 	if n > 0 {
 		log.Println("[Cleanup] cleaned up", n, "expired sessions")
@@ -123,6 +133,10 @@ func GetSessionCookie(r *http.Request) (int64, Session) {
 		}
 
 		SessionsMutex.Lock()
+		Debugln("[goit.GetSessionCookie] SessionsMutex lock")
+		defer SessionsMutex.Unlock()
+		defer Debugln("[goit.GetSessionCookie] SessionsMutex unlock")
+
 		for i, s := range Sessions[uid] {
 			if ss[1] == s.Token {
 				if s != (Session{}) {
@@ -133,7 +147,6 @@ func GetSessionCookie(r *http.Request) (int64, Session) {
 				return uid, s
 			}
 		}
-		SessionsMutex.Unlock()
 
 		return uid, Session{}
 	}
diff --git a/src/log.go b/src/log.go
new file mode 100644
index 0000000..2268859
--- /dev/null
+++ b/src/log.go
@@ -0,0 +1,13 @@
+package goit
+
+import "log"
+
+var Debug = false
+
+func Debugln(v ...any) {
+	if Debug {
+		var a = []any{"\033[34m[DEBUG]\033[0m"}
+		a = append(a, v...)
+		log.Println(a...)
+	}
+}
diff --git a/src/user/sessions.go b/src/user/sessions.go
index 6f20118..7a6a041 100644
--- a/src/user/sessions.go
+++ b/src/user/sessions.go
@@ -32,11 +32,15 @@ func HandleSessions(w http.ResponseWriter, r *http.Request) {
 	}{Title: "User - Sessions"}
 
 	goit.SessionsMutex.RLock()
+	goit.Debugln("[goit.HandleSessions] SessionsMutex rlock")
+
 	if revoke >= 0 && revoke < int64(len(goit.Sessions[uid])) {
 		var token = goit.Sessions[uid][revoke].Token
 		var current = token == ss.Token
 
 		goit.SessionsMutex.RUnlock()
+		goit.Debugln("[goit.HandleSessions] SessionsMutex runlock")
+
 		goit.EndSession(uid, token)
 
 		if current {
@@ -55,7 +59,9 @@ func HandleSessions(w http.ResponseWriter, r *http.Request) {
 			Current: util.If(v.Token == ss.Token, "(current)", ""),
 		})
 	}
+
 	goit.SessionsMutex.RUnlock()
+	goit.Debugln("[goit.HandleSessions] SessionsMutex runlock")
 
 	if err := goit.Tmpl.ExecuteTemplate(w, "user/sessions", data); err != nil {
 		log.Println("[/user/login]", err.Error())