resources.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package node
  16. import (
  17. "fmt"
  18. "strings"
  19. "github.com/libp2p/go-libp2p-core/network"
  20. "github.com/libp2p/go-libp2p-core/peer"
  21. libp2pprotocol "github.com/libp2p/go-libp2p-core/protocol"
  22. rcmgr "github.com/libp2p/go-libp2p-resource-manager"
  23. )
  24. type NetLimitConfig struct {
  25. Dynamic bool `json:",omitempty"`
  26. // set if Dynamic is false
  27. Memory int64 `json:",omitempty"`
  28. // set if Dynamic is true
  29. MemoryFraction float64 `json:",omitempty"`
  30. MinMemory int64 `json:",omitempty"`
  31. MaxMemory int64 `json:",omitempty"`
  32. Streams, StreamsInbound, StreamsOutbound int
  33. Conns, ConnsInbound, ConnsOutbound int
  34. FD int
  35. }
  36. func NetSetLimit(mgr network.ResourceManager, scope string, limit NetLimitConfig) error {
  37. setLimit := func(s network.ResourceScope) error {
  38. limiter, ok := s.(rcmgr.ResourceScopeLimiter)
  39. if !ok {
  40. return fmt.Errorf("resource scope doesn't implement ResourceScopeLimiter interface")
  41. }
  42. var newLimit rcmgr.Limit
  43. if limit.Dynamic {
  44. newLimit = &rcmgr.DynamicLimit{
  45. MemoryLimit: rcmgr.MemoryLimit{
  46. MemoryFraction: limit.MemoryFraction,
  47. MinMemory: limit.MinMemory,
  48. MaxMemory: limit.MaxMemory,
  49. },
  50. BaseLimit: rcmgr.BaseLimit{
  51. Streams: limit.Streams,
  52. StreamsInbound: limit.StreamsInbound,
  53. StreamsOutbound: limit.StreamsOutbound,
  54. Conns: limit.Conns,
  55. ConnsInbound: limit.ConnsInbound,
  56. ConnsOutbound: limit.ConnsOutbound,
  57. FD: limit.FD,
  58. },
  59. }
  60. } else {
  61. newLimit = &rcmgr.StaticLimit{
  62. Memory: limit.Memory,
  63. BaseLimit: rcmgr.BaseLimit{
  64. Streams: limit.Streams,
  65. StreamsInbound: limit.StreamsInbound,
  66. StreamsOutbound: limit.StreamsOutbound,
  67. Conns: limit.Conns,
  68. ConnsInbound: limit.ConnsInbound,
  69. ConnsOutbound: limit.ConnsOutbound,
  70. FD: limit.FD,
  71. },
  72. }
  73. }
  74. limiter.SetLimit(newLimit)
  75. return nil
  76. }
  77. switch {
  78. case scope == "system":
  79. err := mgr.ViewSystem(func(s network.ResourceScope) error {
  80. return setLimit(s)
  81. })
  82. return err
  83. case scope == "transient":
  84. err := mgr.ViewTransient(func(s network.ResourceScope) error {
  85. return setLimit(s)
  86. })
  87. return err
  88. case strings.HasPrefix(scope, "svc:"):
  89. svc := scope[4:]
  90. err := mgr.ViewService(svc, func(s network.ServiceScope) error {
  91. return setLimit(s)
  92. })
  93. return err
  94. case strings.HasPrefix(scope, "proto:"):
  95. proto := scope[6:]
  96. err := mgr.ViewProtocol(libp2pprotocol.ID(proto), func(s network.ProtocolScope) error {
  97. return setLimit(s)
  98. })
  99. return err
  100. case strings.HasPrefix(scope, "peer:"):
  101. p := scope[5:]
  102. pid, err := peer.Decode(p)
  103. if err != nil {
  104. return fmt.Errorf("invalid peer ID: %s: %w", p, err)
  105. }
  106. err = mgr.ViewPeer(pid, func(s network.PeerScope) error {
  107. return setLimit(s)
  108. })
  109. return err
  110. default:
  111. return fmt.Errorf("invalid scope %s", scope)
  112. }
  113. }