Skip to content

Commit

Permalink
feat: reduce time.now calls and use atomic int instead of value
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishKumar4 committed Dec 13, 2023
1 parent 81df285 commit ab72dae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
44 changes: 31 additions & 13 deletions candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"github.com/pion/stun/v2"
)

var (

Check failure on line 21 in candidate_base.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)
startTime = time.Now()

Check failure on line 22 in candidate_base.go

View workflow job for this annotation

GitHub Actions / lint / Go

startTime is a global variable (gochecknoglobals)
)

type candidateBase struct {
id string
networkType NetworkType
Expand All @@ -31,9 +35,9 @@ type candidateBase struct {

resolvedAddr net.Addr

lastSent atomic.Value
lastReceived atomic.Value
conn net.PacketConn
lastSentAfter atomic.Int64
lastReceivedAfter atomic.Int64
conn net.PacketConn

currAgent *Agent
closeCh chan struct{}
Expand Down Expand Up @@ -207,6 +211,7 @@ func (c *candidateBase) start(a *Agent, conn net.PacketConn, initializedCh <-cha
c.conn = conn
c.closeCh = make(chan struct{})
c.closedCh = make(chan struct{})
startTime = time.Now()

go c.recvLoop(initializedCh)
}
Expand Down Expand Up @@ -400,34 +405,47 @@ 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, ok := c.lastReceived.Load().(time.Time); ok {
// return lastReceived
// }
// return time.Time{}
diff := c.lastReceivedAfter.Load()
if diff > 0 {
return startTime.Add(time.Duration(diff))
}

return time.Time{}
}

func (c *candidateBase) setLastReceived(t time.Time) {
c.lastReceived.Store(t)
func (c *candidateBase) setLastReceived() {
c.lastReceivedAfter.Store(time.Since(startTime).Nanoseconds())
}

// 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, ok := c.lastSent.Load().(time.Time); ok {
// return lastSent
// }
// return time.Time{}

diff := c.lastSentAfter.Load()
if diff > 0 {
return startTime.Add(time.Duration(diff))
}

return time.Time{}
}

func (c *candidateBase) setLastSent(t time.Time) {
c.lastSent.Store(t)
func (c *candidateBase) setLastSent() {
c.lastSentAfter.Store(time.Since(startTime).Nanoseconds())
}

func (c *candidateBase) seen(outbound bool) {
if outbound {
c.setLastSent(time.Now())
c.setLastSent() //time.Now())

Check failure on line 446 in candidate_base.go

View workflow job for this annotation

GitHub Actions / lint / Go

commentFormatting: put a space between `//` and comment text (gocritic)
} else {
c.setLastReceived(time.Now())
c.setLastReceived() //time.Now())

Check failure on line 448 in candidate_base.go

View workflow job for this annotation

GitHub Actions / lint / Go

commentFormatting: put a space between `//` and comment text (gocritic)
}
}

Expand Down
10 changes: 6 additions & 4 deletions candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,18 @@ func TestCandidateLastSent(t *testing.T) {
candidate := candidateBase{}
assert.Equal(t, candidate.LastSent(), time.Time{})
now := time.Now()
candidate.setLastSent(now)
assert.Equal(t, candidate.LastSent(), now)
candidate.setLastSent()
// assert.Equal(t, candidate.LastSent(), now)
assert.WithinDuration(t, candidate.LastSent(), now, time.Millisecond)
}

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

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

0 comments on commit ab72dae

Please sign in to comment.