-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: capping mechanism for relay and service connections #3184
Changes from 16 commits
738c507
9f778af
a89d619
7c00a8e
1bb28bd
2d9ec0f
7d1a2a7
6720dfd
8b80dc5
a2ed692
4347631
5c61d77
42c4dd9
0724f7d
c625d14
eef0d11
16caad0
03609a2
a44ce34
bb88d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import std/strutils, results, regex | ||
import std/[strutils, math], results, regex | ||
|
||
proc parseMsgSize*(input: string): Result[uint64, string] = | ||
## Parses size strings such as "1.2 KiB" or "3Kb" and returns the equivalent number of bytes | ||
|
@@ -49,3 +49,28 @@ proc parseCorrectMsgSize*(input: string): uint64 = | |
let ret = parseMsgSize(input).valueOr: | ||
return 0 | ||
return ret | ||
|
||
proc parseRelayServiceRatio*(ratio: string): Result[(float, float), string] = | ||
## Parses a relay/service ratio string to [ float, float ]. The total should sum 100% | ||
## e.g., (0.4, 0.6) == parseRelayServiceRatio("40:60") | ||
try: | ||
let elements = ratio.split(":") | ||
if elements.len != 2: | ||
raise newException(ValueError, "expected format 'X:Y', ratio = " & ratio) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, sorry that I didn't mention before. Would it be possible to avoid raising an exception plz? Applies elsewhere. We already have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. |
||
let | ||
relayRatio = parseFloat(elements[0]) | ||
serviceRatio = parseFloat(elements[1]) | ||
|
||
if relayRatio < 0 or serviceRatio < 0: | ||
raise newException( | ||
ValueError, "Relay service ratio must be non-negative, ratio = " & ratio | ||
) | ||
|
||
let total = relayRatio + serviceRatio | ||
if int(total) != 100: | ||
Ivansete-status marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise newException(ValueError, "Total ratio should be 100, total = " & $total) | ||
|
||
return ok((relayRatio / 100.0, serviceRatio / 100.0)) | ||
except ValueError as e: | ||
raise newException(ValueError, "Invalid Format Error : " & e.msg) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{.push raises: [].} | ||
|
||
import | ||
std/[options, net], | ||
std/[options, net, math], | ||
results, | ||
chronicles, | ||
libp2p/crypto/crypto, | ||
|
@@ -15,7 +15,8 @@ import | |
../discovery/waku_discv5, | ||
../waku_node, | ||
../node/peer_manager, | ||
../common/rate_limit/setting | ||
../common/rate_limit/setting, | ||
../common/utils/parse_size_units | ||
|
||
type | ||
WakuNodeBuilder* = object # General | ||
|
@@ -29,7 +30,8 @@ type | |
peerStorageCapacity: Option[int] | ||
|
||
# Peer manager config | ||
maxRelayPeers: Option[int] | ||
maxRelayPeers: int | ||
maxServicePeers: int | ||
colocationLimit: int | ||
shardAware: bool | ||
|
||
|
@@ -108,10 +110,21 @@ proc withPeerStorage*( | |
builder.peerStorageCapacity = capacity | ||
|
||
proc withPeerManagerConfig*( | ||
builder: var WakuNodeBuilder, maxRelayPeers = none(int), shardAware = false | ||
builder: var WakuNodeBuilder, | ||
maxConnections: int, | ||
relayServiceRatio: string, | ||
shardAware = false, | ||
) = | ||
builder.maxRelayPeers = maxRelayPeers | ||
builder.shardAware = shardAware | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not needed to handle exception now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes ! we already handle in function. |
||
let (relayRatio, serviceRatio) = parseRelayServiceRatio(relayServiceRatio).get() | ||
var relayPeers = int(ceil(float(maxConnections) * relayRatio)) | ||
var servicePeers = int(floor(float(maxConnections) * serviceRatio)) | ||
|
||
builder.maxServicePeers = servicePeers | ||
builder.maxRelayPeers = relayPeers | ||
builder.shardAware = shardAware | ||
except ValueError as e: | ||
error "Error : ", error = e.msg | ||
|
||
proc withColocationLimit*(builder: var WakuNodeBuilder, colocationLimit: int) = | ||
builder.colocationLimit = colocationLimit | ||
|
@@ -190,7 +203,8 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = | |
let peerManager = PeerManager.new( | ||
switch = switch, | ||
storage = builder.peerStorage.get(nil), | ||
maxRelayPeers = builder.maxRelayPeers, | ||
maxRelayPeers = some(builder.maxRelayPeers), | ||
maxServicePeers = some(builder.maxServicePeers), | ||
colocationLimit = builder.colocationLimit, | ||
shardedPeerManagement = builder.shardAware, | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,7 +197,20 @@ type WakuNodeConf* = object | |
desc: "Maximum allowed number of libp2p connections.", | ||
defaultValue: 50, | ||
name: "max-connections" | ||
.}: uint16 | ||
.}: int | ||
|
||
maxRelayPeers* {. | ||
desc: | ||
"Deprecated. Use relay-service-ratio instead. It represents the maximum allowed number of relay peers.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While checking changes it seems to me the deprecated means here we will not use it at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your insight. We've decided to adopt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I buy in to this idea. All I wanted to emphasize that do nothide the unintended/old usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That’s a great idea! Users typically don’t check logs unless there’s an error. will stop after detect wrong config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Now I understand your concern. I’ve made it backward compatible with maxRelayPeers, ensuring that users who aren’t aware of the deprecation are covered as well. At that time, I didn’t fully grasp your point. |
||
name: "max-relay-peers" | ||
.}: Option[int] | ||
|
||
relayServiceRatio* {. | ||
desc: | ||
"This percentage ratio represents the relay peers to service peers. For example, 60:40, tells that 60% of the max-connections will be used for relay protocol and the other 40% of max-connections will be reserved for other service protocols (e.g., filter, lightpush, store, metadata, etc.)", | ||
name: "relay-service-ratio", | ||
defaultValue: "60:40" # 60:40 ratio of relay to service peers | ||
.}: string | ||
|
||
colocationLimit* {. | ||
desc: | ||
|
@@ -206,10 +219,6 @@ type WakuNodeConf* = object | |
name: "ip-colocation-limit" | ||
.}: int | ||
|
||
maxRelayPeers* {. | ||
desc: "Maximum allowed number of relay peers.", name: "max-relay-peers" | ||
.}: Option[int] | ||
|
||
Comment on lines
-209
to
-212
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot directly remove this optional parameter. Instead, we should inform that we are deprecating it and it will be completely removed after two releases. Therefore, we now just need to "tag" it as deprecated:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good approch ! will revert this delete. |
||
peerStoreCapacity* {. | ||
desc: "Maximum stored peers in the peerstore.", name: "peer-store-capacity" | ||
.}: Option[int] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,8 +101,15 @@ proc initNode( | |
agentString = some(conf.agentString), | ||
) | ||
builder.withColocationLimit(conf.colocationLimit) | ||
|
||
# Backword compatibility for maxRelayPeers | ||
if conf.maxRelayPeers.isSome(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this condition is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your review and patience. It’s now backward compatible and parse function without raising exceptions. |
||
error "maxRelayPeers is deprecated, using relayServiceRatio instead ( recommendation ), if relayServiceRatio is not set, by default it will be 60:40, maxRelayPeers and maxServicePeer is calculated accordingly" | ||
|
||
builder.withPeerManagerConfig( | ||
maxRelayPeers = conf.maxRelayPeers, shardAware = conf.relayShardedPeerManagement | ||
maxConnections = conf.maxConnections, | ||
relayServiceRatio = conf.relayServiceRatio, | ||
shardAware = conf.relayShardedPeerManagement, | ||
) | ||
builder.withRateLimit(conf.rateLimits) | ||
builder.withCircuitRelay(relay) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not hide the exception, so compiler can help using the function properly.