From 672ce1f0a88e52780b8f2ff174bf3d542c72f7c7 Mon Sep 17 00:00:00 2001 From: mhp Date: Thu, 19 Nov 2020 15:44:05 -0800 Subject: [PATCH] Move slice allocations in connection manager monitor loop (#340) * Move slice allocations in connection manager monitor loop * move further out Co-authored-by: Miran Park --- connection_manager.go | 9 ++++++--- connection_manager_test.go | 22 ++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/connection_manager.go b/connection_manager.go index bcd13b6..bb38e3d 100644 --- a/connection_manager.go +++ b/connection_manager.go @@ -141,14 +141,17 @@ func (n *connectionManager) Start() { func (n *connectionManager) Run() { clockSource := time.Tick(500 * time.Millisecond) + p := []byte("") + nb := make([]byte, 12, 12) + out := make([]byte, mtu) for now := range clockSource { - n.HandleMonitorTick(now) + n.HandleMonitorTick(now, p, nb, out) n.HandleDeletionTick(now) } } -func (n *connectionManager) HandleMonitorTick(now time.Time) { +func (n *connectionManager) HandleMonitorTick(now time.Time, p, nb, out []byte) { n.TrafficTimer.advance(now) for { ep := n.TrafficTimer.Purge() @@ -188,7 +191,7 @@ func (n *connectionManager) HandleMonitorTick(now time.Time) { if hostinfo != nil && hostinfo.ConnectionState != nil { // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues - n.intf.SendMessageToVpnIp(test, testRequest, vpnIP, []byte(""), make([]byte, 12, 12), make([]byte, mtu)) + n.intf.SendMessageToVpnIp(test, testRequest, vpnIP, p, nb, out) } else { hostinfo.logger().Debugf("Hostinfo sadness: %s", IntIp(vpnIP)) diff --git a/connection_manager_test.go b/connection_manager_test.go index b1b79de..0dd3f7a 100644 --- a/connection_manager_test.go +++ b/connection_manager_test.go @@ -42,7 +42,10 @@ func Test_NewConnectionManagerTest(t *testing.T) { // Create manager nc := newConnectionManager(ifce, 5, 10) - nc.HandleMonitorTick(now) + p := []byte("") + nb := make([]byte, 12, 12) + out := make([]byte, mtu) + nc.HandleMonitorTick(now, p, nb, out) // Add an ip we have established a connection w/ to hostmap hostinfo := nc.hostMap.AddVpnIP(vpnIP) hostinfo.ConnectionState = &ConnectionState{ @@ -57,18 +60,18 @@ func Test_NewConnectionManagerTest(t *testing.T) { assert.Contains(t, nc.hostMap.Hosts, vpnIP) // Move ahead 5s. Nothing should happen next_tick := now.Add(5 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // Move ahead 6s. We haven't heard back next_tick = now.Add(6 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // This host should now be up for deletion assert.Contains(t, nc.pendingDeletion, vpnIP) assert.Contains(t, nc.hostMap.Hosts, vpnIP) // Move ahead some more next_tick = now.Add(45 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // The host should be evicted assert.NotContains(t, nc.pendingDeletion, vpnIP) @@ -105,7 +108,10 @@ func Test_NewConnectionManagerTest2(t *testing.T) { // Create manager nc := newConnectionManager(ifce, 5, 10) - nc.HandleMonitorTick(now) + p := []byte("") + nb := make([]byte, 12, 12) + out := make([]byte, mtu) + nc.HandleMonitorTick(now, p, nb, out) // Add an ip we have established a connection w/ to hostmap hostinfo := nc.hostMap.AddVpnIP(vpnIP) hostinfo.ConnectionState = &ConnectionState{ @@ -120,11 +126,11 @@ func Test_NewConnectionManagerTest2(t *testing.T) { assert.Contains(t, nc.hostMap.Hosts, vpnIP) // Move ahead 5s. Nothing should happen next_tick := now.Add(5 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // Move ahead 6s. We haven't heard back next_tick = now.Add(6 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // This host should now be up for deletion assert.Contains(t, nc.pendingDeletion, vpnIP) @@ -133,7 +139,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) { nc.In(vpnIP) // Move ahead some more next_tick = now.Add(45 * time.Second) - nc.HandleMonitorTick(next_tick) + nc.HandleMonitorTick(next_tick, p, nb, out) nc.HandleDeletionTick(next_tick) // The host should be evicted assert.NotContains(t, nc.pendingDeletion, vpnIP)