From 9efd5ede011232d75452db320faef1dc62f2add1 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 4 Mar 2021 14:32:22 -0500 Subject: [PATCH] forwarder.scanner. (#173) --- router/forwarder/forwarder.go | 3 ++ router/forwarder/scanner.go | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 router/forwarder/scanner.go diff --git a/router/forwarder/forwarder.go b/router/forwarder/forwarder.go index c9e65554d..dd68ae463 100644 --- a/router/forwarder/forwarder.go +++ b/router/forwarder/forwarder.go @@ -33,6 +33,7 @@ type Forwarder struct { sessions *sessionTable destinations *destinationTable faulter *Faulter + scanner *scanner metricsRegistry metrics.UsageRegistry traceController trace.Controller Options *Options @@ -63,6 +64,8 @@ func NewForwarder(metricsRegistry metrics.UsageRegistry, faulter *Faulter, optio Options: options, CloseNotify: closeNotify, } + forwarder.scanner = newScanner(forwarder.sessions, 30 * time.Second, 30 * time.Second, forwarder.CloseNotify) + go forwarder.scanner.run() return forwarder } diff --git a/router/forwarder/scanner.go b/router/forwarder/scanner.go new file mode 100644 index 000000000..94d28f290 --- /dev/null +++ b/router/forwarder/scanner.go @@ -0,0 +1,63 @@ +/* + Copyright NetFoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package forwarder + +import ( + "github.com/sirupsen/logrus" + "time" +) + +type scanner struct { + sessions *sessionTable + interval time.Duration + timeout time.Duration + closeNotify <-chan struct{} +} + +func newScanner(sessions *sessionTable, interval time.Duration, timeout time.Duration, closeNotify <-chan struct{}) *scanner { + return &scanner{ + sessions: sessions, + interval: interval, + timeout: timeout, + closeNotify: closeNotify, + } +} + +func (self *scanner) run() { + logrus.Info("started") + defer logrus.Warn("exited") + + for { + select { + case <-time.After(self.interval): + self.scan() + + case <-self.closeNotify: + return + } + } +} + +func (self *scanner) scan() { + for sessionId, ft := range self.sessions.sessions.Items() { + var idleSessionIds []string + if time.Since(ft.(*forwardTable).last) > self.timeout { + idleSessionIds = append(idleSessionIds, sessionId) + logrus.Warnf("[s/%s] idle after [%s]", sessionId, self.timeout) + } + } +}