github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/builder/utils.go (about) 1 package p2pbuilder 2 3 import ( 4 "fmt" 5 6 "github.com/libp2p/go-libp2p/core/peer" 7 "github.com/libp2p/go-libp2p/core/protocol" 8 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" 9 "github.com/rs/zerolog" 10 11 "github.com/onflow/flow-go/module" 12 "github.com/onflow/flow-go/network/p2p" 13 p2plogging "github.com/onflow/flow-go/network/p2p/logging" 14 ) 15 16 const keyResourceManagerLimit = "libp2p_resource_manager_limit" 17 18 // notEjectedPeerFilter returns a PeerFilter that will return an error if the peer is unknown or ejected. 19 func notEjectedPeerFilter(idProvider module.IdentityProvider) p2p.PeerFilter { 20 return func(p peer.ID) error { 21 if id, found := idProvider.ByPeerID(p); !found { 22 return fmt.Errorf("failed to get identity of unknown peer with peer id %s", p2plogging.PeerId(p)) 23 } else if id.IsEjected() { 24 return fmt.Errorf("peer %s with node id %s is ejected", p2plogging.PeerId(p), id.NodeID.String()) 25 } 26 27 return nil 28 } 29 } 30 31 type limitConfigLogger struct { 32 logger zerolog.Logger 33 } 34 35 // newLimitConfigLogger creates a new limitConfigLogger. 36 func newLimitConfigLogger(logger zerolog.Logger) *limitConfigLogger { 37 return &limitConfigLogger{logger: logger} 38 } 39 40 // withBaseLimit appends the base limit to the logger with the given prefix. 41 func (l *limitConfigLogger) withBaseLimit(prefix string, baseLimit rcmgr.ResourceLimits) zerolog.Logger { 42 return l.logger.With(). 43 Str("key", keyResourceManagerLimit). 44 Str(fmt.Sprintf("%s_streams", prefix), fmt.Sprintf("%v", baseLimit.Streams)). 45 Str(fmt.Sprintf("%s_streams_inbound", prefix), fmt.Sprintf("%v", baseLimit.StreamsInbound)). 46 Str(fmt.Sprintf("%s_streams_outbound", prefix), fmt.Sprintf("%v,", baseLimit.StreamsOutbound)). 47 Str(fmt.Sprintf("%s_conns", prefix), fmt.Sprintf("%v", baseLimit.Conns)). 48 Str(fmt.Sprintf("%s_conns_inbound", prefix), fmt.Sprintf("%v", baseLimit.ConnsInbound)). 49 Str(fmt.Sprintf("%s_conns_outbound", prefix), fmt.Sprintf("%v", baseLimit.ConnsOutbound)). 50 Str(fmt.Sprintf("%s_file_descriptors", prefix), fmt.Sprintf("%v", baseLimit.FD)). 51 Str(fmt.Sprintf("%s_memory", prefix), fmt.Sprintf("%v", baseLimit.Memory)).Logger() 52 } 53 54 func (l *limitConfigLogger) LogResourceManagerLimits(config rcmgr.ConcreteLimitConfig) { 55 // PartialLimit config is the same as ConcreteLimit config, but with the exported fields. 56 pCfg := config.ToPartialLimitConfig() 57 l.logGlobalResourceLimits(pCfg) 58 l.logServiceLimits(pCfg.Service) 59 l.logProtocolLimits(pCfg.Protocol) 60 l.logPeerLimits(pCfg.Peer) 61 l.logPeerProtocolLimits(pCfg.ProtocolPeer) 62 } 63 64 func (l *limitConfigLogger) logGlobalResourceLimits(config rcmgr.PartialLimitConfig) { 65 lg := l.withBaseLimit("system", config.System) 66 lg.Info().Msg("system limits set") 67 68 lg = l.withBaseLimit("transient", config.Transient) 69 lg.Info().Msg("transient limits set") 70 71 lg = l.withBaseLimit("allowed_listed_system", config.AllowlistedSystem) 72 lg.Info().Msg("allowed listed system limits set") 73 74 lg = l.withBaseLimit("allowed_lister_transient", config.AllowlistedTransient) 75 lg.Info().Msg("allowed listed transient limits set") 76 77 lg = l.withBaseLimit("service_default", config.ServiceDefault) 78 lg.Info().Msg("service default limits set") 79 80 lg = l.withBaseLimit("service_peer_default", config.ServicePeerDefault) 81 lg.Info().Msg("service peer default limits set") 82 83 lg = l.withBaseLimit("protocol_default", config.ProtocolDefault) 84 lg.Info().Msg("protocol default limits set") 85 86 lg = l.withBaseLimit("protocol_peer_default", config.ProtocolPeerDefault) 87 lg.Info().Msg("protocol peer default limits set") 88 89 lg = l.withBaseLimit("peer_default", config.PeerDefault) 90 lg.Info().Msg("peer default limits set") 91 92 lg = l.withBaseLimit("connections", config.Conn) 93 lg.Info().Msg("connection limits set") 94 95 lg = l.withBaseLimit("streams", config.Stream) 96 lg.Info().Msg("stream limits set") 97 } 98 99 func (l *limitConfigLogger) logServiceLimits(s map[string]rcmgr.ResourceLimits) { 100 for sName, sLimits := range s { 101 lg := l.withBaseLimit(fmt.Sprintf("service_%s", sName), sLimits) 102 lg.Info().Msg("service limits set") 103 } 104 } 105 106 func (l *limitConfigLogger) logProtocolLimits(p map[protocol.ID]rcmgr.ResourceLimits) { 107 for pName, pLimits := range p { 108 lg := l.withBaseLimit(fmt.Sprintf("protocol_%s", pName), pLimits) 109 lg.Info().Msg("protocol limits set") 110 } 111 } 112 113 func (l *limitConfigLogger) logPeerLimits(p map[peer.ID]rcmgr.ResourceLimits) { 114 for pId, pLimits := range p { 115 lg := l.withBaseLimit(fmt.Sprintf("peer_%s", p2plogging.PeerId(pId)), pLimits) 116 lg.Info().Msg("peer limits set") 117 } 118 } 119 120 func (l *limitConfigLogger) logPeerProtocolLimits(p map[protocol.ID]rcmgr.ResourceLimits) { 121 for pName, pLimits := range p { 122 lg := l.withBaseLimit(fmt.Sprintf("protocol_peer_%s", pName), pLimits) 123 lg.Info().Msg("protocol peer limits set") 124 } 125 }