From bf7c937be5778d2c9fb805afd21e0d20a33bc4ad Mon Sep 17 00:00:00 2001
From: Alexander Wuerstlein <arw@arw.name>
Date: Mon, 12 Aug 2019 21:23:27 +0200
Subject: [PATCH] oops, locking. thx izibi

---
 goatherd.go | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/goatherd.go b/goatherd.go
index b7c7cc3..3e476a6 100644
--- a/goatherd.go
+++ b/goatherd.go
@@ -291,6 +291,7 @@ func transactionFailed(err error) bool {
 }
 
 var autoresyncList map[string]*autoresyncEntry = make(map[string]*autoresyncEntry)
+var autoresyncListLock sync.RWMutex
 
 type autoresyncEntry struct {
 	Time    int64
@@ -322,7 +323,9 @@ func checkOffer(remote string, name string, offer string) (bool, error) {
 			errPanic(err)
 		}
 
+		autoresyncListLock.RLock()
 		if s, found := autoresyncList[name]; found {
+			autoresyncListLock.RUnlock()
 			debugf("[%v] autoresync in progress: %v", remote, s)
 			if uint64(time.Now().Unix()-s.Time) <= cfg.AutoresyncTime {
 				if s.Num >= cfg.AutoresyncRepeat && s.Counter-hotp.Counter() < cfg.AutoresyncLookahead {
@@ -335,8 +338,12 @@ func checkOffer(remote string, name string, offer string) (bool, error) {
 			} else {
 				// timeout
 				debugf("[%v] autoresync timeout: %v", remote, name)
+				autoresyncListLock.Lock()
 				delete(autoresyncList, name)
+				autoresyncListLock.Unlock()
 			}
+		} else {
+			autoresyncListLock.RUnlock()
 		}
 
 		for i = uint64(0); i <= lookahead; i++ {
@@ -362,6 +369,7 @@ func checkOffer(remote string, name string, offer string) (bool, error) {
 		for ; i <= cfg.AutoresyncLookahead; i++ {
 			debugf("[%v] autoresync checking for match (offset %v counter %v)", remote, i, hotp.Counter())
 			if hotp.Check(offer) {
+				autoresyncListLock.Lock()
 				debugf("[%v] autoresync repeat count increase hotp.Counter %v, %v", remote, hotp.Counter(), autoresyncList[name])
 				if s, found := autoresyncList[name]; found && hotp.Counter()-1 == s.Counter {
 					s.Time = time.Now().Unix()
@@ -376,6 +384,7 @@ func checkOffer(remote string, name string, offer string) (bool, error) {
 					}
 					debugf("[%v] autoresync repeat count init for %v", remote, name)
 				}
+				autoresyncListLock.Unlock()
 				break
 			} else {
 				hotp.Increment()
-- 
GitLab