From c1264b2dc1255101ca77d20aecbd919c60677686 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 2 Feb 2022 15:19:38 +0100 Subject: [PATCH] main.go: refactor read loop to do connect (and reconnect indefinitely if connection drops) --- main.go | 74 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 2740508..056b790 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,52 @@ var shClient *shipclient.ShipClient var eosClient *eos.API var eosClientCtx = context.Background() + +// Reader states +const RS_CONNECT = 1 +const RS_READ = 2 + +func readerLoop() { + + state := RS_CONNECT + + for { + switch state { + case RS_CONNECT : + log.Printf("Connecting to ship at: %s", config.ShipApi) + err := shClient.Connect(config.ShipApi) + if err != nil { + log.Println(err) + log.Printf("Trying again in 5 seconds ....") + time.Sleep(5 * time.Second) + break; + } + + err = shClient.SendBlocksRequest() + if err != nil { + log.Println(err) + break + } + + // Connected + log.Printf("Connected, Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock) + state = RS_READ + case RS_READ : + err := shClient.Read() + if err != nil { + log.Print(err.Error()) + + // Reconnect + if err.Type == shipclient.ErrSockRead { + state = RS_CONNECT + } + } + } + } + + shClient.Close() +} + func run() { // Create done and interrupt channels. @@ -38,19 +84,7 @@ func run() { // Spawn message read loop in another thread. go func() { - for { - err := shClient.Read() - if err != nil { - log.Print(err.Error()) - - // Bail out on socket read error. - if err.Type == shipclient.ErrSockRead { - break - } - } - } - - shClient.Close() + readerLoop() // Reader exited. signal that we are done. done <- true @@ -149,20 +183,6 @@ func main() { shClient.BlockHandler = processBlock shClient.TraceHandler = processTraces - err = shClient.Connect(config.ShipApi) - if err != nil { - log.Println(err) - return - } - - err = shClient.SendBlocksRequest() - if err != nil { - log.Println(err) - return - } - - log.Printf("Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock) - // Run the application run() }