From edb69295c04429931ede6a3df01d49520a1b31dc Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Tue, 30 Apr 2024 06:01:33 -0700 Subject: [PATCH] Avoid allocation storing last active time --- candidate_base.go | 16 ++++++++-------- candidate_test.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/candidate_base.go b/candidate_base.go index 1a60899a..81b54a5c 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -31,8 +31,8 @@ type candidateBase struct { resolvedAddr net.Addr - lastSent atomic.Value - lastReceived atomic.Value + lastSent atomic.Int64 + lastReceived atomic.Int64 conn net.PacketConn currAgent *Agent @@ -409,27 +409,27 @@ func (c *candidateBase) String() string { // LastReceived returns a time.Time indicating the last time // this candidate was received func (c *candidateBase) LastReceived() time.Time { - if lastReceived, ok := c.lastReceived.Load().(time.Time); ok { - return lastReceived + if lastReceived := c.lastReceived.Load(); lastReceived != 0 { + return time.Unix(0, lastReceived) } return time.Time{} } func (c *candidateBase) setLastReceived(t time.Time) { - c.lastReceived.Store(t) + c.lastReceived.Store(t.UnixNano()) } // LastSent returns a time.Time indicating the last time // this candidate was sent func (c *candidateBase) LastSent() time.Time { - if lastSent, ok := c.lastSent.Load().(time.Time); ok { - return lastSent + if lastSent := c.lastSent.Load(); lastSent != 0 { + return time.Unix(0, lastSent) } return time.Time{} } func (c *candidateBase) setLastSent(t time.Time) { - c.lastSent.Store(t) + c.lastSent.Store(t.UnixNano()) } func (c *candidateBase) seen(outbound bool) { diff --git a/candidate_test.go b/candidate_test.go index 04bb17c1..aecbeec0 100644 --- a/candidate_test.go +++ b/candidate_test.go @@ -186,7 +186,7 @@ func TestCandidateLastSent(t *testing.T) { require.Equal(t, candidate.LastSent(), time.Time{}) now := time.Now() candidate.setLastSent(now) - require.Equal(t, candidate.LastSent(), now) + require.EqualValues(t, 0, now.Sub(candidate.LastSent())) } func TestCandidateLastReceived(t *testing.T) { @@ -194,7 +194,7 @@ func TestCandidateLastReceived(t *testing.T) { require.Equal(t, candidate.LastReceived(), time.Time{}) now := time.Now() candidate.setLastReceived(now) - require.Equal(t, candidate.LastReceived(), now) + require.EqualValues(t, 0, now.Sub(candidate.LastReceived())) } func TestCandidateFoundation(t *testing.T) {