34
vendor/github.com/status-im/status-go/services/rpcstats/api.go
generated
vendored
Normal file
34
vendor/github.com/status-im/status-go/services/rpcstats/api.go
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package rpcstats
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// PublicAPI represents a set of APIs from the namespace.
|
||||
type PublicAPI struct {
|
||||
s *Service
|
||||
}
|
||||
|
||||
// NewAPI creates an instance of the API.
|
||||
func NewAPI(s *Service) *PublicAPI {
|
||||
return &PublicAPI{s: s}
|
||||
}
|
||||
|
||||
// Reset resets RPC usage stats
|
||||
func (api *PublicAPI) Reset(context context.Context) {
|
||||
resetStats()
|
||||
}
|
||||
|
||||
type RPCStats struct {
|
||||
Total uint `json:"total"`
|
||||
CounterPerMethod map[string]uint `json:"methods"`
|
||||
}
|
||||
|
||||
// GetStats retrun RPC usage stats
|
||||
func (api *PublicAPI) GetStats(context context.Context) (RPCStats, error) {
|
||||
total, perMethod := getStats()
|
||||
return RPCStats{
|
||||
Total: total,
|
||||
CounterPerMethod: perMethod,
|
||||
}, nil
|
||||
}
|
||||
44
vendor/github.com/status-im/status-go/services/rpcstats/service.go
generated
vendored
Normal file
44
vendor/github.com/status-im/status-go/services/rpcstats/service.go
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
package rpcstats
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
|
||||
// Service represents our own implementation of status status operations.
|
||||
type Service struct{}
|
||||
|
||||
// New returns a new Service.
|
||||
func New() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
// APIs returns a list of new APIs.
|
||||
func (s *Service) APIs() []rpc.API {
|
||||
return []rpc.API{
|
||||
{
|
||||
Namespace: "rpcstats",
|
||||
Version: "1.0",
|
||||
Service: NewAPI(s),
|
||||
Public: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Protocols returns list of p2p protocols.
|
||||
func (s *Service) Protocols() []p2p.Protocol {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start is run when a service is started.
|
||||
// It does nothing in this case but is required by `node.Service` interface.
|
||||
func (s *Service) Start() error {
|
||||
resetStats()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop is run when a service is stopped.
|
||||
// It does nothing in this case but is required by `node.Service` interface.
|
||||
func (s *Service) Stop() error {
|
||||
return nil
|
||||
}
|
||||
48
vendor/github.com/status-im/status-go/services/rpcstats/stats.go
generated
vendored
Normal file
48
vendor/github.com/status-im/status-go/services/rpcstats/stats.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package rpcstats
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type RPCUsageStats struct {
|
||||
total uint
|
||||
counterPerMethod map[string]uint
|
||||
rw sync.RWMutex
|
||||
}
|
||||
|
||||
var stats *RPCUsageStats
|
||||
|
||||
func getInstance() *RPCUsageStats {
|
||||
if stats == nil {
|
||||
stats = &RPCUsageStats{
|
||||
total: 0,
|
||||
counterPerMethod: map[string]uint{},
|
||||
}
|
||||
}
|
||||
return stats
|
||||
}
|
||||
|
||||
func getStats() (uint, map[string]uint) {
|
||||
stats := getInstance()
|
||||
stats.rw.RLock()
|
||||
defer stats.rw.RUnlock()
|
||||
return stats.total, stats.counterPerMethod
|
||||
}
|
||||
|
||||
func resetStats() {
|
||||
stats := getInstance()
|
||||
stats.rw.Lock()
|
||||
defer stats.rw.Unlock()
|
||||
|
||||
stats.total = 0
|
||||
stats.counterPerMethod = map[string]uint{}
|
||||
}
|
||||
|
||||
func CountCall(method string) {
|
||||
stats := getInstance()
|
||||
stats.rw.Lock()
|
||||
defer stats.rw.Unlock()
|
||||
|
||||
stats.total++
|
||||
stats.counterPerMethod[method]++
|
||||
}
|
||||
Reference in New Issue
Block a user