1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

cmd/thalos/server.go: move chainInfoOnce to its own file

This commit is contained in:
Henrik Hautakoski 2024-07-19 13:42:07 +02:00
parent c523f1c797
commit 1b1e6a1e33
2 changed files with 35 additions and 27 deletions

View file

@ -0,0 +1,35 @@
package main
import (
"context"
"time"
antelopeapi "github.com/shufflingpixels/antelope-go/api"
log "github.com/sirupsen/logrus"
)
// "Clever" way to make sure we only call the api once.
// Store a info pointer outside the returned closure.
// that pointer will live as long as the closure lives.
// and inside the closure we will reference the pointer and only
// call the api if it is nil.
func chainInfoOnce(api *antelopeapi.Client) func() *antelopeapi.Info {
var info *antelopeapi.Info
return func() *antelopeapi.Info {
if info == nil {
log.WithField("api", api.Url).Info("Get chain info from api")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
result, err := api.GetInfo(ctx)
if err != nil {
log.WithError(err).Fatal("Failed to call eos api")
return nil
}
info = &result
}
return info
}
}

View file

@ -243,33 +243,6 @@ func GetConfig(flags *pflag.FlagSet) (*config.Config, error) {
return cfg, nil
}
// "Clever" way to make sure we only call the api once.
// Store a info pointer outside the returned closure.
// that pointer will live as long as the closure lives.
// and inside the closure we will reference the pointer and only
// call the api if it is nil.
func chainInfoOnce(api *antelopeapi.Client) func() *antelopeapi.Info {
var info *antelopeapi.Info
return func() *antelopeapi.Info {
if info == nil {
log.WithField("api", api.Url).Info("Get chain info from api")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
result, err := api.GetInfo(ctx)
if err != nil {
log.WithError(err).Fatal("Failed to call eos api")
return nil
}
info = &result
}
return info
}
}
func ConnectRedis(conf *config.RedisConfig) (*redis.Client, error) {
logEntry := log.WithFields(log.Fields{
"addr": conf.Addr,