From: Bernhard Tittelbach Date: Mon, 25 Nov 2013 02:24:49 +0000 (+0000) Subject: serial reconnect with exponential backoff X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=a95d80b14c57d801db6b699505ee93297f94179c;p=svn42.git serial reconnect with exponential backoff --- diff --git a/go/uc_sensor_node_zmq/main.go b/go/uc_sensor_node_zmq/main.go index bd55297..2e3c0de 100644 --- a/go/uc_sensor_node_zmq/main.go +++ b/go/uc_sensor_node_zmq/main.go @@ -4,6 +4,8 @@ package main import ( "flag" + "time" + zmq "github.com/vaughan0/go-zmq" ) @@ -16,6 +18,8 @@ var ( enable_debug_ bool ) +const exponential_backof_activation_threshold int64 = 4 + func init() { flag.StringVar(&pub_addr, "brokeraddr", "tcp://torwaechter.realraum.at:4243", "zmq address to send stuff to") flag.StringVar(&tty_dev_, "ttydev", "/dev/ttyACM0", "path do tty uc device") @@ -24,6 +28,21 @@ func init() { flag.Parse() } +func ConnectSerialToZMQ(pub_sock *zmq.Socket) { + defer func() { + if x:= recover(); x != nil { Syslog_.Println(x) } + }() + + serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_) + if err != nil { panic(err) } + defer close(serial_wr) + + for incoming_ser_line := range(serial_rd) { + Syslog_.Printf("%s",incoming_ser_line) + if err := pub_sock.Send(incoming_ser_line); err != nil { Syslog_.Println(err.Error()) } + } +} + func main() { zmqctx, pub_sock := ZmqsInit(pub_addr) if pub_sock == nil { panic("zmq socket creation failed") } @@ -37,12 +56,15 @@ func main() { Syslog_.Print("started") } - serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_) - if err != nil { panic(err) } - defer close(serial_wr) - - for incoming_ser_line := range(serial_rd) { - Syslog_.Printf("%s",incoming_ser_line) - if err := pub_sock.Send(incoming_ser_line); err != nil { panic(err) } + var backoff_exp uint32 = 0 + for { + start_time := time.Now().Unix() + ConnectSerialToZMQ(pub_sock) + run_time := time.Now().Unix() - start_time + if run_time > exponential_backof_activation_threshold { + backoff_exp = 0 + } + time.Sleep(150*(1 << backoff_exp) * time.Millisecond) + if backoff_exp < 12 { backoff_exp++ } } }