Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Avoid allocation storing last active time" #698

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -400,27 +400,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) {
Expand Down
4 changes: 2 additions & 2 deletions candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ func TestCandidateLastSent(t *testing.T) {
assert.Equal(t, candidate.LastSent(), time.Time{})
now := time.Now()
candidate.setLastSent(now)
assert.EqualValues(t, 0, now.Sub(candidate.LastSent()))
assert.Equal(t, candidate.LastSent(), now)
}

func TestCandidateLastReceived(t *testing.T) {
candidate := candidateBase{}
assert.Equal(t, candidate.LastReceived(), time.Time{})
now := time.Now()
candidate.setLastReceived(now)
assert.EqualValues(t, 0, now.Sub(candidate.LastReceived()))
assert.Equal(t, candidate.LastReceived(), now)
}

func TestCandidateFoundation(t *testing.T) {
Expand Down
Loading