-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat!: rcmgr: Change LimitConfig to use LimitVal type #2000
Changes from all commits
bf102b3
2259dd9
ecf1094
4e0689d
421a07d
75f2fc4
e3999f1
88df42e
7e52472
7e9135e
c68e24c
2f95f28
15f6fa5
56056a3
0702c19
75c0b14
ba009ae
e7a41cd
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 |
---|---|---|
|
@@ -28,9 +28,21 @@ scalingLimits := rcmgr.DefaultLimits | |
// Add limits around included libp2p protocols | ||
libp2p.SetDefaultServiceLimits(&scalingLimits) | ||
|
||
// Turn the scaling limits into a static set of limits using `.AutoScale`. This | ||
// Turn the scaling limits into a concrete set of limits using `.AutoScale`. This | ||
// scales the limits proportional to your system memory. | ||
limits := scalingLimits.AutoScale() | ||
scaledDefaultLimits := scalingLimits.AutoScale() | ||
|
||
// Tweak certain settings | ||
cfg := rcmgr.PartialLimitConfig{ | ||
System: &rcmgr.ResourceLimits{ | ||
// Allow unlimited outbound streams | ||
StreamsOutbound: rcmgr.Unlimited, | ||
}, | ||
// Everything else is default. The exact values will come from `scaledDefaultLimits` above. | ||
} | ||
|
||
// Create our limits by using our cfg and replacing the default values with values from `scaledDefaultLimits` | ||
limits := cfg.Build(scaledDefaultLimits) | ||
|
||
// The resource manager expects a limiter, se we create one from our limits. | ||
limiter := rcmgr.NewFixedLimiter(limits) | ||
|
@@ -51,6 +63,54 @@ if err != nil { | |
host, err := libp2p.New(libp2p.ResourceManager(rm)) | ||
``` | ||
|
||
### Saving the limits config | ||
The easiest way to save the defined limits is to serialize the `PartialLimitConfig` | ||
type as JSON. | ||
|
||
```go | ||
noisyNeighbor, _ := peer.Decode("QmVvtzcZgCkMnSFf2dnrBPXrWuNFWNM9J3MpZQCvWPuVZf") | ||
cfg := rcmgr.PartialLimitConfig{ | ||
System: &rcmgr.ResourceLimits{ | ||
// Allow unlimited outbound streams | ||
StreamsOutbound: rcmgr.Unlimited, | ||
}, | ||
Peer: map[peer.ID]rcmgr.ResourceLimits{ | ||
noisyNeighbor: { | ||
// No inbound connections from this peer | ||
ConnsInbound: rcmgr.BlockAllLimit, | ||
// But let me open connections to them | ||
Conns: rcmgr.DefaultLimit, | ||
ConnsOutbound: rcmgr.DefaultLimit, | ||
// No inbound streams from this peer | ||
StreamsInbound: rcmgr.BlockAllLimit, | ||
// And let me open unlimited (by me) outbound streams (the peer may have their own limits on me) | ||
StreamsOutbound: rcmgr.Unlimited, | ||
}, | ||
}, | ||
} | ||
jsonBytes, _ := json.Marshal(&cfg) | ||
|
||
// string(jsonBytes) | ||
// { | ||
// "System": { | ||
// "StreamsOutbound": "unlimited" | ||
// }, | ||
// "Peer": { | ||
// "QmVvtzcZgCkMnSFf2dnrBPXrWuNFWNM9J3MpZQCvWPuVZf": { | ||
// "StreamsInbound": "blockAll", | ||
// "StreamsOutbound": "unlimited", | ||
// "ConnsInbound": "blockAll" | ||
Comment on lines
+100
to
+102
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. Is the order that they serialize as? If we are in control, I think it would be good to go from outwards to in Conns |
||
// } | ||
// } | ||
// } | ||
``` | ||
|
||
This will omit defaults from the JSON output. It will also serialize the | ||
blockAll, and unlimited values explicitly. | ||
|
||
The `Memory` field is serialized as a string to workaround the JSON limitation | ||
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. To drive this home, maybe set a Memory limit in your example above so it shows up in the serialization? |
||
of 32 bit integers (`Memory` is an int64). | ||
|
||
## Basic Resources | ||
|
||
### Memory | ||
|
@@ -278,7 +338,7 @@ This is done using the `ScalingLimitConfig`. For every scope, this configuration | |
struct defines the absolutely bare minimum limits, and an (optional) increase of | ||
these limits, which will be applied on nodes that have sufficient memory. | ||
|
||
A `ScalingLimitConfig` can be converted into a `LimitConfig` (which can then be | ||
A `ScalingLimitConfig` can be converted into a `ConcreteLimitConfig` (which can then be | ||
used to initialize a fixed limiter with `NewFixedLimiter`) by calling the `Scale` method. | ||
The `Scale` method takes two parameters: the amount of memory and the number of file | ||
descriptors that an application is willing to dedicate to libp2p. | ||
Comment on lines
+341
to
344
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. (minor) Maybe also mention the AutoScale method for added convenience? |
||
|
@@ -346,7 +406,7 @@ go-libp2p process. For the default definitions see [`DefaultLimits` and | |
|
||
If the defaults seem mostly okay, but you want to adjust one facet you can | ||
simply copy the default struct object and update the field you want to change. You can | ||
apply changes to a `BaseLimit`, `BaseLimitIncrease`, and `LimitConfig` with | ||
apply changes to a `BaseLimit`, `BaseLimitIncrease`, and `ConcreteLimitConfig` with | ||
`.Apply`. | ||
|
||
Example | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"System": { | ||
"Memory": 65536, | ||
"Conns": 16, | ||
"ConnsInbound": 8, | ||
"ConnsOutbound": 16, | ||
"FD": 16 | ||
}, | ||
"ServiceDefault": { | ||
"Memory": 8765 | ||
}, | ||
"Service": { | ||
"A": { | ||
"Memory": 8192 | ||
}, | ||
"B": {} | ||
}, | ||
"ServicePeerDefault": { | ||
"Memory": 2048 | ||
}, | ||
"ServicePeer": { | ||
"A": { | ||
"Memory": 4096 | ||
} | ||
}, | ||
"ProtocolDefault": { | ||
"Memory": 2048 | ||
}, | ||
"ProtocolPeerDefault": { | ||
"Memory": 1024 | ||
}, | ||
"Protocol": { | ||
"/A": { | ||
"Memory": 8192 | ||
} | ||
}, | ||
"PeerDefault": { | ||
"Memory": 4096 | ||
}, | ||
"Peer": { | ||
"12D3KooWPFH2Bx2tPfw6RLxN8k2wh47GRXgkt9yrAHU37zFwHWzS": { | ||
"Memory": 4097 | ||
} | ||
} | ||
} |
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.
Do you even have to specify rcmgr.DefaultLimit or are those just added for clarity?
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.
Just added for clarity, and to show that default doesn't get serialized into json. You'll also notice Memory is implicitly default.