1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

Added connection timeouts

This commit is contained in:
postmannen 2021-12-17 17:28:57 +01:00
parent b02a84f6cc
commit 6399cc427d
2 changed files with 16 additions and 1 deletions

View file

@ -31,6 +31,8 @@ type Configuration struct {
NodeName string
// the address of the message broker
BrokerAddress string
// NatsConnOptTimeout the timeout for trying the connect to nats broker
NatsConnOptTimeout int
// nats connect retry
NatsConnectRetryInterval int
// NatsReconnectJitter in milliseconds
@ -110,6 +112,7 @@ type ConfigurationFromFile struct {
DatabaseFolder *string
NodeName *string
BrokerAddress *string
NatsConnOptTimeout *int
NatsConnectRetryInterval *int
NatsReconnectJitter *int
NatsReconnectJitterTLS *int
@ -157,6 +160,7 @@ func newConfigurationDefaults() Configuration {
HTTPListener: "",
DatabaseFolder: "./var/lib",
BrokerAddress: "127.0.0.1:4222",
NatsConnOptTimeout: 20,
NatsConnectRetryInterval: 10,
NatsReconnectJitter: 100,
NatsReconnectJitterTLS: 1,
@ -233,6 +237,11 @@ func checkConfigValues(cf ConfigurationFromFile) Configuration {
} else {
conf.BrokerAddress = *cf.BrokerAddress
}
if cf.NatsConnOptTimeout == nil {
conf.NatsConnOptTimeout = cd.NatsConnOptTimeout
} else {
conf.NatsConnOptTimeout = *cf.NatsConnOptTimeout
}
if cf.NatsConnectRetryInterval == nil {
conf.NatsConnectRetryInterval = cd.NatsConnectRetryInterval
} else {
@ -422,6 +431,7 @@ func (c *Configuration) CheckFlags() error {
flag.StringVar(&c.DatabaseFolder, "databaseFolder", fc.DatabaseFolder, "folder who contains the database file. Defaults to ./var/lib/. If other folder is used this flag must be specified at startup.")
flag.StringVar(&c.NodeName, "nodeName", fc.NodeName, "some unique string to identify this Edge unit")
flag.StringVar(&c.BrokerAddress, "brokerAddress", fc.BrokerAddress, "the address of the message broker")
flag.IntVar(&c.NatsConnOptTimeout, "natsConnOptTimeout", fc.NatsConnOptTimeout, "default nats client conn timeout")
flag.IntVar(&c.NatsConnectRetryInterval, "natsConnectRetryInterval", fc.NatsConnectRetryInterval, "default nats retry connect interval in seconds.")
flag.IntVar(&c.NatsReconnectJitter, "natsReconnectJitter", fc.NatsReconnectJitter, "default nats ReconnectJitter interval in milliseconds.")
flag.IntVar(&c.NatsReconnectJitterTLS, "natsReconnectJitterTLS", fc.NatsReconnectJitterTLS, "default nats ReconnectJitterTLS interval in seconds.")

View file

@ -91,7 +91,12 @@ func NewServer(c *Configuration, version string) (*server, error) {
for {
var err error
// Setting MaxReconnects to -1 which equals unlimited.
conn, err = nats.Connect(c.BrokerAddress, opt, nats.MaxReconnects(-1), nats.ReconnectJitter(time.Duration(c.NatsReconnectJitter)*time.Millisecond, time.Duration(c.NatsReconnectJitterTLS)*time.Second))
conn, err = nats.Connect(c.BrokerAddress,
opt,
nats.MaxReconnects(-1),
nats.ReconnectJitter(time.Duration(c.NatsReconnectJitter)*time.Millisecond, time.Duration(c.NatsReconnectJitterTLS)*time.Second),
nats.Timeout(time.Second*time.Duration(c.NatsConnOptTimeout)),
)
// If no servers where available, we loop and retry until succesful.
if err != nil {
log.Printf("error: could not connect, waiting %v seconds, and retrying: %v\n", c.NatsConnectRetryInterval, err)