Skip to content

Commit

Permalink
Improve cache time reliability
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
  • Loading branch information
mblaschke committed May 28, 2020
1 parent 8a19638 commit c92a215
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
7 changes: 5 additions & 2 deletions probe_metrics_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ func probeMetricsListHandler(w http.ResponseWriter, r *http.Request) {
wg.Wait()

// enable caching if enabled
if settings.Cache != nil {
metricsList.StoreToCache(cacheKey, *settings.Cache)
fmt.Println(settings.CacheDuration(startTime))
if cacheDuration := settings.CacheDuration(startTime); cacheDuration != nil {
fmt.Println(settings.CacheDuration(startTime).String())
metricsList.StoreToCache(cacheKey, *cacheDuration)
w.Header().Add("X-metrics-cached-until", time.Now().Add(*cacheDuration).Format(time.RFC3339))
}
} else {
w.Header().Add("X-metrics-cached", "true")
Expand Down
5 changes: 3 additions & 2 deletions probe_metrics_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ func probeMetricsResourceHandler(w http.ResponseWriter, r *http.Request) {
}

// enable caching if enabled
if settings.Cache != nil {
metricsList.StoreToCache(cacheKey, *settings.Cache)
if cacheDuration := settings.CacheDuration(startTime); cacheDuration != nil {
metricsList.StoreToCache(cacheKey, *cacheDuration)
w.Header().Add("X-metrics-cached-until", time.Now().Add(*cacheDuration).Format(time.RFC3339))
}
} else {
w.Header().Add("X-metrics-cached", "true")
Expand Down
5 changes: 3 additions & 2 deletions probe_metrics_scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ func probeMetricsScrapeHandler(w http.ResponseWriter, r *http.Request) {
wg.Wait()

// enable caching if enabled
if settings.Cache != nil {
metricsList.StoreToCache(cacheKey, *settings.Cache)
if cacheDuration := settings.CacheDuration(startTime); cacheDuration != nil {
metricsList.StoreToCache(cacheKey, *cacheDuration)
w.Header().Add("X-metrics-cached-until", time.Now().Add(*cacheDuration).Format(time.RFC3339))
}
} else {
w.Header().Add("X-metrics-cached", "true")
Expand Down
12 changes: 12 additions & 0 deletions request.metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ func NewRequestMetricSettings(r *http.Request) (RequestMetricSettings, error) {
return ret, nil
}

func (s *RequestMetricSettings) CacheDuration(requestTime time.Time) (ret *time.Duration) {
if s.Cache != nil {
bufferDuration := time.Duration(2 * time.Second)
cachedUntilTime := requestTime.Add(*s.Cache).Add(-bufferDuration)
cacheDuration := cachedUntilTime.Sub(time.Now())
if cacheDuration.Seconds() > 0 {
ret = &cacheDuration
}
}
return
}

func (s *RequestMetricSettings) SetMetrics(val string) {
s.Metric = strings.Split(val, ",")
}
Expand Down

0 comments on commit c92a215

Please sign in to comment.