Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/samcook/RedLock.net
Browse files Browse the repository at this point in the history
  • Loading branch information
samcook committed Sep 1, 2014
2 parents 141cfa8 + 16ca6d0 commit 959c101
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ Makes use of the excellent [StackExchange.Redis](https://github.com/StackExchang

Distributed locks are useful for ensuring only one process is using a particular resource at any given time (even if the processes are running on different machines).

RedLock.net is available using NuGet - search for [RedLock.net](https://www.nuget.org/packages/RedLock.net) (you'll need to enable pre-release packages at this point).

## Usage

Construct a `RedisLockFactory` (passing in a set of redis endpoints). You should keep hold of this and reuse it in your application. Each instance of `RedisLockFactory` maintains its own set of connections with Redis. Remember to dispose it when your app shuts down.
Construct a `RedisLockFactory`, passing in a set of independent Redis endpoints. The Redis endpoints should be independent (i.e. not replicated masters/slaves).

You should keep hold of the `RedisLockFactory` and reuse it in your application. Each instance maintains its own connections with the configured Redis instances. Remember to dispose it when your app shuts down.

With the factory, create a `RedisLock` in a using block, making sure to check that that the lock `IsAcquired` inside the block before performing any actions. A lock is acquired if `RedisLock` can set a lock key in more than half of the redis instances.

Internally `RedisLock` has a timer that automatically tries to keep the redis lock key alive. In the worst case of a process crashing without releasing the lock, it will automatically be expired by redis after the specified expiry time.

#### On app init:
```csharp
var endPoints = new[]
{
new DnsEndPoint("redis1", 6379),
new DnsEndPoint("redis2", 6379),
new DnsEndPoint("redis3", 6379)
};
var redisLockFactory = new RedisLockFactory(endPoints);
```

Expand All @@ -42,6 +52,7 @@ var expiry = TimeSpan.FromSeconds(30);
var wait = TimeSpan.FromSeconds(10);
var retry = TimeSpan.FromSeconds(1);

// blocks until acquired or 'wait' timeout
using (var redisLock = redisLockFactory.Create(resource, expiry, wait, retry))
{
// make sure we got the lock
Expand Down
5 changes: 3 additions & 2 deletions RedLock/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RedLock.net")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("An implementation of the Redlock distributed lock algorithm")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RedLock.net")]
Expand All @@ -32,7 +32,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0-alpha")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("RedLock.Tests")]
1 change: 1 addition & 0 deletions RedLock/RedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="RedLock.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
18 changes: 18 additions & 0 deletions RedLock/RedLock.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>RedLock.net</id>
<version>$version$</version>
<title>RedLock.net</title>
<authors>Sam Cook</authors>
<owners>Sam Cook</owners>
<licenseUrl>https://github.com/samcook/RedLock.net/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/samcook/RedLock.net</projectUrl>
<!-- <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl> -->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>An implementation of the Redlock distributed lock algorithm</description>
<releaseNotes>1.0.0 - Initial release</releaseNotes>
<copyright>Copyright 2014 Sam Cook</copyright>
<tags>RedLock Redis Distributed Lock</tags>
</metadata>
</package>

0 comments on commit 959c101

Please sign in to comment.