Revert "Avoid allocation storing last active time"

This reverts commit edb69295c0.

In that commit, active time was changed from time.Time to
Unix time in order to avoid allocations. Unfortunately, that
has the side effect of discarding the monotonic component of
time.Time, and therefore makes our code vulnerable to stepping
of the system clock.

Fixes #697
This commit is contained in:
Juliusz Chroboczek
2024-05-26 06:59:13 -07:00
committed by Paul Wells
parent 899594c008
commit 7e44037480
2 changed files with 10 additions and 10 deletions
+8 -8
View File
@@ -31,8 +31,8 @@ type candidateBase struct {
resolvedAddr net.Addr
lastSent atomic.Int64
lastReceived atomic.Int64
lastSent atomic.Value
lastReceived atomic.Value
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 := c.lastReceived.Load(); lastReceived != 0 {
return time.Unix(0, lastReceived)
if lastReceived, ok := c.lastReceived.Load().(time.Time); ok {
return lastReceived
}
return time.Time{}
}
func (c *candidateBase) setLastReceived(t time.Time) {
c.lastReceived.Store(t.UnixNano())
c.lastReceived.Store(t)
}
// LastSent returns a time.Time indicating the last time
// this candidate was sent
func (c *candidateBase) LastSent() time.Time {
if lastSent := c.lastSent.Load(); lastSent != 0 {
return time.Unix(0, lastSent)
if lastSent, ok := c.lastSent.Load().(time.Time); ok {
return lastSent
}
return time.Time{}
}
func (c *candidateBase) setLastSent(t time.Time) {
c.lastSent.Store(t.UnixNano())
c.lastSent.Store(t)
}
func (c *candidateBase) seen(outbound bool) {
+2 -2
View File
@@ -186,7 +186,7 @@ func TestCandidateLastSent(t *testing.T) {
require.Equal(t, candidate.LastSent(), time.Time{})
now := time.Now()
candidate.setLastSent(now)
require.EqualValues(t, 0, now.Sub(candidate.LastSent()))
require.Equal(t, candidate.LastSent(), now)
}
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.EqualValues(t, 0, now.Sub(candidate.LastReceived()))
require.Equal(t, candidate.LastReceived(), now)
}
func TestCandidateFoundation(t *testing.T) {