This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
SingleServerManager.cs
107 lines (86 loc) · 2.08 KB
/
SingleServerManager.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
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
104
105
106
107
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace FluentCassandra.Connections
{
public class SingleServerManager : IServerManager
{
private readonly object _lock = new object();
private readonly Timer _recoveryTimer;
private readonly long _recoveryTimerInterval;
private Server _server;
private bool _failed;
public SingleServerManager(IConnectionBuilder builder)
{
_server = builder.Servers[0];
_recoveryTimerInterval = (long)builder.ServerPollingInterval.TotalMilliseconds;
_recoveryTimer = new Timer(ServerRecover);
}
private void ServerRecover(object unused)
{
lock(_lock)
{
if(!_failed)
return;
var connection = new Connection(_server, ConnectionType.Simple, 1024);
try
{
connection.Open();
_failed = false;
}
catch { }
finally
{
connection.Close();
}
}
}
#region IServerManager Members
public bool HasNext
{
get { return !_failed; }
}
public Server Next()
{
return _failed ? null : _server;
}
public void ErrorOccurred(Server server, Exception exc = null)
{
Debug.WriteLineIf(exc != null, exc, "connection");
lock(_lock)
{
if(_failed)
return;
_failed = true;
_recoveryTimer.Change(_recoveryTimerInterval, Timeout.Infinite);
}
}
public void Add(Server server)
{
lock(_lock)
{
_server = server;
_failed = false;
_recoveryTimer.Change(Timeout.Infinite,Timeout.Infinite);
}
}
public void Remove(Server server)
{
throw new NotSupportedException("You cannot remove a server since SingleServerManager supports one server. Call the Add method to change the server.");
}
#endregion
#region IEnumerable<Server> Members
public IEnumerator<Server> GetEnumerator()
{
throw new NotImplementedException("SingleServerManager does not implement Enumerable(server)");
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}