NATS driver: try to reconnect if the connection is down
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Adam Štrauch 2021-09-27 00:10:16 +02:00
parent 322ef2a258
commit 0e836b68a8
Signed by: cx
GPG Key ID: 018304FFA8988F8D

View File

@ -3,6 +3,7 @@ package nats_driver
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"github.com/by-cx/lobby/common" "github.com/by-cx/lobby/common"
"github.com/by-cx/lobby/server" "github.com/by-cx/lobby/server"
@ -93,7 +94,14 @@ func (d *Driver) SendDiscoveryPacket(discovery server.Discovery) error {
return fmt.Errorf("sending discovery formating message error: %v", err) return fmt.Errorf("sending discovery formating message error: %v", err)
} }
err = d.nc.Publish(d.NATSDiscoveryChannel, data) err = d.nc.Publish(d.NATSDiscoveryChannel, data)
if err != nil { // In case the connection is down we will try to reconnect
if err != nil && strings.Contains(err.Error(), "connection closed") {
d.nc.Close()
err = d.Init()
if err != nil {
return fmt.Errorf("sending discovery reconnect error: %v", err)
}
} else if err != nil {
return fmt.Errorf("sending discovery error: %v", err) return fmt.Errorf("sending discovery error: %v", err)
} }
return nil return nil