question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for semaphore

See original GitHub issue

This may be a bit out there for a “lightweight” library but have you thought of implementing a distributed counting semaphore?

In my scenario, I have a sql azure database with many customers. Customers can have a lot of data. The .net code is hosted in several azure web instances.

I regularly want to process every customer data. However, I don’t want to process all customers at the same time because it would overload the database. I also don’t want to process each customer serially (1 by 1) because it would take too long. Ideally I would use a distributed semaphore with a count of N to process N customers at a time…

It could be built on top of SqlDistributedLock and I wonder if you’ve come across this scenario before? Do you see that fitting within the vision of your library?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
madelsoncommented, Feb 8, 2018

@alastairtree the algorithm I ended up with is essentially this (implemented in SQL):

// the idea of the preamble is to make sure we never hit a case where a lock acquisition with a timeout
// (e. g. TryAcquire) returns false due to timing out on the busy wait lock (below) even though there
// were tickets available
preambleLock = name + '_preamble'
lock (preambleLock) // no blocking inside this; so we can wait for it regardless of timeout
{
     waiterCount = SELECT COUNT(*) FROM tempdb.sys.tables WHERE name like '##' + name + 'marker%';
     var markerTable = '##' + name + 'marker' + GUID;
     CREATE TABLE markerTable;

     if (waiterCount < maxCount) { // space available; just take it
         for (i = 0; i < maxCount; ++i) {
             ticketLock = name + i;
             if (tryAcquire(ticketLock)) { return (ticketLock, markerTable); }
         }
         throw "should have acquired";
     }
}

// the idea of the busy wait lock is that threads queue up here to do a spin wait.
// This both ensures fairness (one spinner at a time) and should prevent too much CPU
// load with many waiters (all but one blocked)
busyWaitLock = name + '_busyWait'
if (tryLock(busyWaitLock, timeout)) {
    while (!timeout expired) {
           for (var i = 0; i < maxCount; ++i) {
                 ticketLock = name + i;
                 if (tryAcquire(ticketLock)) { return (ticketLock, markerTable); }
           }
    }
}

// failed acquire
DROP TABLE markerTable;

To clean up, you just drop the marker temp table along with the ticket lock you took. The full code is in https://github.com/madelson/DistributedLock/blob/master/DistributedLock/Sql/SqlSemaphore.cs#L347

There are some additional complexities there dealing with cancellation and lock reentrance.

Give it a try and let me know how it goes.

1reaction
madelsoncommented, Feb 8, 2018

This is available as of version 1.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get in touch - Semaphore CI
Help us build an industry-defining product. Explore our jobs. team photo. FAQs. Does Semaphore offer flat pricing ...
Read more >
Contact
Let's talk about how we can help grow your business. · Main Office. 9675 SE 36th Street, Suite 110. Mercer Island, WA 98040....
Read more >
Semaphores in Process Synchronization
By using semaphores, processes can coordinate access to shared resources, such as shared memory or I/O devices. Semaphores are of two types:.
Read more >
Contact Technical Support / Progress | Semaphore
To help improve customer experience, we provide the latest status updates to help identify service and maintenance related issues. Smartlogic Service Status ...
Read more >
Semaphore (programming)
In computer science, a semaphore is a variable or abstract data type used to control access to a common resource by multiple threads...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found