-
Notifications
You must be signed in to change notification settings - Fork 230
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
#41458, async cache revoke (in ahead) #233
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#region Copyright | ||
// Copyright 2017 Gigya Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Gigya.Microdot.ServiceProxy.Caching | ||
{ | ||
public class ReverseItem | ||
{ | ||
public HashSet<string> CacheKeysSet = new HashSet<string>(); | ||
public DateTime WhenRevoked = DateTime.MinValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why magic number DateTime.MinValue instead of DateTime? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing Copyright |
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using Gigya.Microdot.SharedLogic.Utils; | ||
|
||
namespace Gigya.Microdot.SharedLogic.Collections | ||
{ | ||
/// <summary> | ||
/// A general purpose queue (FIFO) to keep items queued after a cut off time. | ||
/// </summary> | ||
/// <remarks> | ||
/// Items expected to be queued sequentially in time while the next queued greater or equal to previous 'now'. | ||
/// If condition violated, the dequeue will keep items out of expected order. | ||
/// </remarks> | ||
/// <typeparam name="T"></typeparam> | ||
public class TimeBoundConcurrentQueue<T> | ||
{ | ||
public struct Item | ||
{ | ||
public DateTimeOffset Time; | ||
public T Data; | ||
} | ||
private readonly ConcurrentQueue<Item> _queue = new ConcurrentQueue<Item>(); | ||
|
||
public int Count => _queue.Count; | ||
|
||
public void Enqueue(DateTimeOffset now, T data) | ||
{ | ||
_queue.Enqueue(new Item { Time = now, Data = data }); | ||
} | ||
|
||
/// <summary> | ||
/// Dequeues and returns items from the queue as long as their <see cref="Item.Time"/> is older or equal to the provided time. | ||
/// </summary> | ||
/// <param name="olderThanOrEqual">The cut off time to dequeue items older or equal than.</param> | ||
public ICollection<Item> Dequeue(DateTimeOffset olderThanOrEqual) | ||
{ | ||
var oldItems = new List<Item>(); | ||
lock (_queue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this lock? |
||
// Break, if an empty queue or an item is younger | ||
while (_queue.TryPeek(out var item) && item.Time <= olderThanOrEqual) | ||
if (_queue.TryDequeue(out item)) | ||
oldItems.Add(item); | ||
else | ||
GAssert.Fail("Failed to dequeue the item."); | ||
return oldItems; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,10 +81,9 @@ public Task<List<T>> WhenEventsReceived(int? expectedNumberOfEvents, TimeSpan? t | |
|
||
var cancel = new CancellationTokenSource(timeout.Value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not relay related but we should try to reduce the use of multiple timers for better performance |
||
var wait = new TaskCompletionSource<List<T>>(); | ||
cancel.Token.Register( | ||
() => wait.TrySetException( | ||
new Exception( | ||
$"Expected events: {expectedNumberOfEvents}. Received events: {_events.Count}. Timeout after {timeout.Value.TotalMilliseconds} ms"))); | ||
cancel.Token.Register(() => wait.TrySetException( new Exception( $"Expected events: {expectedNumberOfEvents}. " + | ||
$"Received events: {_events.Count}. " + | ||
$"Timeout after {timeout.Value.TotalMilliseconds} ms"))); | ||
wait.Task.ContinueWith(x => cancel.Dispose(), TaskContinuationOptions.OnlyOnRanToCompletion); | ||
|
||
_waiting.Add(new KeyValuePair<int, TaskCompletionSource<List<T>>>(expectedNumberOfEvents.Value, wait)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheKeysSet is use by one thread at a time?