Remove the serverInfo rwlock; just use the global serversInfo rwlock

This commit is contained in:
Frank Denis 2019-10-26 17:28:24 +02:00
parent d17b572b75
commit 4824e91d46
1 changed files with 8 additions and 9 deletions

View File

@ -32,7 +32,6 @@ type RegisteredServer struct {
} }
type ServerInfo struct { type ServerInfo struct {
sync.RWMutex
Proto stamps.StampProtoType Proto stamps.StampProtoType
MagicQuery [8]byte MagicQuery [8]byte
ServerPk [32]byte ServerPk [32]byte
@ -158,6 +157,7 @@ func (serversInfo *ServersInfo) refresh(proxy *Proxy) (int, error) {
} }
func (serversInfo *ServersInfo) estimatorUpdate() { func (serversInfo *ServersInfo) estimatorUpdate() {
// serversInfo.RWMutex is assumed to be Locked
candidate := rand.Intn(len(serversInfo.inner)) candidate := rand.Intn(len(serversInfo.inner))
if candidate == 0 { if candidate == 0 {
return return
@ -191,9 +191,9 @@ func (serversInfo *ServersInfo) estimatorUpdate() {
func (serversInfo *ServersInfo) getOne() *ServerInfo { func (serversInfo *ServersInfo) getOne() *ServerInfo {
serversInfo.Lock() serversInfo.Lock()
defer serversInfo.Unlock()
serversCount := len(serversInfo.inner) serversCount := len(serversInfo.inner)
if serversCount <= 0 { if serversCount <= 0 {
serversInfo.Unlock()
return nil return nil
} }
if serversInfo.lbEstimator { if serversInfo.lbEstimator {
@ -211,7 +211,6 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
candidate = rand.Intn(Min(serversCount, 2)) candidate = rand.Intn(Min(serversCount, 2))
} }
serverInfo := serversInfo.inner[candidate] serverInfo := serversInfo.inner[candidate]
serversInfo.Unlock()
dlog.Debugf("Using candidate [%s] RTT: %d", (*serverInfo).Name, int((*serverInfo).rtt.Value())) dlog.Debugf("Using candidate [%s] RTT: %d", (*serverInfo).Name, int((*serverInfo).rtt.Value()))
return serverInfo return serverInfo
@ -415,24 +414,24 @@ func fetchDoHServerInfo(proxy *Proxy, name string, stamp stamps.ServerStamp, isN
} }
func (serverInfo *ServerInfo) noticeFailure(proxy *Proxy) { func (serverInfo *ServerInfo) noticeFailure(proxy *Proxy) {
serverInfo.Lock() proxy.serversInfo.Lock()
serverInfo.rtt.Add(float64(proxy.timeout.Nanoseconds() / 1000000)) serverInfo.rtt.Add(float64(proxy.timeout.Nanoseconds() / 1000000))
serverInfo.Unlock() proxy.serversInfo.Unlock()
} }
func (serverInfo *ServerInfo) noticeBegin(proxy *Proxy) { func (serverInfo *ServerInfo) noticeBegin(proxy *Proxy) {
serverInfo.Lock() proxy.serversInfo.Lock()
serverInfo.lastActionTS = time.Now() serverInfo.lastActionTS = time.Now()
serverInfo.Unlock() proxy.serversInfo.Unlock()
} }
func (serverInfo *ServerInfo) noticeSuccess(proxy *Proxy) { func (serverInfo *ServerInfo) noticeSuccess(proxy *Proxy) {
now := time.Now() now := time.Now()
serverInfo.Lock() proxy.serversInfo.Lock()
elapsed := now.Sub(serverInfo.lastActionTS) elapsed := now.Sub(serverInfo.lastActionTS)
elapsedMs := elapsed.Nanoseconds() / 1000000 elapsedMs := elapsed.Nanoseconds() / 1000000
if elapsedMs > 0 && elapsed < proxy.timeout { if elapsedMs > 0 && elapsed < proxy.timeout {
serverInfo.rtt.Add(float64(elapsedMs)) serverInfo.rtt.Add(float64(elapsedMs))
} }
serverInfo.Unlock() proxy.serversInfo.Unlock()
} }