feat: add retry on connection restart

This commit is contained in:
braginini 2021-04-18 15:54:10 +02:00
parent ead16a35c9
commit 801ce0730d
4 changed files with 14 additions and 7 deletions

View File

@ -10,8 +10,7 @@ import (
type Config struct { type Config struct {
// Wireguard private key of local peer // Wireguard private key of local peer
PrivateKey string PrivateKey string
// configured remote peers (Wireguard public keys)
Peers []connection.Peer Peers []connection.Peer
StunTurnURLs []*ice.URL StunTurnURLs []*ice.URL
// host:port of the signal server // host:port of the signal server

View File

@ -5,6 +5,7 @@ import (
"github.com/pion/ice/v2" "github.com/pion/ice/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/iface" "github.com/wiretrustee/wiretrustee/iface"
"github.com/wiretrustee/wiretrustee/util"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net" "net"
"time" "time"
@ -156,6 +157,8 @@ func (conn *Connection) OnAnswer(remoteAuth IceCredentials) error {
func (conn *Connection) OnOffer(remoteAuth IceCredentials) error { func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {
conn.remoteAuthChannel <- remoteAuth
uFrag, pwd, err := conn.agent.GetLocalUserCredentials() uFrag, pwd, err := conn.agent.GetLocalUserCredentials()
if err != nil { if err != nil {
return err return err
@ -166,8 +169,6 @@ func (conn *Connection) OnOffer(remoteAuth IceCredentials) error {
return err return err
} }
conn.remoteAuthChannel <- remoteAuth
return nil return nil
} }
@ -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) log.Debugf("connected to peer %s via selected candidate pair %s", conn.Config.RemoteWgKey.String(), pair)
} else if state == ice.ConnectionStateDisconnected || state == ice.ConnectionStateFailed { } 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 { if err != nil {
log.Errorf("failed restarting connection %s", err)
return return
} }
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wiretrustee/wiretrustee/signal/proto" "github.com/wiretrustee/wiretrustee/signal/proto"
"github.com/wiretrustee/wiretrustee/util"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive" "google.golang.org/grpc/keepalive"
@ -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) { func (client *Client) Receive(key string, msgHandler func(msg *proto.Message) error) {
client.connWg.Add(1) client.connWg.Add(1)
go func() { go func() {
err := Retry(15, time.Second, func() error { err := util.Retry(15, time.Second, func() error {
return client.connect(key, msgHandler) return client.connect(key, msgHandler)
}, func(err error) { }, func(err error) {
log.Warnf("disconnected from the Signal Exchange due to an error %s. Retrying ... ", err) log.Warnf("disconnected from the Signal Exchange due to an error %s. Retrying ... ", err)

View File

@ -1,4 +1,4 @@
package signal package util
import ( import (
"math/rand" "math/rand"