fix: do Connection.Open() in goroutine to avoid blocking other peers

This commit is contained in:
braginini
2021-04-16 15:26:10 +02:00
parent 3d8233034d
commit b86c0c716c
2 changed files with 12 additions and 10 deletions

View File

@@ -82,7 +82,7 @@ func (conn *Connection) Open() error {
return err return err
} }
// create an ice Agent that will be responsible for negotiating and establishing actual peer-to-peer connection // create an ice.Agent that will be responsible for negotiating and establishing actual peer-to-peer connection
conn.agent, err = ice.NewAgent(&ice.AgentConfig{ conn.agent, err = ice.NewAgent(&ice.AgentConfig{
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4}, NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4},
Urls: conn.Config.StunTurnURLS, Urls: conn.Config.StunTurnURLS,
@@ -193,8 +193,8 @@ func (conn *Connection) signalCredentials() error {
return nil return nil
} }
// listenOnLocalCandidates registers callback of an ICE Agent to new local connection candidates and // listenOnLocalCandidates registers callback of an ICE Agent to receive new local connection candidates and then
// signal them to the remote peer // signals them to the remote peer
func (conn *Connection) listenOnLocalCandidates() error { func (conn *Connection) listenOnLocalCandidates() error {
err := conn.agent.OnCandidate(func(candidate ice.Candidate) { err := conn.agent.OnCandidate(func(candidate ice.Candidate) {
if candidate != nil { if candidate != nil {
@@ -238,8 +238,8 @@ func (conn *Connection) listenOnConnectionStateChanges() error {
return nil return nil
} }
// createWireguardProxy opens connection to the local Wireguard instance (proxy) and sets peer endpoint of Wireguard to point // createWireguardProxy opens connection to a local Wireguard instance (proxy) and sets Wireguard's peer endpoint to point
// to the local address of a proxy // to a local address of a proxy
func (conn *Connection) createWireguardProxy() (*net.Conn, error) { func (conn *Connection) createWireguardProxy() (*net.Conn, error) {
wgConn, err := net.Dial("udp", conn.Config.WgListenAddr) wgConn, err := net.Dial("udp", conn.Config.WgListenAddr)
if err != nil { if err != nil {

View File

@@ -89,11 +89,13 @@ func (e *Engine) Start(privateKey string, peers []string) error {
conn := NewConnection(*connConfig, signalCandidate, signalOffer, signalAnswer) conn := NewConnection(*connConfig, signalCandidate, signalOffer, signalAnswer)
e.conns[myPubKey] = conn e.conns[myPubKey] = conn
err = conn.Open() go func() {
if err != nil { err = conn.Open()
log.Errorf("error openning connection to a remote peer %s %s", remoteKey.String(), err.Error()) if err != nil {
return err log.Errorf("error openning connection to a remote peer %s %s", remoteKey.String(), err.Error())
} //todo
}
}()
} }
return nil return nil