2022-04-01 16:16:46 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
import (
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2022-04-01 16:16:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/autonat"
|
2022-11-04 13:57:20 +00:00
|
|
|
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
|
2022-04-01 16:16:46 +00:00
|
|
|
circuit "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
|
|
|
|
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetDefaultServiceLimits sets the default limits for bundled libp2p services
|
2022-08-19 16:34:07 +00:00
|
|
|
func SetDefaultServiceLimits(config *rcmgr.ScalingLimitConfig) {
|
2022-04-01 16:16:46 +00:00
|
|
|
// identify
|
2022-08-19 16:34:07 +00:00
|
|
|
config.AddServiceLimit(
|
|
|
|
identify.ServiceName,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
|
|
|
|
)
|
|
|
|
config.AddServicePeerLimit(
|
|
|
|
identify.ServiceName,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 1 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2023-02-22 21:58:17 +00:00
|
|
|
for _, id := range [...]protocol.ID{identify.ID, identify.IDPush} {
|
2022-08-19 16:34:07 +00:00
|
|
|
config.AddProtocolLimit(
|
|
|
|
id,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 128, Memory: 4 << 20},
|
|
|
|
)
|
|
|
|
config.AddProtocolPeerLimit(
|
|
|
|
id,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 16, StreamsOutbound: 16, Streams: 32, Memory: 32 * (256<<20 + 16<<10)},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// ping
|
|
|
|
addServiceAndProtocolLimit(config,
|
|
|
|
ping.ServiceName, ping.ID,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
|
|
|
|
)
|
|
|
|
addServicePeerAndProtocolPeerLimit(
|
|
|
|
config,
|
|
|
|
ping.ServiceName, ping.ID,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 3, Streams: 4, Memory: 32 * (256<<20 + 16<<10)},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
// autonat
|
2022-08-19 16:34:07 +00:00
|
|
|
addServiceAndProtocolLimit(config,
|
|
|
|
autonat.ServiceName, autonat.AutoNATProto,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 4 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 4, StreamsOutbound: 4, Streams: 4, Memory: 2 << 20},
|
|
|
|
)
|
|
|
|
addServicePeerAndProtocolPeerLimit(
|
|
|
|
config,
|
|
|
|
autonat.ServiceName, autonat.AutoNATProto,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
// holepunch
|
2022-08-19 16:34:07 +00:00
|
|
|
addServiceAndProtocolLimit(config,
|
|
|
|
holepunch.ServiceName, holepunch.Protocol,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 32, StreamsOutbound: 32, Streams: 64, Memory: 4 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 8, StreamsOutbound: 8, Streams: 16, Memory: 4 << 20},
|
|
|
|
)
|
|
|
|
addServicePeerAndProtocolPeerLimit(config,
|
|
|
|
holepunch.ServiceName, holepunch.Protocol,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 2, StreamsOutbound: 2, Streams: 2, Memory: 1 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
// relay/v2
|
2022-08-19 16:34:07 +00:00
|
|
|
config.AddServiceLimit(
|
|
|
|
relayv2.ServiceName,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 256, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20},
|
|
|
|
)
|
|
|
|
config.AddServicePeerLimit(
|
|
|
|
relayv2.ServiceName,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 64, StreamsOutbound: 64, Streams: 64, Memory: 1 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
// circuit protocols, both client and service
|
2023-04-07 18:23:07 +00:00
|
|
|
for _, proto := range [...]protocol.ID{circuit.ProtoIDv2Hop, circuit.ProtoIDv2Stop} {
|
2022-08-19 16:34:07 +00:00
|
|
|
config.AddProtocolLimit(
|
|
|
|
proto,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{StreamsInbound: 640, StreamsOutbound: 640, Streams: 640, Memory: 16 << 20},
|
|
|
|
)
|
|
|
|
config.AddProtocolPeerLimit(
|
|
|
|
proto,
|
|
|
|
rcmgr.BaseLimit{StreamsInbound: 128, StreamsOutbound: 128, Streams: 128, Memory: 32 << 20},
|
|
|
|
rcmgr.BaseLimitIncrease{},
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func addServiceAndProtocolLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
|
|
|
|
config.AddServiceLimit(service, limit, increase)
|
|
|
|
config.AddProtocolLimit(proto, limit, increase)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func addServicePeerAndProtocolPeerLimit(config *rcmgr.ScalingLimitConfig, service string, proto protocol.ID, limit rcmgr.BaseLimit, increase rcmgr.BaseLimitIncrease) {
|
|
|
|
config.AddServicePeerLimit(service, limit, increase)
|
|
|
|
config.AddProtocolPeerLimit(proto, limit, increase)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|