summaryrefslogtreecommitdiffhomepage
path: root/device.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-09-22 06:29:02 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-09-24 00:37:43 +0200
commit833597b585f460aaa17bad93ad59290ec282e77e (patch)
tree209278c3c686fefffc56067b03a4f66017e009ae /device.go
parentcf81a28dd30bd8714432d2ff108d64c7f4b65e50 (diff)
More pooling
Diffstat (limited to 'device.go')
-rw-r--r--device.go39
1 files changed, 7 insertions, 32 deletions
diff --git a/device.go b/device.go
index bbcd0fc..7cf9ba2 100644
--- a/device.go
+++ b/device.go
@@ -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