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:
parent
c523f1c797
commit
1b1e6a1e33
2 changed files with 35 additions and 27 deletions
35
cmd/thalos/antelope_api_helpers.go
Normal file
35
cmd/thalos/antelope_api_helpers.go
Normal 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
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue