forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache statistics and provide estimation methods
Currently whenever the prometheus metrics endpoint or `/admin` endpoint are viewed the statistics are recalculated immediately - using COUNT rather than a less expensive method. This PR provides a mechanism to cache these statistics, avoids generating all of the metrics on the admin page and provides an estimation method for the plain table counts. Fix go-gitea#17506 Signed-off-by: Andrew Thornton <[email protected]>
- Loading branch information
Showing
9 changed files
with
165 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package metrics | ||
|
||
import ( | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/cache" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
var statisticsLock sync.Mutex | ||
|
||
func GetStatistic(estimate bool, statisticsTTL time.Duration, metrics bool) models.Statistic { | ||
if statisticsTTL > 0 { | ||
c := cache.GetCache() | ||
if c != nil { | ||
statisticsLock.Lock() | ||
defer statisticsLock.Unlock() | ||
cacheKey := "models/statistic.Statistic." + strconv.FormatBool(estimate) + strconv.FormatBool(metrics) | ||
|
||
if stats, ok := c.Get(cacheKey).(*models.Statistic); ok { | ||
return *stats | ||
} | ||
|
||
stats := models.GetStatistic(estimate, metrics) | ||
c.Put(cacheKey, &stats, setting.DurationToCacheTTL(statisticsTTL)) | ||
return stats | ||
} | ||
} | ||
|
||
return models.GetStatistic(estimate, metrics) | ||
} |
Oops, something went wrong.