Giter VIP home page Giter VIP logo

in-memory-locking's Introduction

In Memory Locking

NuGet

Add this library into your projects from NuGet.

Features

The purpose of this is to provide a light weight, thread-safe in-memory locking solution. If a lock on an id is already held and not timed-out, trying to get a lock on that same id from another thread will throw an exception.

The timeouts are soft timeouts best explained with an example. If the global timeout is set to 5 seconds, and a lock is held for 10 seconds by an operation, then released, it will be successful.

If however in the lock in Request A has timed out (after five seconds) and another Request B tries to lock on that id, that will be given to Request B and the original lock for A will be invalidated. If the locks are used within transactions (like below), then any changes made in Request A will be rolled back. The timeout can be completely removed or set to whatever you need it to be.

Example

It is recomended you initialize the service as a singleton at a global level.

var timeout = TimeSpan.FromSeconds(5);
var lockingService = new InMemoryLockingService(timeout);

Then create a factor and use as below:

var lockingFactory = new InMemoryLockingFactory(lockingService);
using(lockingFactory.ObtainLock(key))
{
	...	
}

The lock gets disposed automatically. You can clear all held locks by calling the lockingService.ClearAll() method.

Transactions

Using this with transactions? The transaction will only complete/commit if a lock is held, the business logic executes cleanly, and the lock is released successfully. All other cases, the transaction will rollback.

using(var scope = new TransactionScope())
{
	using(lockingFactory.ObtainLock(key))
	{
		...
	}
	scope.Complete();
}

Wish to contribute?

Feel free to fork and make pull requests :)

in-memory-locking's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

develohpanda

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.