diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-07-14 14:25:18 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-07-14 14:25:18 +0200 |
commit | 8993b3927cf66517e2884b181d6b71d4c6599b7a (patch) | |
tree | a19a98791a7b4e800ad5cf815a7e008d317949f9 /src/device.go | |
parent | 0043008ad06bbeda4b037f2959f2c9cf1b55d65b (diff) |
Improved throughput
- Improved performance by adding the message buffers to a sync.Pool.
- Fixed issue with computing "next" key-pair upon
receiving a response message.
Diffstat (limited to 'src/device.go')
-rw-r--r-- | src/device.go | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/device.go b/src/device.go index b272544..a15961a 100644 --- a/src/device.go +++ b/src/device.go @@ -11,7 +11,11 @@ type Device struct { log *Logger // collection of loggers for levels idCounter uint // for assigning debug ids to peers fwMark uint32 - net struct { + pool struct { + // pools objects for reuse + messageBuffers sync.Pool + } + net struct { // seperate for performance reasons mutex sync.RWMutex addr *net.UDPAddr // UDP source address @@ -57,6 +61,14 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) { } } +func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte { + return device.pool.messageBuffers.Get().(*[MaxMessageSize]byte) +} + +func (device *Device) PutMessageBuffer(msg *[MaxMessageSize]byte) { + device.pool.messageBuffers.Put(msg) +} + func NewDevice(tun TUNDevice, logLevel int) *Device { device := new(Device) @@ -78,6 +90,14 @@ func NewDevice(tun TUNDevice, logLevel int) *Device { device.net.addr, _ = net.ResolveUDPAddr(addr.Network(), addr.String()) device.net.mutex.Unlock() + // setup pools + + device.pool.messageBuffers = sync.Pool{ + New: func() interface{} { + return new([MaxMessageSize]byte) + }, + } + // create queues device.queue.handshake = make(chan QueueHandshakeElement, QueueHandshakeSize) |