github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/libp2p/rcmgr/defaults.go (about) 1 package rcmgr 2 3 import ( 4 "github.com/filecoin-project/bacalhau/pkg/transport/bprotocol" 5 "github.com/libp2p/go-libp2p" 6 libp2p_rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" 7 "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs" 8 "github.com/rs/zerolog/log" 9 "go.opencensus.io/stats/view" 10 ) 11 12 func SetDefaultServiceLimits(config *libp2p_rcmgr.ScalingLimitConfig) { 13 // Requester -> Compute nodes 14 // reasoning behind these limits: 15 // - Requester nodes should have a high number of outbound streams to compute nodes 16 // - Compute nodes should have a high number of inbound streams from requester nodes 17 // - Since there are few requester nodes in the network, we should set a high limit for the number of streams per peer 18 config.AddServiceLimit( 19 bprotocol.ComputeServiceName, 20 libp2p_rcmgr.BaseLimit{StreamsInbound: 1024, StreamsOutbound: 4096, Streams: 4096}, 21 libp2p_rcmgr.BaseLimitIncrease{StreamsInbound: 512, StreamsOutbound: 2048, Streams: 2048}, 22 ) 23 config.AddServicePeerLimit( 24 bprotocol.ComputeServiceName, 25 libp2p_rcmgr.BaseLimit{StreamsInbound: 1024, StreamsOutbound: 1024, Streams: 1024}, 26 libp2p_rcmgr.BaseLimitIncrease{StreamsInbound: 128, StreamsOutbound: 128, Streams: 128}, 27 ) 28 29 // Compute -> Requester nodes 30 // reasoning behind these limits: 31 // - Compute nodes should have a high number of outbound streams to requester nodes 32 // - Requester nodes should have a high number of inbound streams from compute nodes 33 // - Since there are few requester nodes in the network, we should set a high limit for the number of streams per peer 34 config.AddServiceLimit( 35 bprotocol.CallbackServiceName, 36 libp2p_rcmgr.BaseLimit{StreamsInbound: 4096, StreamsOutbound: 1024, Streams: 4096}, 37 libp2p_rcmgr.BaseLimitIncrease{StreamsInbound: 2048, StreamsOutbound: 512, Streams: 2048}, 38 ) 39 config.AddServicePeerLimit( 40 bprotocol.CallbackServiceName, 41 libp2p_rcmgr.BaseLimit{StreamsInbound: 1024, StreamsOutbound: 1024, Streams: 1024}, 42 libp2p_rcmgr.BaseLimitIncrease{StreamsInbound: 128, StreamsOutbound: 128, Streams: 128}, 43 ) 44 } 45 46 var DefaultResourceManager = func(cfg *libp2p.Config) error { 47 // Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB 48 limits := libp2p_rcmgr.DefaultLimits 49 libp2p.SetDefaultServiceLimits(&limits) 50 SetDefaultServiceLimits(&limits) 51 52 // Hook up the trace reporter metrics. This will expose all opencensus 53 // stats via the default prometheus registry. See https://opencensus.io/exporters/supported-exporters/go/prometheus/ for other options. 54 err := view.Register(obs.DefaultViews...) 55 if err != nil { 56 log.Warn().Err(err).Msg("failed to register resource manager metrics") 57 } 58 59 str, err := obs.NewStatsTraceReporter() 60 if err != nil { 61 return err 62 } 63 mgr, err := libp2p_rcmgr.NewResourceManager( 64 libp2p_rcmgr.NewFixedLimiter(limits.AutoScale()), 65 libp2p_rcmgr.WithTraceReporter(str), 66 ) 67 if err != nil { 68 return err 69 } 70 71 return cfg.Apply(libp2p.ResourceManager(mgr)) 72 }