1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-31 01:24:31 +00:00

added config flags for jitter

This commit is contained in:
postmannen 2021-12-16 11:01:01 +01:00
parent e3b535665a
commit b02a84f6cc
2 changed files with 24 additions and 1 deletions

View file

@ -33,6 +33,10 @@ type Configuration struct {
BrokerAddress string
// nats connect retry
NatsConnectRetryInterval int
// NatsReconnectJitter in milliseconds
NatsReconnectJitter int
// NatsReconnectJitterTLS in seconds
NatsReconnectJitterTLS int
// The number of the profiling port
ProfilingPort string
// host and port for prometheus listener, e.g. localhost:2112
@ -107,6 +111,8 @@ type ConfigurationFromFile struct {
NodeName *string
BrokerAddress *string
NatsConnectRetryInterval *int
NatsReconnectJitter *int
NatsReconnectJitterTLS *int
ProfilingPort *string
PromHostAndPort *string
DefaultMessageTimeout *int
@ -152,6 +158,8 @@ func newConfigurationDefaults() Configuration {
DatabaseFolder: "./var/lib",
BrokerAddress: "127.0.0.1:4222",
NatsConnectRetryInterval: 10,
NatsReconnectJitter: 100,
NatsReconnectJitterTLS: 1,
ProfilingPort: "",
PromHostAndPort: "",
DefaultMessageTimeout: 10,
@ -230,6 +238,16 @@ func checkConfigValues(cf ConfigurationFromFile) Configuration {
} else {
conf.NatsConnectRetryInterval = *cf.NatsConnectRetryInterval
}
if cf.NatsReconnectJitter == nil {
conf.NatsReconnectJitter = cd.NatsReconnectJitter
} else {
conf.NatsReconnectJitter = *cf.NatsReconnectJitter
}
if cf.NatsReconnectJitterTLS == nil {
conf.NatsReconnectJitterTLS = cd.NatsReconnectJitterTLS
} else {
conf.NatsReconnectJitterTLS = *cf.NatsReconnectJitterTLS
}
if cf.ProfilingPort == nil {
conf.ProfilingPort = cd.ProfilingPort
} else {
@ -405,6 +423,8 @@ func (c *Configuration) CheckFlags() error {
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.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.")
flag.StringVar(&c.ProfilingPort, "profilingPort", fc.ProfilingPort, "The number of the profiling port")
flag.StringVar(&c.PromHostAndPort, "promHostAndPort", fc.PromHostAndPort, "host and port for prometheus listener, e.g. localhost:2112")
flag.IntVar(&c.DefaultMessageTimeout, "defaultMessageTimeout", fc.DefaultMessageTimeout, "default message timeout in seconds. This can be overridden on the message level")

View file

@ -91,7 +91,7 @@ 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))
conn, err = nats.Connect(c.BrokerAddress, opt, nats.MaxReconnects(-1), nats.ReconnectJitter(time.Duration(c.NatsReconnectJitter)*time.Millisecond, time.Duration(c.NatsReconnectJitterTLS)*time.Second))
// 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)
@ -102,6 +102,9 @@ func NewServer(c *Configuration, version string) (*server, error) {
break
}
log.Printf(" * conn.Opts.ReconnectJitterTLS: %v\n", conn.Opts.ReconnectJitterTLS)
log.Printf(" * conn.Opts.ReconnectJitter: %v\n", conn.Opts.ReconnectJitter)
// Prepare the connection to the Steward socket file
// Check if socket folder exists, if not create it