mirror of
https://github.com/netbirdio/ice.git
synced 2026-05-22 17:10:58 -07:00
Invoke handlers from their own Goroutines
This commit is contained in:
@@ -358,7 +358,13 @@ func NewAgent(config *AgentConfig) (*Agent, error) { //nolint:gocognit
|
||||
}
|
||||
|
||||
go a.taskLoop()
|
||||
a.startOnConnectionStateChangeRoutine()
|
||||
|
||||
// CandidatePair and ConnectionState are usually changed at once.
|
||||
// Blocking one by the other one causes deadlock.
|
||||
// Hence, we call handlers from independent Goroutines.
|
||||
go a.candidatePairRoutine()
|
||||
go a.connectionStateRoutine()
|
||||
go a.candidateRoutine()
|
||||
|
||||
// Restart is also used to initialize the agent for the first time
|
||||
if err := a.Restart(config.LocalUfrag, config.LocalPwd); err != nil {
|
||||
|
||||
+15
-34
@@ -41,39 +41,20 @@ func (a *Agent) onConnectionStateChange(s ConnectionState) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) startOnConnectionStateChangeRoutine() {
|
||||
go func() {
|
||||
for {
|
||||
// CandidatePair and ConnectionState are usually changed at once.
|
||||
// Blocking one by the other one causes deadlock.
|
||||
p, isOpen := <-a.chanCandidatePair
|
||||
if !isOpen {
|
||||
return
|
||||
}
|
||||
a.onSelectedCandidatePairChange(p)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case s, isOpen := <-a.chanState:
|
||||
if !isOpen {
|
||||
for c := range a.chanCandidate {
|
||||
a.onCandidate(c)
|
||||
}
|
||||
return
|
||||
}
|
||||
go a.onConnectionStateChange(s)
|
||||
func (a *Agent) candidatePairRoutine() {
|
||||
for p := range a.chanCandidatePair {
|
||||
a.onSelectedCandidatePairChange(p)
|
||||
}
|
||||
}
|
||||
|
||||
case c, isOpen := <-a.chanCandidate:
|
||||
if !isOpen {
|
||||
for s := range a.chanState {
|
||||
go a.onConnectionStateChange(s)
|
||||
}
|
||||
return
|
||||
}
|
||||
a.onCandidate(c)
|
||||
}
|
||||
}
|
||||
}()
|
||||
func (a *Agent) connectionStateRoutine() {
|
||||
for s := range a.chanState {
|
||||
go a.onConnectionStateChange(s)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Agent) candidateRoutine() {
|
||||
for c := range a.chanCandidate {
|
||||
a.onCandidate(c)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user