func connectToNatsServer(logger lager.Logger, c *config.Config) *nats.Conn { var natsClient *nats.Conn var err error natsServers := c.NatsServers() attempts := 3 for attempts > 0 { options := nats.DefaultOptions options.Servers = natsServers options.PingInterval = c.NatsClientPingInterval options.ClosedCB = func(conn *nats.Conn) { logger.Fatal("nats-connection-closed", errors.New("unexpected close"), lager.Data{"last_error": conn.LastError()}) } natsClient, err = options.Connect() if err == nil { break } else { attempts-- time.Sleep(100 * time.Millisecond) } } if err != nil { logger.Fatal("nats-connection-error", err) } return natsClient }
func connectToNatsServer(c *config.Config, logger *steno.Logger) yagnats.NATSConn { var natsClient yagnats.NATSConn var err error natsServers := c.NatsServers() attempts := 3 for attempts > 0 { natsClient, err = yagnats.Connect(natsServers) if err == nil { break } else { attempts-- time.Sleep(100 * time.Millisecond) } } if err != nil { logger.Errorf("Error connecting to NATS: %s\n", err) os.Exit(1) } natsClient.AddClosedCB(func(conn *nats.Conn) { logger.Errorf("Close on NATS client. nats.Conn: %+v", *conn) os.Exit(1) }) return natsClient }
func connectToNatsServer(logger lager.Logger, c *config.Config) yagnats.NATSConn { var natsClient yagnats.NATSConn var err error natsServers := c.NatsServers() attempts := 3 for attempts > 0 { natsClient, err = yagnats.Connect(natsServers) if err == nil { break } else { attempts-- time.Sleep(100 * time.Millisecond) } } if err != nil { logger.Fatal("nats-connection-error", err) } natsClient.AddClosedCB(func(conn *nats.Conn) { logger.Fatal("nats-connection-closed", errors.New("unexpected close"), lager.Data{"connection": *conn}) }) return natsClient }