diff --git a/.golangci.yml b/.golangci.yml index 2148d09..50c90ef 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -47,3 +47,4 @@ linters: - nosnakecase - ifshort - gci + - depguard diff --git a/pkg/config/config.go b/pkg/config/config.go index a9e2663..90763c9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -11,23 +11,23 @@ import ( ) type LogConfig struct { - LogLevel string `toml:"level" default:"info"` - JSONOutput null.Bool `toml:"json" default:"false"` + LogLevel string `default:"info" toml:"level"` + JSONOutput null.Bool `default:"false" toml:"json"` } type TendermintConfig struct { - Enabled null.Bool `toml:"enabled" default:"true"` - Address string `toml:"address" default:"http://localhost:26657"` - QueryUpgrades null.Bool `toml:"query-upgrades" default:"true"` + Enabled null.Bool `default:"true" toml:"enabled"` + Address string `default:"http://localhost:26657" toml:"address"` + QueryUpgrades null.Bool `default:"true" toml:"query-upgrades"` } type GithubConfig struct { - Repository string `toml:"repository" default:""` + Repository string `default:"" toml:"repository"` Token string `toml:"token"` } type CosmovisorConfig struct { - Enabled null.Bool `toml:"enabled" default:"true"` + Enabled null.Bool `default:"true" toml:"enabled"` ChainBinaryName string `toml:"chain-binary-name"` ChainFolder string `toml:"chain-folder"` CosmovisorPath string `toml:"cosmovisor-path"` @@ -38,7 +38,7 @@ type Config struct { TendermintConfig TendermintConfig `toml:"tendermint"` CosmovisorConfig CosmovisorConfig `toml:"cosmovisor"` GithubConfig GithubConfig `toml:"github"` - ListenAddress string `toml:"listen-address" default:":9500"` + ListenAddress string `default:":9500" toml:"listen-address"` } func (c *GithubConfig) Validate() error { diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index bb1bd9a..5598ac6 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -1,9 +1,13 @@ package constants -import "regexp" +import ( + "regexp" + "time" +) const ( - MetricsPrefix = "cosmos_node_exporter_" + MetricsPrefix = "cosmos_node_exporter_" + UncachedGithubQueryTime = time.Hour ) var ( diff --git a/pkg/github/github.go b/pkg/github/github.go index af60cf8..1684b94 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -33,6 +33,18 @@ func NewGithub(config *config.Config, logger *zerolog.Logger) *Github { } } +func (g *Github) UseCache() bool { + // If the last result is not present - do not use cache, for the first query. + if g.LastResult == nil { + return false + } + + // We need to make uncached requests once in a while, to make sure everything is ok + // (for example, if we messed up caching itself). + diff := time.Since(g.LastModified) + return diff < constants.UncachedGithubQueryTime +} + func (g *Github) GetLatestRelease() (types.ReleaseInfo, error) { latestReleaseUrl := fmt.Sprintf( "https://api.github.com/repos/%s/%s/releases/latest", @@ -49,7 +61,15 @@ func (g *Github) GetLatestRelease() (types.ReleaseInfo, error) { return types.ReleaseInfo{}, err } - if g.LastResult != nil { + useCache := g.UseCache() + + g.Logger.Trace(). + Str("url", latestReleaseUrl). + Bool("cached", useCache). + Str("time-since-latest", time.Since(g.LastModified).String()). + Msg("Querying GitHub") + + if useCache { req.Header.Set("If-Modified-Since", g.LastModified.Format(http.TimeFormat)) } @@ -77,7 +97,7 @@ func (g *Github) GetLatestRelease() (types.ReleaseInfo, error) { return releaseInfo, err } - // Github returned error, probably rate-limiting + // GitHub returned error, probably rate-limiting if releaseInfo.Message != "" { return releaseInfo, fmt.Errorf("got error from Github: %s", releaseInfo.Message) }