Skip to content

Commit 443de9f

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 7e07928 commit 443de9f

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"
@@ -999,9 +999,9 @@ func (s *Server) ChannelFinalized(pid funding.PendingChanID) error {
999999
func (s *Server) ShouldHandleTraffic(cid lnwire.ShortChannelID,
10001000
fundingBlob, htlcBlob lfn.Option[tlv.Blob]) (bool, error) {
10011001

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

10061006
if err := s.waitForReady(); err != nil {
10071007
return false, err
@@ -1025,8 +1025,9 @@ func (s *Server) PaymentBandwidth(fundingBlob, htlcBlob,
10251025
htlcView lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error) {
10261026

10271027
srvrLog.Debugf("PaymentBandwidth called, fundingBlob=%v, htlcBlob=%v, "+
1028-
"commitmentBlob=%v", spew.Sdump(fundingBlob),
1029-
spew.Sdump(htlcBlob), spew.Sdump(commitmentBlob))
1028+
"commitmentBlob=%v", lnutils.SpewLogClosure(fundingBlob),
1029+
lnutils.SpewLogClosure(htlcBlob),
1030+
lnutils.SpewLogClosure(commitmentBlob))
10301031

10311032
if err := s.waitForReady(); err != nil {
10321033
return 0, err
@@ -1048,7 +1049,8 @@ func (s *Server) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
10481049
lnwire.CustomRecords, error) {
10491050

10501051
srvrLog.Debugf("ProduceHtlcExtraData called, totalAmount=%d, "+
1051-
"htlcBlob=%v", totalAmount, spew.Sdump(htlcCustomRecords))
1052+
"htlcBlob=%v", totalAmount,
1053+
lnutils.SpewLogClosure(htlcCustomRecords))
10521054

10531055
if err := s.waitForReady(); err != nil {
10541056
return 0, nil, err
@@ -1079,7 +1081,8 @@ func (s *Server) AuxCloseOutputs(
10791081
desc chancloser.AuxCloseDesc) (lfn.Option[chancloser.AuxCloseOutputs],
10801082
error) {
10811083

1082-
srvrLog.Tracef("AuxCloseOutputs called, desc=%v", spew.Sdump(desc))
1084+
srvrLog.Tracef("AuxCloseOutputs called, desc=%v",
1085+
lnutils.SpewLogClosure(desc))
10831086

10841087
if err := s.waitForReady(); err != nil {
10851088
return lfn.None[chancloser.AuxCloseOutputs](), err
@@ -1096,7 +1099,8 @@ func (s *Server) ShutdownBlob(
10961099
req chancloser.AuxShutdownReq) (lfn.Option[lnwire.CustomRecords],
10971100
error) {
10981101

1099-
srvrLog.Tracef("ShutdownBlob called, req=%v", spew.Sdump(req))
1102+
srvrLog.Tracef("ShutdownBlob called, req=%v",
1103+
lnutils.SpewLogClosure(req))
11001104

11011105
if err := s.waitForReady(); err != nil {
11021106
return lfn.None[lnwire.CustomRecords](), err
@@ -1114,7 +1118,7 @@ func (s *Server) FinalizeClose(desc chancloser.AuxCloseDesc,
11141118
closeTx *wire.MsgTx) error {
11151119

11161120
srvrLog.Tracef("FinalizeClose called, desc=%v, closeTx=%v",
1117-
spew.Sdump(desc), spew.Sdump(closeTx))
1121+
lnutils.SpewLogClosure(desc), lnutils.SpewLogClosure(closeTx))
11181122

11191123
if err := s.waitForReady(); err != nil {
11201124
return err
@@ -1128,7 +1132,8 @@ func (s *Server) FinalizeClose(desc chancloser.AuxCloseDesc,
11281132
//
11291133
// NOTE: This method is part of the lnwallet.AuxContractResolver interface.
11301134
func (s *Server) ResolveContract(req lnwl.ResolutionReq) lfn.Result[tlv.Blob] {
1131-
srvrLog.Tracef("ResolveContract called, req=%v", spew.Sdump(req))
1135+
srvrLog.Tracef("ResolveContract called, req=%v",
1136+
lnutils.SpewLogClosure(req))
11321137

11331138
if err := s.waitForReady(); err != nil {
11341139
return lfn.Err[tlv.Blob](err)
@@ -1146,7 +1151,7 @@ func (s *Server) DeriveSweepAddr(inputs []input.Input,
11461151
change lnwl.AddrWithKey) lfn.Result[sweep.SweepOutput] {
11471152

11481153
srvrLog.Tracef("DeriveSweepAddr called, inputs=%v, change=%v",
1149-
spew.Sdump(inputs), spew.Sdump(change))
1154+
lnutils.SpewLogClosure(inputs), lnutils.SpewLogClosure(change))
11501155

11511156
if err := s.waitForReady(); err != nil {
11521157
return lfn.Err[sweep.SweepOutput](err)
@@ -1163,7 +1168,7 @@ func (s *Server) ExtraBudgetForInputs(
11631168
inputs []input.Input) lfn.Result[btcutil.Amount] {
11641169

11651170
srvrLog.Tracef("ExtraBudgetForInputs called, inputs=%v",
1166-
spew.Sdump(inputs))
1171+
lnutils.SpewLogClosure(inputs))
11671172

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

11831188
srvrLog.Tracef("NotifyBroadcast called, req=%v, tx=%v, fee=%v, "+
1184-
"out_index=%v", spew.Sdump(req), spew.Sdump(tx), fee,
1185-
spew.Sdump(outpointToTxIndex))
1189+
"out_index=%v", lnutils.SpewLogClosure(req),
1190+
lnutils.SpewLogClosure(tx), fee,
1191+
lnutils.SpewLogClosure(outpointToTxIndex))
11861192

11871193
if err := s.waitForReady(); err != nil {
11881194
return err

0 commit comments

Comments
 (0)