github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/logging/logging.go (about)

     1  package p2plogging
     2  
     3  import (
     4  	"github.com/libp2p/go-libp2p/core/peer"
     5  
     6  	"github.com/onflow/flow-go/network/p2p/logging/internal"
     7  )
     8  
     9  // peerIdCache is a global cache of peer ids, it is used to avoid expensive base58 encoding of peer ids.
    10  var peerIdCache *internal.PeerIdCache
    11  
    12  // init is called before the package is initialized. This is used to initialize
    13  // the peer id cache before any other code is run, so that the cache is ready
    14  // to use.
    15  func init() {
    16  	cache, err := internal.NewPeerIdCache(10_000)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	peerIdCache = cache
    21  }
    22  
    23  // PeerId is a logger helper that returns the base58 encoded peer id string, it looks up the peer id in a cache to avoid
    24  // expensive base58 encoding, and caches the result for future use in case of a cache miss.
    25  func PeerId(pid peer.ID) string {
    26  	return peerIdCache.PeerIdString(pid)
    27  }