This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
scanner.go
103 lines (87 loc) · 2.6 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
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/golang/protobuf/proto"
"github.com/openziti/fabric/ctrl_msg"
"github.com/openziti/fabric/pb/ctrl_pb"
"github.com/openziti/foundation/channel2"
"github.com/sirupsen/logrus"
"time"
)
type Scanner struct {
ctrl channel2.Channel
sessions *sessionTable
interval time.Duration
timeout time.Duration
closeNotify <-chan struct{}
}
func NewScanner(options *Options, closeNotify <-chan struct{}) *Scanner {
s := &Scanner{
interval: options.IdleTxInterval,
timeout: options.IdleSessionTimeout,
closeNotify: closeNotify,
}
if s.interval > 0 {
go s.run()
} else {
logrus.Warnf("scanner disabled")
}
return s
}
func (self *Scanner) SetCtrl(ch channel2.Channel) {
self.ctrl = ch
}
func (self *Scanner) setSessionTable(sessions *sessionTable) {
self.sessions = sessions
}
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() {
sessions := self.sessions.sessions.Items()
logrus.Debugf("scanning [%d] sessions", len(sessions))
var idleSessionIds []string
for sessionId, ft := range sessions {
if time.Since(ft.(*forwardTable).last) > self.timeout {
idleSessionIds = append(idleSessionIds, sessionId)
logrus.Warnf("[s/%s] idle after [%s]", sessionId, self.timeout)
}
}
if len(idleSessionIds) > 0 {
logrus.Debugf("found [%d] idle sessions, confirming with controller", len(idleSessionIds))
if self.ctrl != nil {
confirm := &ctrl_pb.SessionConfirmation{SessionIds: idleSessionIds}
body, err := proto.Marshal(confirm)
if err == nil {
msg := channel2.NewMessage(ctrl_msg.SessionConfirmationType, body)
if err := self.ctrl.Send(msg); err == nil {
logrus.Warnf("sent confirmation for [%d] sessions", len(idleSessionIds))
} else {
logrus.Errorf("error sending confirmation request (%v)", err)
}
}
} else {
logrus.Errorf("no ctrl channel, cannot request session confirmations")
}
}
}