2022-07-13 09:55:38 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Statistic is metrics of the pool.
|
|
|
|
type Statistic struct {
|
|
|
|
overallErrors uint64
|
2022-07-18 08:42:29 +00:00
|
|
|
nodes []NodeStatistic
|
2022-07-13 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OverallErrors returns sum of errors on all connections. It doesn't decrease.
|
|
|
|
func (s Statistic) OverallErrors() uint64 {
|
|
|
|
return s.overallErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes returns list of nodes statistic.
|
2022-07-18 08:42:29 +00:00
|
|
|
func (s Statistic) Nodes() []NodeStatistic {
|
2022-07-13 09:55:38 +00:00
|
|
|
return s.nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrUnknownNode indicate that node with current address is not found in list.
|
|
|
|
var ErrUnknownNode = errors.New("unknown node")
|
|
|
|
|
|
|
|
// Node returns NodeStatistic by node address.
|
|
|
|
// If such node doesn't exist ErrUnknownNode error is returned.
|
|
|
|
func (s Statistic) Node(address string) (*NodeStatistic, error) {
|
|
|
|
for i := range s.nodes {
|
|
|
|
if s.nodes[i].address == address {
|
2022-07-18 08:42:29 +00:00
|
|
|
return &s.nodes[i], nil
|
2022-07-13 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrUnknownNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeStatistic is metrics of certain connections.
|
|
|
|
type NodeStatistic struct {
|
|
|
|
address string
|
|
|
|
latency time.Duration
|
2022-07-13 10:16:55 +00:00
|
|
|
requests uint64
|
2022-07-13 09:55:38 +00:00
|
|
|
overallErrors uint64
|
|
|
|
currentErrors uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// OverallErrors returns all errors on current node.
|
|
|
|
// This value never decreases.
|
|
|
|
func (n NodeStatistic) OverallErrors() uint64 {
|
|
|
|
return n.overallErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentErrors returns errors on current node.
|
|
|
|
// This value is always less than 'errorThreshold' from InitParameters.
|
|
|
|
func (n NodeStatistic) CurrentErrors() uint32 {
|
|
|
|
return n.currentErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
// Latency returns average latency for node request.
|
|
|
|
func (n NodeStatistic) Latency() time.Duration {
|
|
|
|
return n.latency
|
|
|
|
}
|
|
|
|
|
2022-07-13 10:16:55 +00:00
|
|
|
// Requests returns number of requests.
|
|
|
|
func (n NodeStatistic) Requests() uint64 {
|
|
|
|
return n.requests
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:55:38 +00:00
|
|
|
// Address returns node endpoint address.
|
|
|
|
func (n NodeStatistic) Address() string {
|
|
|
|
return n.address
|
|
|
|
}
|