More pooling

This commit is contained in:
Jason A. Donenfeld
2018-09-22 06:29:02 +02:00
parent cf81a28dd3
commit 833597b585
4 changed files with 148 additions and 56 deletions

View File

@@ -19,8 +19,6 @@ const (
DeviceRoutineNumberAdditional = 2
)
var preallocatedBuffers = 0
type Device struct {
isUp AtomicBool // device is (going) up
isClosed AtomicBool // device is closed? (acting as guard)
@@ -68,8 +66,12 @@ type Device struct {
}
pool struct {
messageBuffers *sync.Pool
reuseChan chan interface{}
messageBufferPool *sync.Pool
messageBufferReuseChan chan *[MaxMessageSize]byte
inboundElementPool *sync.Pool
inboundElementReuseChan chan *QueueInboundElement
outboundElementPool *sync.Pool
outboundElementReuseChan chan *QueueOutboundElement
}
queue struct {
@@ -245,22 +247,6 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
return nil
}
func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte {
if preallocatedBuffers == 0 {
return device.pool.messageBuffers.Get().(*[MaxMessageSize]byte)
} else {
return (<-device.pool.reuseChan).(*[MaxMessageSize]byte)
}
}
func (device *Device) PutMessageBuffer(msg *[MaxMessageSize]byte) {
if preallocatedBuffers == 0 {
device.pool.messageBuffers.Put(msg)
} else {
device.pool.reuseChan <- msg
}
}
func NewDevice(tunDevice tun.TUNDevice, logger *Logger) *Device {
device := new(Device)
@@ -285,18 +271,7 @@ func NewDevice(tunDevice tun.TUNDevice, logger *Logger) *Device {
device.indexTable.Init()
device.allowedips.Reset()
if preallocatedBuffers == 0 {
device.pool.messageBuffers = &sync.Pool{
New: func() interface{} {
return new([MaxMessageSize]byte)
},
}
} else {
device.pool.reuseChan = make(chan interface{}, preallocatedBuffers)
for i := 0; i < preallocatedBuffers; i += 1 {
device.pool.reuseChan <- new([MaxMessageSize]byte)
}
}
device.PopulatePools()
// create queues