Skip to content

Commit

Permalink
Remove afterRun from Task Loop
Browse files Browse the repository at this point in the history
Not needed anymore since we have Notification Queues
  • Loading branch information
Sean-Der committed Mar 23, 2024
1 parent d17be4d commit b36d332
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ type bindingRequest struct {

// Agent represents the ICE agent
type Agent struct {
chanTask chan task
afterRunFn []func(ctx context.Context)
muAfterRun sync.Mutex
chanTask chan task

onConnectionStateChangeHdlr atomic.Value // func(ConnectionState)
onSelectedCandidatePairChangeHdlr atomic.Value // func(Candidate, Candidate)
Expand Down Expand Up @@ -154,21 +152,6 @@ type task struct {
done chan struct{}
}

// afterRun registers function to be run after the task.
func (a *Agent) afterRun(f func(context.Context)) {
a.muAfterRun.Lock()
a.afterRunFn = append(a.afterRunFn, f)
a.muAfterRun.Unlock()
}

func (a *Agent) getAfterRunFn() []func(context.Context) {
a.muAfterRun.Lock()
defer a.muAfterRun.Unlock()
fns := a.afterRunFn
a.afterRunFn = nil
return fns
}

func (a *Agent) ok() error {
select {
case <-a.done:
Expand Down Expand Up @@ -202,18 +185,6 @@ func (a *Agent) run(ctx context.Context, t func(context.Context, *Agent)) error

// taskLoop handles registered tasks and agent close.
func (a *Agent) taskLoop() {
after := func() {
for {
// Get and run func registered by afterRun().
fns := a.getAfterRunFn()
if len(fns) == 0 {
break
}
for _, fn := range fns {
fn(a.context())
}
}
}
defer func() {
a.deleteAllCandidates()
a.startedFn()
Expand All @@ -225,7 +196,10 @@ func (a *Agent) taskLoop() {
a.closeMulticastConn()
a.updateConnectionState(ConnectionStateClosed)

after()
a.gatherCandidateCancel()
if a.gatherCandidateDone != nil {
<-a.gatherCandidateDone
}

close(a.taskLoopDone)
}()
Expand All @@ -237,7 +211,6 @@ func (a *Agent) taskLoop() {
case t := <-a.chanTask:
t.fn(a.context(), a)
close(t.done)
after()
}
}
}
Expand Down Expand Up @@ -906,12 +879,6 @@ func (a *Agent) Close() error {
return err
}

a.afterRun(func(context.Context) {
a.gatherCandidateCancel()
if a.gatherCandidateDone != nil {
<-a.gatherCandidateDone
}
})
a.err.Store(ErrClosed)

a.removeUfragFromMux()
Expand Down

0 comments on commit b36d332

Please sign in to comment.