Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add semaphore to fix LockImpl() #5

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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