This repository has been archived by the owner on May 25, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added round robin server manager to connection provider
- Loading branch information
Nick Berardi
committed
Nov 15, 2010
1 parent
4b8a27e
commit b6643b0
Showing
16 changed files
with
142 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/Connection/ConnectionProviderFactory.cs → .../Connections/ConnectionProviderFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
FluentCassandra/Connection/IConnection.cs → FluentCassandra/Connections/IConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ssandra/Connection/IConnectionProvider.cs → ...sandra/Connections/IConnectionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace FluentCassandra.Connections | ||
{ | ||
public interface IServerManager : IEnumerable<Server> | ||
{ | ||
bool HasNext { get; } | ||
Server Next(); | ||
|
||
void Add(Server server); | ||
void Remove(Server server); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace FluentCassandra.Connections | ||
{ | ||
public class RoundRobinServerManager : IServerManager | ||
{ | ||
private readonly object _lock = new object(); | ||
|
||
private List<Server> _servers; | ||
private Queue<Server> _serverQueue; | ||
|
||
public RoundRobinServerManager(ConnectionBuilder builder) | ||
{ | ||
_servers = new List<Server>(builder.Servers); | ||
_serverQueue = new Queue<Server>(_servers); | ||
} | ||
|
||
#region IServerManager Members | ||
|
||
/// <summary> | ||
/// Gets if there are any more connections left to try. | ||
/// </summary> | ||
public bool HasNext | ||
{ | ||
get { return _servers.Count > 0; } | ||
} | ||
|
||
public Server Next() | ||
{ | ||
Server server; | ||
|
||
using (TimedLock.Lock(_lock)) | ||
{ | ||
server = _serverQueue.Dequeue(); | ||
_serverQueue.Enqueue(server); | ||
} | ||
|
||
return server; | ||
} | ||
|
||
public void Add(Server server) | ||
{ | ||
using (TimedLock.Lock(_lock)) | ||
{ | ||
_servers.Add(server); | ||
_serverQueue.Enqueue(server); | ||
} | ||
} | ||
|
||
public void Remove(Server server) | ||
{ | ||
using (TimedLock.Lock(_lock)) | ||
{ | ||
_servers.Remove(server); | ||
_serverQueue = new Queue<Server>(_servers); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region IEnumerable<Server> Members | ||
|
||
public IEnumerator<Server> GetEnumerator() | ||
{ | ||
return _servers.GetEnumerator(); | ||
} | ||
|
||
#endregion | ||
|
||
#region IEnumerable Members | ||
|
||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
FluentCassandra/Connection/Server.cs → FluentCassandra/Connections/Server.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
using System; | ||
|
||
namespace FluentCassandra | ||
namespace FluentCassandra.Connections | ||
{ | ||
public class Server | ||
{ | ||
public const int DefaultPort = 9160; | ||
public const int DefaultTimeout = 0; | ||
|
||
public Server(string host = "127.0.0.1", int port = DefaultPort) | ||
public Server(string host = "127.0.0.1", int port = DefaultPort, int timeout = DefaultTimeout) | ||
{ | ||
Host = host; | ||
Port = port; | ||
Timeout = timeout; | ||
} | ||
|
||
public int Port { get; private set; } | ||
|
||
public string Host { get; private set; } | ||
|
||
public int Timeout { get; private set; } | ||
|
||
public override string ToString() | ||
{ | ||
return String.Concat(Host, ":", Port); | ||
return String.Concat(Host, ":", Port, "-", Timeout); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.