github.com/status-im/status-go@v1.1.0/services/rpcstats/stats.go (about) 1 package rpcstats 2 3 import ( 4 "sync" 5 ) 6 7 type RPCUsageStats struct { 8 total uint 9 counterPerMethod *sync.Map 10 counterPerMethodPerTag *sync.Map 11 } 12 13 var stats *RPCUsageStats 14 var mu sync.Mutex 15 16 func getInstance() *RPCUsageStats { 17 mu.Lock() 18 defer mu.Unlock() 19 20 if stats == nil { 21 stats = &RPCUsageStats{} 22 stats.counterPerMethod = &sync.Map{} 23 stats.counterPerMethodPerTag = &sync.Map{} 24 } 25 return stats 26 } 27 28 func getStats() (uint, *sync.Map, *sync.Map) { 29 stats := getInstance() 30 return stats.total, stats.counterPerMethod, stats.counterPerMethodPerTag 31 } 32 33 func resetStats() { 34 stats := getInstance() 35 stats.total = 0 36 stats.counterPerMethod = &sync.Map{} 37 stats.counterPerMethodPerTag = &sync.Map{} 38 } 39 40 func CountCall(method string) { 41 stats := getInstance() 42 stats.total++ 43 value, _ := stats.counterPerMethod.LoadOrStore(method, uint(0)) 44 stats.counterPerMethod.Store(method, value.(uint)+1) 45 } 46 47 func CountCallWithTag(method string, tag string) { 48 if tag == "" { 49 CountCall(method) 50 return 51 } 52 53 stats := getInstance() 54 value, _ := stats.counterPerMethodPerTag.LoadOrStore(tag, &sync.Map{}) 55 methodMap := value.(*sync.Map) 56 value, _ = methodMap.LoadOrStore(method, uint(0)) 57 methodMap.Store(method, value.(uint)+1) 58 stats.total++ 59 }