Improve candidate base locking mechanism

Use atomic values for lastReceived and lastSenti. The lock around
resolvedAddr and conn was not being used, so I removed it
This commit is contained in:
Hugo Arregui
2019-08-16 00:07:02 +00:00
committed by Hugo Arregui
parent aab2d5ba84
commit 425c6d9ef8
+8 -21
View File
@@ -3,7 +3,7 @@ package ice
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/pion/logging"
@@ -22,12 +22,11 @@ type candidateBase struct {
resolvedAddr *net.UDPAddr
lock sync.RWMutex
lastSent time.Time
lastReceived time.Time
lastSent atomic.Value
lastReceived atomic.Value
conn net.PacketConn
currAgent *Agent
conn net.PacketConn
closeCh chan struct{}
closedCh chan struct{}
}
@@ -140,8 +139,6 @@ func handleInboundCandidateMsg(c Candidate, buffer []byte, srcAddr net.Addr, log
// close stops the recvLoop
func (c *candidateBase) close() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.conn != nil {
// Unblock recvLoop
close(c.closeCh)
@@ -197,29 +194,21 @@ func (c *candidateBase) String() string {
// LastReceived returns a time.Time indicating the last time
// this candidate was received
func (c *candidateBase) LastReceived() time.Time {
c.lock.RLock()
defer c.lock.RUnlock()
return c.lastReceived
return c.lastReceived.Load().(time.Time)
}
func (c *candidateBase) setLastReceived(t time.Time) {
c.lock.Lock()
defer c.lock.Unlock()
c.lastReceived = t
c.lastReceived.Store(t)
}
// LastSent returns a time.Time indicating the last time
// this candidate was sent
func (c *candidateBase) LastSent() time.Time {
c.lock.RLock()
defer c.lock.RUnlock()
return c.lastSent
return c.lastSent.Load().(time.Time)
}
func (c *candidateBase) setLastSent(t time.Time) {
c.lock.Lock()
defer c.lock.Unlock()
c.lastSent = t
c.lastSent.Store(t)
}
func (c *candidateBase) seen(outbound bool) {
@@ -231,8 +220,6 @@ func (c *candidateBase) seen(outbound bool) {
}
func (c *candidateBase) addr() *net.UDPAddr {
c.lock.Lock()
defer c.lock.Unlock()
return c.resolvedAddr
}