fix: "Lazy" Stringers to defer Sprintf and Hash until logs print (#8845)

This commit is contained in:
Joe Abbey
2022-06-23 14:56:34 -04:00
committed by GitHub
parent a3cc3d98b9
commit 4a1df4911d
23 changed files with 226 additions and 115 deletions

View File

@@ -352,7 +352,7 @@ func (c *MConnection) Send(chID byte, msgBytes []byte) bool {
return false
}
c.Logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes))
c.Logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes))
// Send message to channel.
channel, ok := c.channelsIdx[chID]
@@ -369,7 +369,7 @@ func (c *MConnection) Send(chID byte, msgBytes []byte) bool {
default:
}
} else {
c.Logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes))
c.Logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes))
}
return success
}
@@ -381,7 +381,7 @@ func (c *MConnection) TrySend(chID byte, msgBytes []byte) bool {
return false
}
c.Logger.Debug("TrySend", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes))
c.Logger.Debug("TrySend", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes))
// Send message to channel.
channel, ok := c.channelsIdx[chID]

View File

@@ -17,6 +17,7 @@ import (
"github.com/minio/highwayhash"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
tmmath "github.com/tendermint/tendermint/libs/math"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/libs/service"
@@ -739,7 +740,7 @@ func (a *addrBook) expireNew(bucketIdx int) {
for addrStr, ka := range a.bucketsNew[bucketIdx] {
// If an entry is bad, throw it away
if ka.isBad() {
a.Logger.Info(fmt.Sprintf("expiring bad address %v", addrStr))
a.Logger.Info("expire new", "msg", log.NewLazySprintf("expiring bad address %v", addrStr))
a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
return
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cmap"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/p2p/conn"
@@ -261,7 +262,7 @@ func (sw *Switch) OnStop() {
//
// NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool {
sw.Logger.Debug("Broadcast", "channel", chID, "msgBytes", fmt.Sprintf("%X", msgBytes))
sw.Logger.Debug("Broadcast", "channel", chID, "msgBytes", log.NewLazySprintf("%X", msgBytes))
peers := sw.peers.List()
var wg sync.WaitGroup

View File

@@ -18,19 +18,19 @@ func makeUPNPListener(intPort int, extPort int, logger log.Logger) (NAT, net.Lis
if err != nil {
return nil, nil, nil, fmt.Errorf("nat upnp could not be discovered: %v", err)
}
logger.Info(fmt.Sprintf("ourIP: %v", nat.(*upnpNAT).ourIP))
logger.Info("make upnp listener", "msg", log.NewLazySprintf("ourIP: %v", nat.(*upnpNAT).ourIP))
ext, err := nat.GetExternalAddress()
if err != nil {
return nat, nil, nil, fmt.Errorf("external address error: %v", err)
}
logger.Info(fmt.Sprintf("External address: %v", ext))
logger.Info("make upnp listener", "msg", log.NewLazySprintf("External address: %v", ext))
port, err := nat.AddPortMapping("tcp", extPort, intPort, "Tendermint UPnP Probe", 0)
if err != nil {
return nat, nil, ext, fmt.Errorf("port mapping error: %v", err)
}
logger.Info(fmt.Sprintf("Port mapping mapped: %v", port))
logger.Info("make upnp listener", "msg", log.NewLazySprintf("Port mapping mapped: %v", port))
// also run the listener, open for all remote addresses.
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", intPort))
@@ -45,17 +45,23 @@ func testHairpin(listener net.Listener, extAddr string, logger log.Logger) (supp
go func() {
inConn, err := listener.Accept()
if err != nil {
logger.Info(fmt.Sprintf("Listener.Accept() error: %v", err))
logger.Info("test hair pin", "msg", log.NewLazySprintf("Listener.Accept() error: %v", err))
return
}
logger.Info(fmt.Sprintf("Accepted incoming connection: %v -> %v", inConn.LocalAddr(), inConn.RemoteAddr()))
logger.Info("test hair pin",
"msg",
log.NewLazySprintf("Accepted incoming connection: %v -> %v", inConn.LocalAddr(), inConn.RemoteAddr()))
buf := make([]byte, 1024)
n, err := inConn.Read(buf)
if err != nil {
logger.Info(fmt.Sprintf("Incoming connection read error: %v", err))
logger.Info("test hair pin",
"msg",
log.NewLazySprintf("Incoming connection read error: %v", err))
return
}
logger.Info(fmt.Sprintf("Incoming connection read %v bytes: %X", n, buf))
logger.Info("test hair pin",
"msg",
log.NewLazySprintf("Incoming connection read %v bytes: %X", n, buf))
if string(buf) == "test data" {
supportsHairpin = true
return
@@ -65,16 +71,16 @@ func testHairpin(listener net.Listener, extAddr string, logger log.Logger) (supp
// Establish outgoing
outConn, err := net.Dial("tcp", extAddr)
if err != nil {
logger.Info(fmt.Sprintf("Outgoing connection dial error: %v", err))
logger.Info("test hair pin", "msg", log.NewLazySprintf("Outgoing connection dial error: %v", err))
return
}
n, err := outConn.Write([]byte("test data"))
if err != nil {
logger.Info(fmt.Sprintf("Outgoing connection write error: %v", err))
logger.Info("test hair pin", "msg", log.NewLazySprintf("Outgoing connection write error: %v", err))
return
}
logger.Info(fmt.Sprintf("Outgoing connection wrote %v bytes", n))
logger.Info("test hair pin", "msg", log.NewLazySprintf("Outgoing connection wrote %v bytes", n))
// Wait for data receipt
time.Sleep(1 * time.Second)