-
-
Save dcomartin/4990149ff7539b755888c17483d230ad to your computer and use it in GitHub Desktop.
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 UsernameReservationSync | |
{ | |
private TimeSpan Timeout => TimeSpan.FromSeconds(5); | |
private readonly FakeDatabase _db; | |
public UsernameReservationSync(FakeDatabase db) | |
{ | |
_db = db; | |
} | |
public bool Reserve(string username) | |
{ | |
if (_db.RegisteredUsernames.Any(x => x == username)) | |
{ | |
return false; | |
} | |
if (_db.ReservedUsernames.Any(x => x == username)) | |
{ | |
return false; | |
} | |
_db.ReservedUsernames.Add(username); | |
Task.Run(async () => | |
{ | |
await Task.Delay(Timeout); | |
Expire(username); | |
}); | |
return true; | |
} | |
private void Expire(string username) | |
{ | |
_db.ReservedUsernames.Remove(username); | |
} | |
public bool Complete(string username) | |
{ | |
if (_db.ReservedUsernames.Any(x => x == username) == false) | |
{ | |
return false; | |
} | |
if (_db.RegisteredUsernames.Any(x => x == username)) | |
{ | |
return false; | |
} | |
_db.ReservedUsernames.Remove(username); | |
_db.RegisteredUsernames.Add(username); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment