func connectToNatsServer(logger lager.Logger, c *config.Config, startMsg chan<- struct{}) *nats.Conn { var natsClient *nats.Conn var natsHost atomic.Value var err error options := natsOptions(logger, c, &natsHost, startMsg) attempts := 3 for attempts > 0 { natsClient, err = options.Connect() if err == nil { break } else { attempts-- time.Sleep(100 * time.Millisecond) } } if err != nil { logger.Fatal("nats-connection-error", err) } var natsHostStr string natsUrl, err := url.Parse(natsClient.ConnectedUrl()) if err == nil { natsHostStr = natsUrl.Host } logger.Info("Successfully-connected-to-nats", lager.Data{"host": natsHostStr}) natsHost.Store(natsHostStr) return natsClient }
func reconnectedHandler(nc *nats.Conn) { log.Fatalf("connection %q reconnected to NATS Server at %q", nc.Opts.Name, nc.ConnectedUrl()) }