api.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package client
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/common/mclock"
  20. "github.com/ethereum/go-ethereum/les/utils"
  21. "github.com/ethereum/go-ethereum/p2p/enode"
  22. )
  23. // PrivateClientAPI implements the vflux client side API
  24. type PrivateClientAPI struct {
  25. vt *ValueTracker
  26. }
  27. // NewPrivateClientAPI creates a PrivateClientAPI
  28. func NewPrivateClientAPI(vt *ValueTracker) *PrivateClientAPI {
  29. return &PrivateClientAPI{vt}
  30. }
  31. // parseNodeStr converts either an enode address or a plain hex node id to enode.ID
  32. func parseNodeStr(nodeStr string) (enode.ID, error) {
  33. if id, err := enode.ParseID(nodeStr); err == nil {
  34. return id, nil
  35. }
  36. if node, err := enode.Parse(enode.ValidSchemes, nodeStr); err == nil {
  37. return node.ID(), nil
  38. } else {
  39. return enode.ID{}, err
  40. }
  41. }
  42. // RequestStats returns the current contents of the reference request basket, with
  43. // request values meaning average per request rather than total.
  44. func (api *PrivateClientAPI) RequestStats() []RequestStatsItem {
  45. return api.vt.RequestStats()
  46. }
  47. // Distribution returns a distribution as a series of (X, Y) chart coordinates,
  48. // where the X axis is the response time in seconds while the Y axis is the amount of
  49. // service value received with a response time close to the X coordinate.
  50. // The distribution is optionally normalized to a sum of 1.
  51. // If nodeStr == "" then the global distribution is returned, otherwise the individual
  52. // distribution of the specified server node.
  53. func (api *PrivateClientAPI) Distribution(nodeStr string, normalized bool) (RtDistribution, error) {
  54. var expFactor utils.ExpirationFactor
  55. if !normalized {
  56. expFactor = utils.ExpFactor(api.vt.StatsExpirer().LogOffset(mclock.Now()))
  57. }
  58. if nodeStr == "" {
  59. return api.vt.RtStats().Distribution(normalized, expFactor), nil
  60. }
  61. if id, err := parseNodeStr(nodeStr); err == nil {
  62. return api.vt.GetNode(id).RtStats().Distribution(normalized, expFactor), nil
  63. } else {
  64. return RtDistribution{}, err
  65. }
  66. }
  67. // Timeout suggests a timeout value based on either the global distribution or the
  68. // distribution of the specified node. The parameter is the desired rate of timeouts
  69. // assuming a similar distribution in the future.
  70. // Note that the actual timeout should have a sensible minimum bound so that operating
  71. // under ideal working conditions for a long time (for example, using a local server
  72. // with very low response times) will not make it very hard for the system to accommodate
  73. // longer response times in the future.
  74. func (api *PrivateClientAPI) Timeout(nodeStr string, failRate float64) (float64, error) {
  75. if nodeStr == "" {
  76. return float64(api.vt.RtStats().Timeout(failRate)) / float64(time.Second), nil
  77. }
  78. if id, err := parseNodeStr(nodeStr); err == nil {
  79. return float64(api.vt.GetNode(id).RtStats().Timeout(failRate)) / float64(time.Second), nil
  80. } else {
  81. return 0, err
  82. }
  83. }
  84. // Value calculates the total service value provided either globally or by the specified
  85. // server node, using a weight function based on the given timeout.
  86. func (api *PrivateClientAPI) Value(nodeStr string, timeout float64) (float64, error) {
  87. wt := TimeoutWeights(time.Duration(timeout * float64(time.Second)))
  88. expFactor := utils.ExpFactor(api.vt.StatsExpirer().LogOffset(mclock.Now()))
  89. if nodeStr == "" {
  90. return api.vt.RtStats().Value(wt, expFactor), nil
  91. }
  92. if id, err := parseNodeStr(nodeStr); err == nil {
  93. return api.vt.GetNode(id).RtStats().Value(wt, expFactor), nil
  94. } else {
  95. return 0, err
  96. }
  97. }