Skip to content

Commit

Permalink
Merge pull request #107 from Chia-Network/configurable-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Aug 22, 2023
2 parents 86be8ea + a97be6f commit a3f400f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
34 changes: 12 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -29,10 +29,11 @@ func Execute() {

func init() {
var (
hostname string
metricsPort int
maxmindDBPath string
logLevel string
hostname string
metricsPort int
maxmindDBPath string
logLevel string
requestTimeout time.Duration
)

cobra.OnInitialize(initConfig)
Expand All @@ -42,24 +43,13 @@ func init() {
rootCmd.PersistentFlags().IntVar(&metricsPort, "metrics-port", 9914, "The port the metrics server binds to")
rootCmd.PersistentFlags().StringVar(&maxmindDBPath, "maxmind-db-path", "", "Path to the maxmind database file")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "How verbose the logs should be. panic, fatal, error, warn, info, debug, trace")
rootCmd.PersistentFlags().DurationVar(&requestTimeout, "rpc-timeout", 10*time.Second, "How long RPC requests will wait before timing out")

err := viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname"))
if err != nil {
log.Fatalln(err.Error())
}

err = viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port"))
if err != nil {
log.Fatalln(err.Error())
}
err = viper.BindPFlag("maxmind-db-path", rootCmd.PersistentFlags().Lookup("maxmind-db-path"))
if err != nil {
log.Fatalln(err.Error())
}
err = viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))
if err != nil {
log.Fatalln(err.Error())
}
cobra.CheckErr(viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname")))
cobra.CheckErr(viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port")))
cobra.CheckErr(viper.BindPFlag("maxmind-db-path", rootCmd.PersistentFlags().Lookup("maxmind-db-path")))
cobra.CheckErr(viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level")))
cobra.CheckErr(viper.BindPFlag("rpc-timeout", rootCmd.PersistentFlags().Lookup("rpc-timeout")))
}

// initConfig reads in config file and ENV variables if set.
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewMetrics(port uint16, logLevel log.Level) (*Metrics, error) {
metrics.httpClient, err = rpc.NewClient(rpc.ConnectionModeHTTP, rpc.WithAutoConfig(), rpc.WithBaseURL(&url.URL{
Scheme: "https",
Host: viper.GetString("hostname"),
}))
}), rpc.WithTimeout(viper.GetDuration("rpc-timeout")))
if err != nil {
// For now, http client is optional
// Sometimes this fails with outdated config.yaml files that don't have the crawler/seeder section present
Expand Down

0 comments on commit a3f400f

Please sign in to comment.