change flag from interval to duration

This commit is contained in:
Samir Patel 2021-05-25 18:21:23 -05:00 committed by Samir Patel
parent 0ef16ce634
commit f5cc179893
4 changed files with 11 additions and 10 deletions

11
api.go
View file

@ -900,7 +900,7 @@ type usageCache struct {
data map[string]NodeUsage
lastUpdated time.Time
mu sync.Mutex
refreshRateMins int
refreshInterval time.Duration
}
// NodeUsage represents all usage measurements for one node.
@ -976,7 +976,8 @@ func (api *API) calculateUsage() {
api.usageCache.mu.Lock()
defer api.usageCache.mu.Unlock()
if api.usageCache.lastUpdated.After(time.Now().Add(time.Minute * time.Duration(api.usageCache.refreshRateMins) * -1)) {
// if api.usageCache.lastUpdated.After(time.Now().Add(time.Minute * time.Duration(api.usageCache.refreshInterval) * -1)) {
if time.Since(api.usageCache.lastUpdated) < api.usageCache.refreshInterval {
fmt.Printf("RefreshRate, too soon: time: %v, current time: %v \n", api.usageCache.lastUpdated, time.Now())
return
} else {
@ -1029,15 +1030,15 @@ func (api *API) calculateUsage() {
}
// Periodically calculates disk usage
func (api *API) RefreshUsageCache(refresh int) {
func (api *API) RefreshUsageCache(refresh time.Duration) {
api.usageCache = &usageCache{
data: make(map[string]NodeUsage),
refreshRateMins: refresh,
refreshInterval: refresh,
}
for {
api.calculateUsage()
api.calculateNodeUsage()
time.Sleep(time.Duration(api.usageCache.refreshRateMins) * time.Minute)
time.Sleep(api.usageCache.refreshInterval)
}
}

View file

@ -112,5 +112,5 @@ func BuildServerFlags(cmd *cobra.Command, srv *server.Command) {
flags.Uint16Var(&srv.Config.Postgres.ConnectionLimit, "postgres.connection-limit", srv.Config.Postgres.ConnectionLimit, "Maximum number of simultaneous postgres connections to allow. (set 0 to disable)")
// Disk Usage refresh rate in minutes for ui/usage http endpoint
flags.IntVar(&srv.Config.DiskUsage.Interval, "disk-usage-interval", srv.Config.DiskUsage.Interval, "Number in minutes between recalculations of disk usage cache")
flags.DurationVar((*time.Duration)(&srv.Config.DiskUsage.Interval), "disk-usage-interval", time.Duration(srv.Config.DiskUsage.Interval), "Number in minutes between recalculations of disk usage cache")
}

View file

@ -225,9 +225,9 @@ type Config struct {
// LookupDBDSN is an external database to connect to for `ExternalLookup` queries.
LookupDBDSN string `toml:"lookup-db-dsn"`
// Disk Usage refresh rate in minutes for ui/usage http endpoint
// Disk Usage refresh interval for ui/usage http endpoint
DiskUsage struct {
Interval int `toml:"disk-usage-interval"`
Interval toml.Duration `toml:"disk-usage-interval"`
}
}
@ -364,7 +364,7 @@ func NewConfig() *Config {
c.Etcd.PeerCertFile = ""
c.Etcd.PeerKeyFile = ""
c.DiskUsage.Interval = 6 * 60 // 6 hours
c.DiskUsage.Interval = toml.Duration(6 * 60 * time.Minute) // 6 hours
return c
}

View file

@ -277,7 +277,7 @@ func (m *Command) Start() (err error) {
}
}
go m.API.RefreshUsageCache(m.Config.DiskUsage.Interval)
go m.API.RefreshUsageCache(time.Duration(m.Config.DiskUsage.Interval))
_ = testhook.Opened(pilosa.NewAuditor(), m, nil)
close(m.Started)