From 941e1942e15ee81c37b94107404592e932038dac Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 22 Aug 2023 12:33:59 -0500 Subject: [PATCH 1/2] Add configurable timeout for rpc calls --- cmd/root.go | 34 ++++++++++++---------------------- internal/metrics/metrics.go | 2 +- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b1cc59b..53d60b0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,8 +4,8 @@ import ( "fmt" "os" "strings" + "time" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -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) @@ -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. diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 7f212d1..a6723a8 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -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 From a97be6f363d832a5fba4f4684e34478ce04c5287 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 22 Aug 2023 12:35:13 -0500 Subject: [PATCH 2/2] LInt --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 53d60b0..f6e42e2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ 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") + rootCmd.PersistentFlags().DurationVar(&requestTimeout, "rpc-timeout", 10*time.Second, "How long RPC requests will wait before timing out") cobra.CheckErr(viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname"))) cobra.CheckErr(viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port")))