Skip to content

Commit

Permalink
fix: add semaphore to fix LockImpl()
Browse files Browse the repository at this point in the history
  • Loading branch information
SvetlanaSemyonova committed Jul 15, 2022
1 parent 1a54fff commit ef367e9
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Assets/Httx/Runtime/Caches/DiskCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Threading;
using System.Threading.Tasks;
using Httx.Caches.Disk;
using Httx.Requests.Awaiters.Async;
Expand Down Expand Up @@ -125,7 +126,8 @@ public async void Delete(Action onComplete) {
public void Dispose() => cacheImpl?.Dispose();

private async void GetImpl(string requestUrl, Action<string> onComplete) {
var fileUrl = await Task.Run(() => {
var fileUrl = await Task.Run(async () => {
await Task.Delay(3);
var key = Crypto.Sha256(requestUrl);
var snapshot = cacheImpl.Get(key);
Expand Down Expand Up @@ -160,14 +162,22 @@ await Task.Run(() => {
}

private async void LockImpl(string requestUrl, Action<Editor> onComplete) {
var result = await Task.Run(() => {
var key = Crypto.Sha256(requestUrl);
var snapshot = cacheImpl.Get(key);
var semaphoreSlim = new SemaphoreSlim(1, 1);
await semaphoreSlim.WaitAsync().ConfigureAwait(false);

return snapshot?.Edit();
});
try {
var result = await Task.Run(async () => {
await Task.Delay(3);
var key = Crypto.Sha256(requestUrl);
var snapshot = cacheImpl.Get(key);
return snapshot?.Edit();
});

onComplete(result);
onComplete(result);

} finally {
semaphoreSlim.Release();
}
}

private async void UnlockImpl(Editor editor, Action onComplete) {
Expand Down

0 comments on commit ef367e9

Please sign in to comment.