From 0e836b68a87c61f9ef80c45936fa322acfe8836c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Mon, 27 Sep 2021 00:10:16 +0200 Subject: [PATCH] NATS driver: try to reconnect if the connection is down --- nats_driver/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nats_driver/main.go b/nats_driver/main.go index 8135112..2820965 100644 --- a/nats_driver/main.go +++ b/nats_driver/main.go @@ -3,6 +3,7 @@ package nats_driver import ( "encoding/json" "fmt" + "strings" "github.com/by-cx/lobby/common" "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) } 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 nil