Skip to content

Commit

Permalink
feat: refactor using cache for GitHub (#19)
Browse files Browse the repository at this point in the history
* feat: refactor using cache for GitHub

* chore: fixed linting
  • Loading branch information
freak12techno authored Jul 11, 2023
1 parent 8f1f47e commit fd01dc3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ linters:
- nosnakecase
- ifshort
- gci
- depguard
16 changes: 8 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package constants

import "regexp"
import (
"regexp"
"time"
)

const (
MetricsPrefix = "cosmos_node_exporter_"
MetricsPrefix = "cosmos_node_exporter_"
UncachedGithubQueryTime = time.Hour
)

var (
Expand Down
24 changes: 22 additions & 2 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit fd01dc3

Please sign in to comment.