You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: doc/LIMITS.md
+118-59
Original file line number
Diff line number
Diff line change
@@ -2,100 +2,133 @@
2
2
3
3
In order to prevent excessive resource consumption by a libp2p node it's important to understand limits are applied and how to tune them to the needs of your application.
4
4
5
+
This is important for [DoS](https://en.wikipedia.org/wiki/Denial-of-service_attack) attack mitgation - there is a more holistic discussion and general advice on that topic at [the main libp2p docs website](https://docs.libp2p.io/reference/dos-mitigation/).
-[Data transfer and Event Loop limits](#data-transfer-and-event-loop-limits)
10
13
-[Stream limits](#stream-limits)
11
14
-[Mplex](#mplex)
12
15
-[Yamux](#yamux)
13
16
-[Protocol limits](#protocol-limits)
14
-
-[Closing connections](#closing-connections)
15
17
-[Transport specific limits](#transport-specific-limits)
16
18
-[TCP](#tcp)
17
19
-[Allow/deny lists](#allowdeny-lists)
20
+
-[How much memory will be used for buffering?](#how-much-memory-will-be-used-for-buffering)
18
21
19
22
## Connection limits
20
23
21
-
It's possible to limit the amount of incoming and outgoing connections a node is able to make. When this limit is reached and an attempt to open a new connection is made, existing connections may be closed to make room for the new connection.
24
+
It's possible to limit the total amount of connections a node is able to make (combining incoming and outgoing). When this limit is reached and an attempt to open a new connection is made, existing connections may be closed to make room for the new connection (see [Closing connections][#closing-connections]).
25
+
26
+
* Note: there currently isn't a way to specify different limits for incoming vs. outgoing. Connection limits are applied across both incoming and outgoing connections combined. There is a backlog item for this [here](https://github.com/libp2p/js-libp2p/issues/1508).
22
27
23
28
We can also limit the number of connections in a "pending" state. These connections have been opened by a remote peer but peer IDs have yet to be exchanged and/or connection encryption and multiplexing negotiated. Once this limit is hit further connections will be closed unless the remote peer has an address in the [allow list](#allowdeny-lists).
24
29
25
-
```js
30
+
All fields are optional. The default values are defined in [src/connection-manager/index.ts](https://github.com/libp2p/js-libp2p/blob/master/src/connection-manager/index.ts) - please see that file for the current values.
31
+
32
+
```ts
26
33
const node =awaitcreateLibp2pNode({
27
34
connectionManager: {
28
35
/**
29
36
* The total number of connections allowed to be open at one time
30
37
*/
31
-
maxConnections:200,
38
+
maxConnections: number
32
39
33
40
/**
34
41
* If the number of open connections goes below this number, the node
35
-
* will try to connect to nearby peers from the peer store
42
+
* will try to connect to randomly selected peers from the peer store
36
43
*/
37
-
minConnections:20,
44
+
minConnections: number
38
45
39
46
/**
40
47
* How many connections can be open but not yet upgraded
41
48
*/
42
-
maxIncomingPendingConnections:10
49
+
maxIncomingPendingConnections: number
43
50
}
44
51
})
45
52
```
46
53
54
+
## Closing connections
55
+
56
+
When choosing connections to close the connection manager sorts the list of connections by the value derived from the tags given to each peer. The values of all tags are summed and connections with lower valued peers are eligible for closing first.
57
+
58
+
```js
59
+
// tag a peer
60
+
awaitlibp2p.peerStore.tagPeer(peerId, 'my-tag', {
61
+
value:50, // 0-100 is the typical value range
62
+
ttl:1000// optional field, this tag will be deleted after this many ms
63
+
})
64
+
```
65
+
47
66
## Inbound connection threshold
48
67
49
-
To prevent individual peers from opening multiple connections to a node, an `inboundConnectionThreshold` is configurable. This is the number of connections per second an individual remote host can open to a node, once this threshold is crossed all further connections opened by that host will be rejected.
68
+
To prevent individual peers from opening multiple connections to a node, an `inboundConnectionThreshold` is configurable. This is the number of connections per second an individual peer can open to a node, once this threshold is crossed all further connections opened by that peer will be rejected until the threshold resets in the next second.
50
69
70
+
All fields are optional. The default values are defined in [src/connection-manager/index.ts](https://github.com/libp2p/js-libp2p/blob/master/src/connection-manager/index.ts) - please see that file for the current values.
51
71
52
-
```js
72
+
```ts
53
73
const node =awaitcreateLibp2pNode({
54
74
connectionManager: {
55
75
/**
56
76
* A remote peer may attempt to open up to this many connections per second,
57
77
* any more than that will be automatically rejected
58
78
*/
59
-
inboundConnectionThreshold:5
79
+
inboundConnectionThreshold: number
60
80
}
61
81
})
62
82
```
63
83
64
84
## Data transfer and Event Loop limits
65
85
66
-
If metrics are enabled the node will track the amount of data being sent to and from the network. If the amount sent is over the threshold connections will be trimmed to free up resources. The default amount is `Ininity` so this must be explicitly enabled.
86
+
If metrics are enabled the node will track the amount of data being sent to and from the network. If the rate of data sent is over the threshold connections will be trimmed to free up resources. The default rate is `Ininity` so this must be explicitly enabled.
67
87
68
88
Connections may also be trimmed if [event loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick) latency exceeds the configured limit.
69
89
70
-
```js
90
+
All fields are optional. The default values are defined in [src/connection-manager/index.ts](https://github.com/libp2p/js-libp2p/blob/master/src/connection-manager/index.ts) - please see that file for the current values.
91
+
92
+
```ts
71
93
const node =awaitcreateLibp2pNode({
72
94
metrics: {
73
95
enabled: true
74
96
},
75
97
connectionManager: {
76
98
/**
77
99
* If the node transfers more than this amount of data in bytes/second
78
-
* low value connections may be closed
100
+
* connections to untagged peers or those not in the allow list may be
101
+
* closed.
102
+
*
103
+
* It is bytes per second.
79
104
*/
80
-
maxData:1024*1024,
105
+
maxData: number
81
106
82
107
/**
83
108
* If the node sends more than this amount of data in bytes/second
84
-
* low value connections may be closed
109
+
* connections to untagged peers or those not in the allow list may be
110
+
* closed.
111
+
*
112
+
* It is bytes per second.
85
113
*/
86
-
maxSentData:1024*1024
114
+
maxSentData: number
87
115
88
116
/**
89
117
* If the node receives more than this amount of data in bytes/second
90
-
* low value connections may be closed
118
+
* connections to untagged peers or those not in the allow list may be
119
+
* closed.
120
+
*
121
+
* It is bytes per second.
91
122
*/
92
-
maxReceivedData:1024*1024,
123
+
maxReceivedData: number
93
124
94
125
/**
95
-
* If the event loop takes longer than this many ms to run, low value
96
-
* connections may be closed
126
+
* If the event loop takes longer than this many ms to run, connections
127
+
* to untagged peers or those not in the allow list may be closed.
128
+
*
129
+
* It is milliseconds.
97
130
*/
98
-
maxEventLoopDelay:1000
131
+
maxEventLoopDelay: number
99
132
}
100
133
})
101
134
```
@@ -108,85 +141,88 @@ These settings are done on a per-muxer basis, please see the README of the relev
108
141
109
142
### Mplex
110
143
111
-
[@libp2p/mplex](https://github.com/libp2p/js-libp2p-mplex) supports the following:
144
+
[@libp2p/mplex](https://github.com/libp2p/js-libp2p-mplex) supports the following.
112
145
113
-
```js
146
+
All fields are optional. The default values are defined in [@libp2p/mplex/src/mplex.ts](https://github.com/libp2p/js-libp2p-mplex/blob/master/src/mplex.ts) - please see that file for the current values.
147
+
148
+
```ts
114
149
const node =awaitcreateLibp2pNode({
115
150
muxers: [
116
-
newMplex({
151
+
mplex({
117
152
/**
118
153
* The total number of inbound protocol streams that can be opened on a given connection
119
154
*/
120
-
maxInboundStreams:1024,
155
+
maxInboundStreams: number
121
156
122
157
/**
123
158
* The total number of outbound protocol streams that can be opened on a given connection
124
159
*/
125
-
maxOutboundStreams:1024,
160
+
maxOutboundStreams: number
161
+
162
+
/**
163
+
* How much incoming data in bytes to buffer while attempting to parse messages - peers sending many small messages in batches may cause this buffer to grow
164
+
*/
165
+
maxUnprocessedMessageQueueSize: number
126
166
127
167
/**
128
-
* How much incoming data to buffer before resetting the stream
168
+
* How much message data in bytes to buffer after parsing - slow stream consumers may cause this buffer to grow
129
169
*/
130
-
maxStreamBufferSize:4*1024*1024,
170
+
maxStreamBufferSize: number
131
171
132
172
/**
133
173
* Mplex does not support backpressure so to protect ourselves, if `maxInboundStreams` is
134
174
* hit and the remote opens more than this many streams per second, close the connection
135
175
*/
136
-
disconnectThreshold:5
176
+
disconnectThreshold: number
137
177
})
138
178
]
139
179
})
140
180
```
141
181
142
182
### Yamux
143
183
144
-
[@chainsafe/libp2p-yamux](https://github.com/Chainsafe/js-libp2p-yamux) supports the following:
184
+
[@chainsafe/libp2p-yamux](https://github.com/Chainsafe/js-libp2p-yamux) supports the following.
145
185
146
-
```js
186
+
All fields are optional. The default values are defined in [@chainsafe/libp2p-yamux/src/config.ts](https://github.com/ChainSafe/js-libp2p-yamux/blob/master/src/config.ts) - please see that file for the current values.
187
+
188
+
```ts
147
189
const node =awaitcreateLibp2pNode({
148
190
muxers: [
149
-
newYamux({
191
+
yamux({
150
192
/**
151
193
* The total number of inbound protocol streams that can be opened on a given connection
194
+
*
195
+
* This field is optional, the default value is shown
152
196
*/
153
-
maxInboundStreams:1024,
197
+
maxInboundStreams: number
154
198
155
199
/**
156
200
* The total number of outbound protocol streams that can be opened on a given connection
201
+
*
202
+
* This field is optional, the default value is shown
157
203
*/
158
-
maxOutboundStreams:1024
204
+
maxOutboundStreams: number
159
205
})
160
206
]
161
207
})
162
208
```
163
209
164
210
### Protocol limits
165
211
166
-
When registering listeners for custom protocols, the maximum number of simultaneously open inbound and outbound streams per-connection can be specified. If not specified these will default to 32 inbound streams and 64 outbound streams.
212
+
When registering listeners for custom protocols, the maximum number of simultaneously open inbound and outbound streams per-connection can be specified. If not specified these will default to [32 inbound streams and 64 outbound streams](https://github.com/libp2p/js-libp2p/blob/master/src/registrar.ts#L14-L15).
167
213
168
214
If more than this number of streams for the given protocol are opened on a single connection, subsequent new streams for that protocol will be immediately reset.
169
215
170
-
Since incoming stream data is buffered until it is comsumed, you should attempt to specify the minimum amount of streams required to keep memory usage to a minimum.
216
+
Since incoming stream data is buffered until it is consumed, you should attempt to specify the minimum amount of streams required to keep memory usage to a minimum.
171
217
172
-
```js
218
+
All fields are optional. The default values are defined in [src/registrar.ts](https://github.com/libp2p/js-libp2p/blob/master/src/registrar.ts) - please see that file for the current values.
When choosing connections to close the connection manager sorts the list of connections by the value derived from the tags given to each peer. The values of all tags are summed and connections with lower valued peers are elibible for closing first.
184
-
185
-
```js
186
-
// tag a peer
187
-
awaitlibp2p.peerStore.tagPeer(peerId, 'my-tag', {
188
-
value:50, // 0-100 is the typical value range
189
-
ttl:1000// optional field, this tag will be deleted after this many ms
224
+
maxInboundStreams: number
225
+
maxOutboundStreams: number
190
226
})
191
227
```
192
228
@@ -198,28 +234,30 @@ A non-exhaustive list follows:
198
234
199
235
### TCP
200
236
201
-
The [@libp2p/tcp](https://github.com/libp2p/js-libp2p-tcp) transport allows additional limits to be configured
237
+
The [@libp2p/tcp](https://github.com/libp2p/js-libp2p-tcp) transport allows additional limits to be configured.
202
238
203
-
```js
239
+
All fields are optional. The full list of options is defined in [@libp2p/tcp/src/index.ts](https://github.com/libp2p/js-libp2p-tcp/blob/master/src/index.ts) - please see that file for more details.
240
+
241
+
```ts
204
242
const node =awaitcreateLibp2pNode({
205
243
transports: [
206
-
newTCP({
244
+
tcp({
207
245
/**
208
-
* Inbound connections with no activity in this timeframe (ms) will be closed
246
+
* Inbound connections with no activity in this time frame (ms) will be closed
209
247
*/
210
-
inboundSocketInactivityTimeout:30000,
248
+
inboundSocketInactivityTimeout: number
211
249
212
250
/**
213
-
* Outbound connections with no activity in this timeframe (ms) will be closed
251
+
* Outbound connections with no activity in this time frame (ms) will be closed
214
252
*/
215
-
outboundSocketInactivityTimeout:60000,
253
+
outboundSocketInactivityTimeout: number
216
254
217
255
/**
218
256
* Once this many connections are open on this listener any further connections
219
257
* will be rejected - this will have no effect if it is larger than the value
220
258
* configured for the ConnectionManager maxConnections parameter
0 commit comments