Skip to content

Commit

Permalink
Fix sync error
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Jun 6, 2024
1 parent c121e1c commit 6b17833
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions middleware/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func formatPath(path string) string {
func Stats() gin.HandlerFunc {
return func(c *gin.Context) {
route := formatPath(c.Request.URL.Path)
utils.WriterLock.Lock()
utils.Total++
utils.CommonStats[route]++
utils.WriterLock.Unlock()
c.Next()

}
Expand Down
12 changes: 9 additions & 3 deletions utils/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package utils
import (
"context"
"log"
"sync"
"time"

"github.com/redis/go-redis/v9"
)

var Total int64 = 0
var (
Total int64 = 0
CommonStats = map[string]int64{}

var CommonStats = map[string]int64{}
WriterLock = sync.Mutex{}

var ServerClose = make(chan struct{})
ServerClose = make(chan struct{})
)

func saveStats(client *redis.Client) {
WriterLock.Lock()
newTotal := Total
Total = 0 // reset the total

newStats := CommonStats
CommonStats = map[string]int64{} // reset the map
WriterLock.Unlock()

client.IncrBy(context.Background(), "stats:Total", newTotal) // Capitalized to avoid conflict with a potential key named "total"
for key, value := range newStats {
Expand Down

0 comments on commit 6b17833

Please sign in to comment.