Skip to content

Commit d495c8f

Browse files
committed
brontide+peer: incrementally release buffer after each write
This lets the GC free up the buffers sooner.
1 parent db83bb1 commit d495c8f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

brontide/conn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,10 @@ func (c *Conn) RemotePub() *btcec.PublicKey {
287287
func (c *Conn) LocalPub() *btcec.PublicKey {
288288
return c.noise.localStatic.PubKey()
289289
}
290+
291+
// ClearPendingSend drops references to the next header and body buffers so
292+
// that the memory can be garbage collected.
293+
func (c *Conn) ClearPendingSend() {
294+
c.noise.nextHeaderSend = nil
295+
c.noise.nextBodySend = nil
296+
}

peer/brontide.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/btcsuite/btcd/txscript"
2020
"github.com/btcsuite/btcd/wire"
2121
"github.com/btcsuite/btclog/v2"
22+
"github.com/lightningnetwork/lnd/brontide"
2223
"github.com/lightningnetwork/lnd/buffer"
2324
"github.com/lightningnetwork/lnd/chainntnfs"
2425
"github.com/lightningnetwork/lnd/channeldb"
@@ -2705,6 +2706,13 @@ out:
27052706
goto retry
27062707
}
27072708

2709+
// Message has either been successfully sent or an
2710+
// unrecoverable error occurred. Either way, we can
2711+
// free the memory used to store the message.
2712+
if bConn, ok := p.cfg.Conn.(*brontide.Conn); ok {
2713+
bConn.ClearPendingSend()
2714+
}
2715+
27082716
// The write succeeded, reset the idle timer to prevent
27092717
// us from disconnecting the peer.
27102718
if !idleTimer.Stop() {

0 commit comments

Comments
 (0)