Skip to content

Commit

Permalink
Merge pull request #5 from naile/master
Browse files Browse the repository at this point in the history
Ensure concurrent instances don't run on the same port
  • Loading branch information
poulfoged authored Jun 13, 2017
2 parents 1a62f7a + 2bbd9de commit 1cc8e28
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion source/RedisInside/Redis.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -93,13 +94,19 @@ public void Dispose()

private class Config : IConfig
{
private static readonly ConcurrentDictionary<int, byte> usedPorts = new ConcurrentDictionary<int, byte>();
private static readonly Random random = new Random();
internal Action<string> logger;
internal int port;

public Config()
{
port = random.Next(49152, 65535 + 1);
do
{
port = random.Next(49152, 65535 + 1);
} while (usedPorts.ContainsKey(port));

usedPorts.AddOrUpdate(port, i => byte.MinValue, (i, b) => byte.MinValue);
logger = message => Trace.WriteLine(message);
}

Expand Down

0 comments on commit 1cc8e28

Please sign in to comment.