Skip to content

Commit 73eac89

Browse files
committed
server: use log closures for better performance
To avoid needing to call spew.Sdump() when the trace log level isn't even being used, we wrap the calls in a function closure instead. That way the potentially CPU intensive spew only happens when the trace log level is actually enabled.
1 parent 6f8e3bd commit 73eac89

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

server.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/btcsuite/btcd/btcutil"
1313
"github.com/btcsuite/btcd/chaincfg/chainhash"
1414
"github.com/btcsuite/btcd/wire"
15-
"github.com/davecgh/go-spew/spew"
1615
proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1716
"github.com/lightninglabs/lndclient"
1817
"github.com/lightninglabs/taproot-assets/address"
@@ -34,6 +33,7 @@ import (
3433
"github.com/lightningnetwork/lnd/lncfg"
3534
"github.com/lightningnetwork/lnd/lnrpc"
3635
"github.com/lightningnetwork/lnd/lntypes"
36+
"github.com/lightningnetwork/lnd/lnutils"
3737
"github.com/lightningnetwork/lnd/lnwallet"
3838
lnwl "github.com/lightningnetwork/lnd/lnwallet"
3939
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
@@ -997,10 +997,11 @@ func (s *Server) ChannelFinalized(pid funding.PendingChanID) error {
997997
//
998998
// NOTE: This method is part of the routing.TlvTrafficShaper interface.
999999
func (s *Server) ShouldHandleTraffic(cid lnwire.ShortChannelID,
1000-
fundingBlob, _ lfn.Option[tlv.Blob]) (bool, error) {
1000+
fundingBlob, htlcBlob lfn.Option[tlv.Blob]) (bool, error) {
10011001

1002-
srvrLog.Debugf("HandleTraffic called (cid=%v, fundingBlob=%x)", cid,
1003-
fundingBlob.UnwrapOr(tlv.Blob{}))
1002+
srvrLog.Debugf("HandleTraffic called (cid=%v, fundingBlob=%v, "+
1003+
"htlcBlob=%v)", cid, lnutils.SpewLogClosure(fundingBlob),
1004+
lnutils.SpewLogClosure(htlcBlob))
10041005

10051006
if err := s.waitForReady(); err != nil {
10061007
return false, err
@@ -1021,8 +1022,8 @@ func (s *Server) PaymentBandwidth(htlcBlob, commitmentBlob lfn.Option[tlv.Blob],
10211022
htlcView lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error) {
10221023

10231024
srvrLog.Debugf("PaymentBandwidth called, htlcBlob=%v, "+
1024-
"commitmentBlob=%v", spew.Sdump(htlcBlob),
1025-
spew.Sdump(commitmentBlob))
1025+
"commitmentBlob=%v", lnutils.SpewLogClosure(htlcBlob),
1026+
lnutils.SpewLogClosure(commitmentBlob))
10261027

10271028
if err := s.waitForReady(); err != nil {
10281029
return 0, err
@@ -1043,7 +1044,8 @@ func (s *Server) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
10431044
lnwire.CustomRecords, error) {
10441045

10451046
srvrLog.Debugf("ProduceHtlcExtraData called, totalAmount=%d, "+
1046-
"htlcBlob=%v", totalAmount, spew.Sdump(htlcCustomRecords))
1047+
"htlcBlob=%v", totalAmount,
1048+
lnutils.SpewLogClosure(htlcCustomRecords))
10471049

10481050
if err := s.waitForReady(); err != nil {
10491051
return 0, nil, err
@@ -1074,7 +1076,8 @@ func (s *Server) AuxCloseOutputs(
10741076
desc chancloser.AuxCloseDesc) (lfn.Option[chancloser.AuxCloseOutputs],
10751077
error) {
10761078

1077-
srvrLog.Tracef("AuxCloseOutputs called, desc=%v", spew.Sdump(desc))
1079+
srvrLog.Tracef("AuxCloseOutputs called, desc=%v",
1080+
lnutils.SpewLogClosure(desc))
10781081

10791082
if err := s.waitForReady(); err != nil {
10801083
return lfn.None[chancloser.AuxCloseOutputs](), err
@@ -1091,7 +1094,8 @@ func (s *Server) ShutdownBlob(
10911094
req chancloser.AuxShutdownReq) (lfn.Option[lnwire.CustomRecords],
10921095
error) {
10931096

1094-
srvrLog.Tracef("ShutdownBlob called, req=%v", spew.Sdump(req))
1097+
srvrLog.Tracef("ShutdownBlob called, req=%v",
1098+
lnutils.SpewLogClosure(req))
10951099

10961100
if err := s.waitForReady(); err != nil {
10971101
return lfn.None[lnwire.CustomRecords](), err
@@ -1109,7 +1113,7 @@ func (s *Server) FinalizeClose(desc chancloser.AuxCloseDesc,
11091113
closeTx *wire.MsgTx) error {
11101114

11111115
srvrLog.Tracef("FinalizeClose called, desc=%v, closeTx=%v",
1112-
spew.Sdump(desc), spew.Sdump(closeTx))
1116+
lnutils.SpewLogClosure(desc), lnutils.SpewLogClosure(closeTx))
11131117

11141118
if err := s.waitForReady(); err != nil {
11151119
return err
@@ -1123,7 +1127,8 @@ func (s *Server) FinalizeClose(desc chancloser.AuxCloseDesc,
11231127
//
11241128
// NOTE: This method is part of the lnwallet.AuxContractResolver interface.
11251129
func (s *Server) ResolveContract(req lnwl.ResolutionReq) lfn.Result[tlv.Blob] {
1126-
srvrLog.Tracef("ResolveContract called, req=%v", spew.Sdump(req))
1130+
srvrLog.Tracef("ResolveContract called, req=%v",
1131+
lnutils.SpewLogClosure(req))
11271132

11281133
if err := s.waitForReady(); err != nil {
11291134
return lfn.Err[tlv.Blob](err)
@@ -1141,7 +1146,7 @@ func (s *Server) DeriveSweepAddr(inputs []input.Input,
11411146
change lnwl.AddrWithKey) lfn.Result[sweep.SweepOutput] {
11421147

11431148
srvrLog.Tracef("DeriveSweepAddr called, inputs=%v, change=%v",
1144-
spew.Sdump(inputs), spew.Sdump(change))
1149+
lnutils.SpewLogClosure(inputs), lnutils.SpewLogClosure(change))
11451150

11461151
if err := s.waitForReady(); err != nil {
11471152
return lfn.Err[sweep.SweepOutput](err)
@@ -1158,7 +1163,7 @@ func (s *Server) ExtraBudgetForInputs(
11581163
inputs []input.Input) lfn.Result[btcutil.Amount] {
11591164

11601165
srvrLog.Tracef("ExtraBudgetForInputs called, inputs=%v",
1161-
spew.Sdump(inputs))
1166+
lnutils.SpewLogClosure(inputs))
11621167

11631168
if err := s.waitForReady(); err != nil {
11641169
return lfn.Err[btcutil.Amount](err)
@@ -1176,8 +1181,9 @@ func (s *Server) NotifyBroadcast(req *sweep.BumpRequest,
11761181
outpointToTxIndex map[wire.OutPoint]int) error {
11771182

11781183
srvrLog.Tracef("NotifyBroadcast called, req=%v, tx=%v, fee=%v, "+
1179-
"out_index=%v", spew.Sdump(req), spew.Sdump(tx), fee,
1180-
spew.Sdump(outpointToTxIndex))
1184+
"out_index=%v", lnutils.SpewLogClosure(req),
1185+
lnutils.SpewLogClosure(tx), fee,
1186+
lnutils.SpewLogClosure(outpointToTxIndex))
11811187

11821188
if err := s.waitForReady(); err != nil {
11831189
return err

0 commit comments

Comments
 (0)