forked from mysql-net/MySqlConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement server load balancing. Fixes mysql-net#226
- Loading branch information
Showing
12 changed files
with
258 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace MySql.Data.MySqlClient | ||
{ | ||
public enum MySqlLoadBalance | ||
{ | ||
/// <summary> | ||
/// Servers are tried sequentially, across multiple calls to <see cref="MySqlConnection.Open"/>. | ||
/// </summary> | ||
RoundRobin, | ||
|
||
/// <summary> | ||
/// Servers are tried in order, starting with the first one, for each call to <see cref="MySqlConnection.Open"/>. | ||
/// </summary> | ||
InOrder, | ||
|
||
/// <summary> | ||
/// Servers are tried in random order. | ||
/// </summary> | ||
Random, | ||
|
||
/// <summary> | ||
/// Servers are tried in ascending order of number of currently-open connections. | ||
/// </summary> | ||
FewestConnections, | ||
} | ||
} |
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,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MySql.Data.Serialization | ||
{ | ||
internal interface ILoadBalancer | ||
{ | ||
/// <summary> | ||
/// Returns an <see cref="IEnumerable{string}"/> containing <paramref name="hosts"/> in the order they | ||
/// should be tried to satisfy the load balancing policy. | ||
/// </summary> | ||
IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts); | ||
} | ||
|
||
internal sealed class InOrderLoadBalancer : ILoadBalancer | ||
{ | ||
public static ILoadBalancer Instance { get; } = new InOrderLoadBalancer(); | ||
|
||
public IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts) => hosts; | ||
|
||
private InOrderLoadBalancer() | ||
{ | ||
} | ||
} | ||
|
||
internal sealed class RandomLoadBalancer : ILoadBalancer | ||
{ | ||
public static ILoadBalancer Instance { get; } = new RandomLoadBalancer(); | ||
|
||
public IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts) | ||
{ | ||
// from https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm | ||
var shuffled = new List<string>(hosts); | ||
for (var i = hosts.Count - 1; i >= 1; i--) | ||
{ | ||
int j; | ||
lock (m_random) | ||
j = m_random.Next(i + 1); | ||
if (i != j) | ||
{ | ||
var temp = shuffled[i]; | ||
shuffled[i] = shuffled[j]; | ||
shuffled[j] = temp; | ||
} | ||
} | ||
return shuffled; | ||
} | ||
|
||
private RandomLoadBalancer() => m_random = new Random(); | ||
|
||
readonly Random m_random; | ||
} | ||
|
||
internal sealed class RoundRobinLoadBalancer : ILoadBalancer | ||
{ | ||
public RoundRobinLoadBalancer() => m_lock = new object(); | ||
|
||
public IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts) | ||
{ | ||
int start; | ||
lock (m_lock) | ||
start = (int) (m_counter++ % hosts.Count); | ||
|
||
var shuffled = new List<string>(hosts.Count); | ||
for (var i = start; i < hosts.Count; i++) | ||
shuffled.Add(hosts[i]); | ||
for (var i = 0; i < start; i++) | ||
shuffled.Add(hosts[i]); | ||
return shuffled; | ||
} | ||
|
||
readonly object m_lock; | ||
uint m_counter; | ||
} | ||
} |
Oops, something went wrong.