diff --git a/candidate_base.go b/candidate_base.go index b5cb216..2d7d664 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -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 }