diff --git a/goatherd.go b/goatherd.go index b7c7cc3640211cff840da68e38770c97ed999d82..3e476a6ad8e8a835e4ac32904bea43333e0fd6fd 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()