Conner Fromknecht
5 years ago
3 changed files with 91 additions and 0 deletions
@ -0,0 +1,19 @@ |
|||||||
|
package buffer |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/lightningnetwork/lnd/lnwire" |
||||||
|
) |
||||||
|
|
||||||
|
// ReadSize represents the size of the maximum message that can be read off the
|
||||||
|
// wire by brontide. The buffer is used to hold the ciphertext while the
|
||||||
|
// brontide state machine decrypts the message.
|
||||||
|
const ReadSize = lnwire.MaxMessagePayload + 16 |
||||||
|
|
||||||
|
// Read is a static byte array sized to the maximum-allowed Lightning message
|
||||||
|
// size, plus 16 bytes for the MAC.
|
||||||
|
type Read [ReadSize]byte |
||||||
|
|
||||||
|
// Recycle zeroes the Read, making it fresh for another use.
|
||||||
|
func (b *Read) Recycle() { |
||||||
|
RecycleSlice(b[:]) |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
package pool |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/buffer" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
// DefaultReadBufferGCInterval is the default interval that a Read will
|
||||||
|
// perform a sweep to see which expired buffer.Reads can be released to
|
||||||
|
// the runtime.
|
||||||
|
DefaultReadBufferGCInterval = 15 * time.Second |
||||||
|
|
||||||
|
// DefaultReadBufferExpiryInterval is the default, minimum interval that
|
||||||
|
// must elapse before a Read will release a buffer.Read. The maximum
|
||||||
|
// time before the buffer can be released is equal to the expiry
|
||||||
|
// interval plus the gc interval.
|
||||||
|
DefaultReadBufferExpiryInterval = 30 * time.Second |
||||||
|
) |
||||||
|
|
||||||
|
// ReadBuffer is a pool of buffer.Read items, that dynamically allocates and
|
||||||
|
// reclaims buffers in response to load.
|
||||||
|
type ReadBuffer struct { |
||||||
|
pool *Recycle |
||||||
|
} |
||||||
|
|
||||||
|
// NewReadBuffer returns a freshly instantiated ReadBuffer, using the given
|
||||||
|
// gcInterval and expieryInterval.
|
||||||
|
func NewReadBuffer(gcInterval, expiryInterval time.Duration) *ReadBuffer { |
||||||
|
return &ReadBuffer{ |
||||||
|
pool: NewRecycle( |
||||||
|
func() interface{} { return new(buffer.Read) }, |
||||||
|
100, gcInterval, expiryInterval, |
||||||
|
), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Take returns a fresh buffer.Read to the caller.
|
||||||
|
func (p *ReadBuffer) Take() *buffer.Read { |
||||||
|
return p.pool.Take().(*buffer.Read) |
||||||
|
} |
||||||
|
|
||||||
|
// Return returns the buffer.Read to the pool, so that it can be cycled or
|
||||||
|
// released.
|
||||||
|
func (p *ReadBuffer) Return(buf *buffer.Read) { |
||||||
|
p.pool.Return(buf) |
||||||
|
} |
Loading…
Reference in new issue