forwarder.scanner. (#173)

This commit is contained in:
Michael Quigley
2021-03-04 14:32:22 -05:00
parent 5e40efb73d
commit 9efd5ede01
2 changed files with 66 additions and 0 deletions
+3
View File
@@ -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
}
+63
View File
@@ -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)
}
}
}