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
lock(someSharedResource) | |
{ | |
// Do some work using the resource | |
// and maybe update it. | |
} |
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
public class LockModel | |
{ | |
// We only need an ID right now | |
public string Id { get; set; } | |
} |
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
public class LockProvider | |
{ | |
private readonly IMongoCollection<LockModel> collection; | |
public LockProvider(string mongodbConnString) {} // Collapsed | |
public async Task<IDisposable> AcquireLock(string resourceId) | |
{ | |
// Determine the id of the lock | |
var lockId = $"lock_{resourceId}"; |
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
public class DistributedLock : IDisposable | |
{ | |
private readonly IMongoCollection<LockModel> collection; | |
private readonly string lockId; | |
public DistributedLock(IMongoCollection<LockModel> collection, string lockId) | |
{ | |
this.collection = collection; | |
this.lockId = lockId; | |
} |
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
try | |
{ | |
var response = await collection.FindOneAndUpdateAsync<LockModel>( | |
// Collapsed | |
); | |
// If the result of the FindOneAndUpdateAsync is null, then that means there was no record | |
// before we ran our statement, so we now have the lock. | |
// If the result is not null, then it means a document for the lock already existed, so someone else has the lock. | |
if (response == null) |
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
using (await LockProvider.AcquireLock(id)) | |
{ | |
// Do some work | |
} |
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
public class LockModel | |
{ | |
public string Id { get; set; } | |
/// <summary> | |
/// I'm going to set this to the moment in time when the lock should be cleared. | |
/// </summary> | |
public DateTime ExpireAt { get; set; } | |
} |
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
var response = await collection.FindOneAndUpdateAsync<LockModel>( | |
// Find a record with the lock ID | |
x => x.Id == lockId, | |
// If our 'upsert' creates a document, set the ID to the lock ID | |
Builders<LockModel>.Update | |
.SetOnInsert(x => x.Id, lockId) | |
.SetOnInsert(x => x.ExpireAt, DateTime.UtcNow.AddMinutes(1)), | |
new FindOneAndUpdateOptions<LockModel> | |
{ | |
// If the record doesn't exist, create it. |
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
void setup() | |
{ | |
// Do your setup here, | |
// configure your board, and so on. | |
} | |
void loop() | |
{ | |
// Called continuously. This is where you | |
// will do the bulk of your work. |
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
void setup() | |
{ | |
// Configure the LED pin | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
void loop() | |
{ | |
// Set the PIN to 'high' | |
digitalWrite(LED_BUILTIN, HIGH); |
OlderNewer