Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable timeout for rpc calls #107

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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