diff --git a/api.go b/api.go index 9a1d5f978..4ac463a4f 100644 --- a/api.go +++ b/api.go @@ -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) } } diff --git a/ctl/server.go b/ctl/server.go index 4d55bfc11..ce9796b13 100644 --- a/ctl/server.go +++ b/ctl/server.go @@ -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") } diff --git a/server/config.go b/server/config.go index 3e5629f89..837226b24 100644 --- a/server/config.go +++ b/server/config.go @@ -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 } diff --git a/server/server.go b/server/server.go index ec34a4194..5635d2089 100644 --- a/server/server.go +++ b/server/server.go @@ -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)