Skip to content

Commit 3097bb8

Browse files
committed
feature: SSH pool cleaner
1 parent ee5890b commit 3097bb8

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

internal/bridge/clean.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
11
package bridge
22

33
import (
4+
"net"
45
"time"
6+
7+
"github.com/limanmys/render-engine/pkg/helpers"
58
)
69

710
func Clean() {
811
now := time.Now()
912
for key, session := range Connections {
1013
if now.Sub(session.LastConnection).Seconds() > 266 {
11-
session.Mutex.Lock()
12-
session.CloseAllConnections()
13-
session.Mutex.Unlock()
14+
closeSession(session, key)
15+
continue
16+
}
17+
18+
if session.SSH == nil {
19+
continue
20+
}
21+
22+
if session.IpAddr != "" && session.Port != "" {
23+
ip, err := helpers.ResolveIP(session.IpAddr)
24+
if err != nil {
25+
closeSession(session, key)
26+
continue
27+
}
1428

15-
Connections.Delete(key)
29+
_, err = net.DialTimeout(
30+
"tcp",
31+
net.JoinHostPort(ip, session.Port),
32+
10*time.Second,
33+
)
34+
if err != nil {
35+
closeSession(session, key)
36+
continue
37+
}
38+
}
39+
40+
ch := make(chan int, 1)
41+
go func() {
42+
select {
43+
case <-time.After(10 * time.Second):
44+
case <-ch:
45+
return
46+
default:
47+
session.SSH.SendRequest("[email protected]", true, nil)
48+
ch <- 1
49+
}
50+
}()
51+
52+
select {
53+
case <-ch:
54+
continue
55+
case <-time.After(10 * time.Second):
56+
closeSession(session, key)
1657
continue
1758
}
1859
}
19-
time.Sleep(time.Second)
60+
time.Sleep(5 * time.Second)
2061
go Clean()
2162
}
63+
64+
func closeSession(s *Session, key string) {
65+
s.Mutex.Lock()
66+
s.CloseAllConnections()
67+
s.Mutex.Unlock()
68+
69+
Connections.Delete(key)
70+
}

0 commit comments

Comments
 (0)