Quellcode durchsuchen

reconnect to nats periodically

Tobias von Dewitz vor 6 Jahren
Ursprung
Commit
dd4e9a3907
1 geänderte Dateien mit 12 neuen und 0 gelöschten Zeilen
  1. 12 0
      main.go

+ 12 - 0
main.go

@@ -40,6 +40,7 @@ var (
 	natsPassword          = flag.String("nats-password", "", "The password for NATS authentication")
 	natsQueue             = flag.String("nats-queue", "requests", "The NATS queue name")
 	natsCA                = flag.String("nats-ca", "", "CA chain for NATS TLS")
+	reconnectToNatsAfter  = flag.Duration("reconnect-to-nats-after", 0, "reconnect to nats after this long periodically")
 	sleepFor              = flag.Duration("sleep", 0, "Sleep this long between sending data (only when replaying a file)")
 	requestsFile          = flag.String("requests", "", "CSV file containing requests (IP and URL)")
 	protocol              = flag.String("protocol", "http", "which protocol to parse: http or ajp13")
@@ -179,6 +180,17 @@ func main() {
 		log.Fatal(err)
 	}
 
+	// reconnect to nats periodically
+	//
+	if *reconnectToNatsAfter > 0 {
+		go func(interval time.Duration) {
+			for range time.Tick(interval) {
+				natsEC.Conn.Close()
+				natsJSONEC.Conn.Close()
+			}
+		}(*reconnectToNatsAfter)
+	}
+
 	go natsWatchdog(natsErrorChan)
 
 	// RPC