Skip to content

Commit ffdd9c0

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

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
@@ -290,3 +290,10 @@ func (c *Conn) RemotePub() *btcec.PublicKey {
290290
func (c *Conn) LocalPub() *btcec.PublicKey {
291291
return c.noise.localStatic.PubKey()
292292
}
293+
294+
// ClearPendingSend drops references to the next header and body buffers so
295+
// that the memory can be garbage collected.
296+
func (c *Conn) ClearPendingSend() {
297+
c.noise.nextHeaderSend = nil
298+
c.noise.nextBodySend = nil
299+
}

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)