Skip to content

Commit

Permalink
feat: add retry on connection restart
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Apr 18, 2021
1 parent ead16a3 commit 801ce07
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cmd/config.go
Expand Up @@ -10,8 +10,7 @@ import (

type Config struct {
// Wireguard private key of local peer
PrivateKey string
// configured remote peers (Wireguard public keys)
PrivateKey string
Peers []connection.Peer
StunTurnURLs []*ice.URL
// host:port of the signal server
Expand Down
13 changes: 10 additions & 3 deletions connection/connection.go
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/pion/ice/v2"
log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/iface"
"github.com/wiretrustee/wiretrustee/util"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
"time"
Expand Down Expand Up @@ -156,6 +157,8 @@ func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {

func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {

conn.remoteAuthChannel <- remoteAuth

uFrag, pwd, err := conn.agent.GetLocalUserCredentials()
if err != nil {
return err
Expand All @@ -166,8 +169,6 @@ func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {
return err
}

conn.remoteAuthChannel <- remoteAuth

return nil
}

Expand Down Expand Up @@ -250,8 +251,14 @@ func (conn *Connection) listenOnConnectionStateChanges() error {
}
log.Debugf("connected to peer %s via selected candidate pair %s", conn.Config.RemoteWgKey.String(), pair)
} else if state == ice.ConnectionStateDisconnected || state == ice.ConnectionStateFailed {
err := conn.Restart()
err := util.Retry(15, time.Second, func() error {
return conn.Restart()
}, func(err error) {
log.Warnf("failed restarting connection, retrying ... %s", err)
})

if err != nil {
log.Errorf("failed restarting connection %s", err)
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion signal/client.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/signal/proto"
"github.com/wiretrustee/wiretrustee/util"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewClient(addr string, ctx context.Context) (*Client, error) {
func (client *Client) Receive(key string, msgHandler func(msg *proto.Message) error) {
client.connWg.Add(1)
go func() {
err := Retry(15, time.Second, func() error {
err := util.Retry(15, time.Second, func() error {
return client.connect(key, msgHandler)
}, func(err error) {
log.Warnf("disconnected from the Signal Exchange due to an error %s. Retrying ... ", err)
Expand Down
2 changes: 1 addition & 1 deletion signal/retry.go → util/retry.go
@@ -1,4 +1,4 @@
package signal
package util

import (
"math/rand"
Expand Down

0 comments on commit 801ce07

Please sign in to comment.