mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
main.go: refactor read loop to do connect (and reconnect indefinitely if connection drops)
This commit is contained in:
parent
107f3c384c
commit
c1264b2dc1
1 changed files with 47 additions and 27 deletions
74
main.go
74
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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue