Skip to content

Commit

Permalink
feat: atomic boolean check for isClosed
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishKumar4 committed Dec 21, 2023
1 parent ab72dae commit c865b6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
11 changes: 8 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Agent struct {

// State for closing
done chan struct{}
closed atomic.Bool // Atomic bools are much faster than channels
taskLoopDone chan struct{}
err atomicx.Error

Expand Down Expand Up @@ -170,10 +171,13 @@ func (a *Agent) getAfterRunFn() []func(context.Context) {
}

func (a *Agent) ok() error {
select {
case <-a.done:
// select {
// case <-a.done:
// return a.getErr()
// default:
// }
if a.closed.Load() {
return a.getErr()
default:
}
return nil
}
Expand Down Expand Up @@ -936,6 +940,7 @@ func (a *Agent) Close() error {

a.removeUfragFromMux()

a.closed.Store(true)
close(a.done)
<-a.taskLoopDone
return nil
Expand Down
15 changes: 9 additions & 6 deletions udp_muxed_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type udpMuxedConn struct {
// Channel holding incoming packets
buf *packetio.Buffer
closedChan chan struct{}
closed atomic.Bool
closeOnce sync.Once
mu sync.Mutex

Expand Down Expand Up @@ -131,17 +132,19 @@ func (c *udpMuxedConn) Close() error {
c.closeOnce.Do(func() {
err = c.buf.Close()
close(c.closedChan)
c.closed.Store(true)
})
return err
}

func (c *udpMuxedConn) isClosed() bool {
select {
case <-c.closedChan:
return true
default:
return false
}
// select {
// case <-c.closedChan:
// return true
// default:
// return false
// }
return c.closed.Load()
}

func (c *udpMuxedConn) getAddresses() []string {
Expand Down

0 comments on commit c865b6a

Please sign in to comment.