Skip to content

Commit

Permalink
fix: do Connection.Open() in goroutine to avoid blocking other peers
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Apr 16, 2021
1 parent 3d82330 commit b86c0c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions connection/connection.go
Expand Up @@ -82,7 +82,7 @@ func (conn *Connection) Open() error {
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{
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4},
Urls: conn.Config.StunTurnURLS,
Expand Down Expand Up @@ -193,8 +193,8 @@ func (conn *Connection) signalCredentials() error {
return nil
}

// listenOnLocalCandidates registers callback of an ICE Agent to new local connection candidates and
// signal them to the remote peer
// listenOnLocalCandidates registers callback of an ICE Agent to receive new local connection candidates and then
// signals them to the remote peer
func (conn *Connection) listenOnLocalCandidates() error {
err := conn.agent.OnCandidate(func(candidate ice.Candidate) {
if candidate != nil {
Expand Down Expand Up @@ -238,8 +238,8 @@ func (conn *Connection) listenOnConnectionStateChanges() error {
return nil
}

// createWireguardProxy opens connection to the local Wireguard instance (proxy) and sets peer endpoint of Wireguard to point
// to the local address of a proxy
// createWireguardProxy opens connection to a local Wireguard instance (proxy) and sets Wireguard's peer endpoint to point
// to a local address of a proxy
func (conn *Connection) createWireguardProxy() (*net.Conn, error) {
wgConn, err := net.Dial("udp", conn.Config.WgListenAddr)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions connection/engine.go
Expand Up @@ -89,11 +89,13 @@ func (e *Engine) Start(privateKey string, peers []string) error {
conn := NewConnection(*connConfig, signalCandidate, signalOffer, signalAnswer)
e.conns[myPubKey] = conn

err = conn.Open()
if err != nil {
log.Errorf("error openning connection to a remote peer %s %s", remoteKey.String(), err.Error())
return err
}
go func() {
err = conn.Open()
if err != nil {
log.Errorf("error openning connection to a remote peer %s %s", remoteKey.String(), err.Error())
//todo
}
}()
}

return nil
Expand Down

0 comments on commit b86c0c7

Please sign in to comment.