mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-03 11:53:41 +02:00
cmd/main/main.go: simplify ship (re)connection by removing the statemachine and using a simple bool instead.
This commit is contained in:
parent
93479506b6
commit
c4167d2b92
1 changed files with 27 additions and 37 deletions
|
|
@ -34,24 +34,17 @@ var conf *config.Config
|
||||||
|
|
||||||
var shClient *shipclient.Stream
|
var shClient *shipclient.Stream
|
||||||
|
|
||||||
// Reader states
|
var running bool = false
|
||||||
const (
|
|
||||||
RS_CONNECT = 1
|
|
||||||
RS_READ = 2
|
|
||||||
)
|
|
||||||
|
|
||||||
func readerLoop() {
|
func readerLoop() {
|
||||||
state := RS_CONNECT
|
running = true
|
||||||
var recon_cnt uint = 0
|
recon_cnt := 0
|
||||||
|
|
||||||
for {
|
for running {
|
||||||
switch state {
|
|
||||||
case RS_CONNECT:
|
|
||||||
recon_cnt++
|
recon_cnt++
|
||||||
log.Infof("Connecting to ship at: %s (Try %d)", conf.Ship.Url, recon_cnt)
|
log.Infof("Connecting to ship at: %s (Try %d)", conf.Ship.Url, recon_cnt)
|
||||||
err := shClient.Connect(conf.Ship.Url)
|
if err := shClient.Connect(conf.Ship.Url); err != nil {
|
||||||
if err != nil {
|
log.WithError(err).Error("Failed to connect")
|
||||||
log.Println(err)
|
|
||||||
|
|
||||||
if recon_cnt >= 3 {
|
if recon_cnt >= 3 {
|
||||||
msg := fmt.Sprintf("Failed to connect to ship at '%s'", conf.Ship.Url)
|
msg := fmt.Sprintf("Failed to connect to ship at '%s'", conf.Ship.Url)
|
||||||
|
|
@ -63,24 +56,19 @@ func readerLoop() {
|
||||||
|
|
||||||
log.Info("Trying again in 5 seconds ....")
|
log.Info("Trying again in 5 seconds ....")
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = shClient.SendBlocksRequest()
|
if err := shClient.SendBlocksRequest(); err != nil {
|
||||||
if err != nil {
|
log.WithError(err).Error("Failed to send block request")
|
||||||
log.Println(err)
|
continue
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connected
|
|
||||||
log.Infof("Connected, Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock)
|
|
||||||
state = RS_READ
|
|
||||||
recon_cnt = 0
|
recon_cnt = 0
|
||||||
case RS_READ:
|
log.Infof("Connected, Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock)
|
||||||
log.WithError(shClient.Run()).Error("Failed to read from ship")
|
log.WithError(shClient.Run()).Error("Failed to read from ship")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
// Spawn reader loop in another thread.
|
// Spawn reader loop in another thread.
|
||||||
|
|
@ -101,6 +89,8 @@ func run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Info("failed to send close message to ship server")
|
log.WithError(err).Info("failed to send close message to ship server")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
running = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue