From 041ae01da117cb3b7db565c9dcc666d8c8361383 Mon Sep 17 00:00:00 2001 From: Samir Patel <48686912+54mir@users.noreply.github.com> Date: Tue, 1 Jun 2021 20:46:44 -0500 Subject: [PATCH] poll places in usage calculation to check for closing --- api.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/api.go b/api.go index 66b0a1212..7d01299bb 100644 --- a/api.go +++ b/api.go @@ -994,10 +994,14 @@ func (api *API) calculateUsage() { lastUpdated := api.usageCache.lastUpdated if time.Since(lastUpdated) > api.usageCache.refreshInterval { - indexDetails, nodeMetadataBytes, err := api.holder.Txf().IndexUsageDetails() + indexDetails, nodeMetadataBytes, err := api.holder.Txf().IndexUsageDetails(api.isClosing) if err != nil { api.server.logger.Infof("couldn't get index usage details: %s", err) } + if api.isClosing() { + return + } + totalSize := nodeMetadataBytes for _, s := range indexDetails { totalSize += s.Total @@ -1042,7 +1046,8 @@ func (api *API) calculateUsage() { } // Periodically calculates disk usage -func (api *API) RefreshUsageCache(refresh time.Duration, trigger chan bool) { +func (api *API) RefreshUsageCache(refresh time.Duration) { + trigger := make(chan bool) defer close(trigger) api.usageCache = &usageCache{ data: make(map[string]NodeUsage), @@ -1075,6 +1080,16 @@ func (api *API) ResetUsageCache() error { return nil } +// isClosing returns true if the server is shutting down. +func (api *API) isClosing() bool { + select { + case <-api.server.closing: + return true + default: + return false + } +} + // RecalculateCaches forces all TopN caches to be updated. // This is done internally within a TopN query, but a user may want to do it ahead of time? func (api *API) RecalculateCaches(ctx context.Context) error {