Skip to content

Commit 3f28565

Browse files
committed
refactor: move default limits logic into internal package
1 parent d674d13 commit 3f28565

File tree

2 files changed

+115
-104
lines changed

2 files changed

+115
-104
lines changed

internal/limits/limits.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package limits
2+
3+
import (
4+
"github.com/libp2p/go-libp2p/core/protocol"
5+
"github.com/libp2p/go-libp2p/p2p/host/autonat"
6+
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
7+
circuit "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
8+
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
9+
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
10+
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
11+
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
12+
)
13+
14+
// SetDefaultServiceLimits sets the default limits for bundled libp2p services
15+
func SetDefaultServiceLimits(config *rcmgr.ScalingLimitConfig) {
16+
// identify
17+
config.AddServiceLimit(
18+
identify.ServiceName,
19+
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
20+
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
21+
)
22+
config.AddServicePeerLimit(
23+
identify.ServiceName,
24+
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 1 << 20},
25+
rcmgr.BaseLimitIncrease{},
26+
)
27+
for _, id := range [...]protocol.ID{identify.ID, identify.IDPush} {
28+
config.AddProtocolLimit(
29+
id,
30+
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
31+
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
32+
)
33+
config.AddProtocolPeerLimit(
34+
id,
35+
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 32 * (256<<20 + 16<<10)},
36+
rcmgr.BaseLimitIncrease{},
37+
)
38+
}
39+
40+
// ping
41+
addServiceAndProtocolLimit(config,
42+
ping.ServiceName, ping.ID,
43+
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
44+
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
45+
)
46+
addServicePeerAndProtocolPeerLimit(
47+
config,
48+
ping.ServiceName, ping.ID,
49+
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 3, Streams: 4, Memory: 32 * (256<<20 + 16<<10)},
50+
rcmgr.BaseLimitIncrease{},
51+
)
52+
53+
// autonat
54+
addServiceAndProtocolLimit(config,
55+
autonat.ServiceName, autonat.AutoNATProto,
56+
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
57+
rcmgr.BaseLimitIncrease{StreamsInbound: 4, StreamsOutbound: 4, Streams: 4, Memory: 2 << 20},
58+
)
59+
addServicePeerAndProtocolPeerLimit(
60+
config,
61+
autonat.ServiceName, autonat.AutoNATProto,
62+
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
63+
rcmgr.BaseLimitIncrease{},
64+
)
65+
66+
// holepunch
67+
addServiceAndProtocolLimit(config,
68+
holepunch.ServiceName, holepunch.Protocol,
69+
rcmgr.BaseLimit{StreamsInbound: 32, StreamsOutbound: 32, Streams: 64, Memory: 4 << 20},
70+
rcmgr.BaseLimitIncrease{StreamsInbound: 8, StreamsOutbound: 8, Streams: 16, Memory: 4 << 20},
71+
)
72+
addServicePeerAndProtocolPeerLimit(config,
73+
holepunch.ServiceName, holepunch.Protocol,
74+
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
75+
rcmgr.BaseLimitIncrease{},
76+
)
77+
78+
// relay/v2
79+
config.AddServiceLimit(
80+
relayv2.ServiceName,
81+
rcmgr.BaseLimit{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
82+
rcmgr.BaseLimitIncrease{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
83+
)
84+
config.AddServicePeerLimit(
85+
relayv2.ServiceName,
86+
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 1 << 20},
87+
rcmgr.BaseLimitIncrease{},
88+
)
89+
90+
// circuit protocols, both client and service
91+
for _, proto := range [...]protocol.ID{circuit.ProtoIDv2Hop, circuit.ProtoIDv2Stop} {
92+
config.AddProtocolLimit(
93+
proto,
94+
rcmgr.BaseLimit{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
95+
rcmgr.BaseLimitIncrease{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
96+
)
97+
config.AddProtocolPeerLimit(
98+
proto,
99+
rcmgr.BaseLimit{StreamsInbound: 128, StreamsOutbound: 128, Streams: 128, Memory: 32 << 20},
100+
rcmgr.BaseLimitIncrease{},
101+
)
102+
}
103+
}
104+
105+
func addServiceAndProtocolLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
106+
config.AddServiceLimit(service, limit, increase)
107+
config.AddProtocolLimit(proto, limit, increase)
108+
}
109+
110+
func addServicePeerAndProtocolPeerLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
111+
config.AddServicePeerLimit(service, limit, increase)
112+
config.AddProtocolPeerLimit(proto, limit, increase)
113+
}

limits.go

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,11 @@
11
package libp2p
22

33
import (
4-
"github.com/libp2p/go-libp2p/core/protocol"
5-
"github.com/libp2p/go-libp2p/p2p/host/autonat"
4+
"github.com/libp2p/go-libp2p/internal/limits"
65
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
7-
circuit "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
8-
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
9-
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
10-
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
11-
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
126
)
137

148
// SetDefaultServiceLimits sets the default limits for bundled libp2p services
159
func SetDefaultServiceLimits(config *rcmgr.ScalingLimitConfig) {
16-
// identify
17-
config.AddServiceLimit(
18-
identify.ServiceName,
19-
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
20-
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
21-
)
22-
config.AddServicePeerLimit(
23-
identify.ServiceName,
24-
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 1 << 20},
25-
rcmgr.BaseLimitIncrease{},
26-
)
27-
for _, id := range [...]protocol.ID{identify.ID, identify.IDPush} {
28-
config.AddProtocolLimit(
29-
id,
30-
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
31-
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
32-
)
33-
config.AddProtocolPeerLimit(
34-
id,
35-
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 32 * (256<<20 + 16<<10)},
36-
rcmgr.BaseLimitIncrease{},
37-
)
38-
}
39-
40-
// ping
41-
addServiceAndProtocolLimit(config,
42-
ping.ServiceName, ping.ID,
43-
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
44-
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
45-
)
46-
addServicePeerAndProtocolPeerLimit(
47-
config,
48-
ping.ServiceName, ping.ID,
49-
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 3, Streams: 4, Memory: 32 * (256<<20 + 16<<10)},
50-
rcmgr.BaseLimitIncrease{},
51-
)
52-
53-
// autonat
54-
addServiceAndProtocolLimit(config,
55-
autonat.ServiceName, autonat.AutoNATProto,
56-
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
57-
rcmgr.BaseLimitIncrease{StreamsInbound: 4, StreamsOutbound: 4, Streams: 4, Memory: 2 << 20},
58-
)
59-
addServicePeerAndProtocolPeerLimit(
60-
config,
61-
autonat.ServiceName, autonat.AutoNATProto,
62-
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
63-
rcmgr.BaseLimitIncrease{},
64-
)
65-
66-
// holepunch
67-
addServiceAndProtocolLimit(config,
68-
holepunch.ServiceName, holepunch.Protocol,
69-
rcmgr.BaseLimit{StreamsInbound: 32, StreamsOutbound: 32, Streams: 64, Memory: 4 << 20},
70-
rcmgr.BaseLimitIncrease{StreamsInbound: 8, StreamsOutbound: 8, Streams: 16, Memory: 4 << 20},
71-
)
72-
addServicePeerAndProtocolPeerLimit(config,
73-
holepunch.ServiceName, holepunch.Protocol,
74-
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
75-
rcmgr.BaseLimitIncrease{},
76-
)
77-
78-
// relay/v2
79-
config.AddServiceLimit(
80-
relayv2.ServiceName,
81-
rcmgr.BaseLimit{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
82-
rcmgr.BaseLimitIncrease{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
83-
)
84-
config.AddServicePeerLimit(
85-
relayv2.ServiceName,
86-
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 1 << 20},
87-
rcmgr.BaseLimitIncrease{},
88-
)
89-
90-
// circuit protocols, both client and service
91-
for _, proto := range [...]protocol.ID{circuit.ProtoIDv2Hop, circuit.ProtoIDv2Stop} {
92-
config.AddProtocolLimit(
93-
proto,
94-
rcmgr.BaseLimit{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
95-
rcmgr.BaseLimitIncrease{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
96-
)
97-
config.AddProtocolPeerLimit(
98-
proto,
99-
rcmgr.BaseLimit{StreamsInbound: 128, StreamsOutbound: 128, Streams: 128, Memory: 32 << 20},
100-
rcmgr.BaseLimitIncrease{},
101-
)
102-
}
103-
}
104-
105-
func addServiceAndProtocolLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
106-
config.AddServiceLimit(service, limit, increase)
107-
config.AddProtocolLimit(proto, limit, increase)
108-
}
109-
110-
func addServicePeerAndProtocolPeerLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
111-
config.AddServicePeerLimit(service, limit, increase)
112-
config.AddProtocolPeerLimit(proto, limit, increase)
10+
limits.SetDefaultServiceLimits(config)
11311
}

0 commit comments

Comments
 (0)