-
Notifications
You must be signed in to change notification settings - Fork 16
/
FtpTransitManager.cs
57 lines (51 loc) · 1.42 KB
/
FtpTransitManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WebOne
{
/// <summary>
/// Manager for FTP backends
/// </summary>
internal static class FtpTransitManager
{
/// <summary>
/// Available FTP backends (running FTP clients)
/// </summary>
public static Dictionary<int, FtpClient> Backends = new();
/// <summary>
/// Period of checks for abandoned FTP backends (seconds); default = 300 sec (5 minutes)
/// </summary>
public static int IdleTimeout = 60 * 5;
static FtpTransitManager()
{
new Task(() =>
{
//clean up unused & unuseful backends in background
while (true)
{
Thread.Sleep(1000 * (IdleTimeout + 1) );
foreach (int BackendId in Backends.Keys.ToList())
{
if (!Backends.ContainsKey(BackendId)) continue;
if (!Backends[BackendId].Connected)
{
Backends[BackendId].Log.WriteLine(" Remove closed FTP connection to {0}.", Backends[BackendId].Server);
Backends.Remove(BackendId);
continue;
}
if(DateTime.Now.Subtract(Backends[BackendId].LastUsed).TotalSeconds > IdleTimeout)
{
Backends[BackendId].Log.WriteLine(" Close abandoned FTP connection to {0}.", Backends[BackendId].Server);
Backends[BackendId].Close();
Backends.Remove(BackendId);
continue;
}
}
}
}).Start();
}
}
}