mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-04 12:03:41 +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 eosClient *eos.API
|
||||||
var eosClientCtx = context.Background()
|
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() {
|
func run() {
|
||||||
|
|
||||||
// Create done and interrupt channels.
|
// Create done and interrupt channels.
|
||||||
|
|
@ -38,19 +84,7 @@ func run() {
|
||||||
|
|
||||||
// Spawn message read loop in another thread.
|
// Spawn message read loop in another thread.
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
readerLoop()
|
||||||
err := shClient.Read()
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err.Error())
|
|
||||||
|
|
||||||
// Bail out on socket read error.
|
|
||||||
if err.Type == shipclient.ErrSockRead {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
shClient.Close()
|
|
||||||
|
|
||||||
// Reader exited. signal that we are done.
|
// Reader exited. signal that we are done.
|
||||||
done <- true
|
done <- true
|
||||||
|
|
@ -149,20 +183,6 @@ func main() {
|
||||||
shClient.BlockHandler = processBlock
|
shClient.BlockHandler = processBlock
|
||||||
shClient.TraceHandler = processTraces
|
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 the application
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue