Rx Challenge #11–Solution

2016/09/15

Rx Challenge #11–Solution You can find the solution for Challenge 11 below. Previous challenges available here. The following code snippet implement the base functionality which is similar to the functionality of SemaphoreSlim. Code Snippet public class AsyncLock {     private int _counter = 0;     private readonly int _limit;     // semantic Task     private ConcurrentQueue<TaskCompletionSource<object>> _tcsQueue =         new ConcurrentQueue<TaskCompletionSource<object>>();       #region Ctor       /// <summary>     /// Initializes a new instance of the <see cref="AsyncLock"/> class.     /// </summary>     /// <param name="limit">The limit.</param>     public AsyncLock(int limit)     {         _limit = limit;     }       #endregion // Ctor       #region WaitAsync  ...
no comments

Rx Challenge #11

2016/09/08

Rx Challenge #11 This challenge is practice of building something already exists, by your own. The challenge is to build async lock similar to the functionality of SemaphoreSlim WaitAsync / Release. You can check this post for the functionality of SemaphoreSlim. You can consider the following building block for this challenge: - Concurrent Collection - Interlocked - TaskCompletionSource You can validate your solution against this Test Code Snippet public async Task AsyncLock_Test() {     // arrange     const int LIMIT = 3;     var gate = new TaskCompletionSource<object>();     var barrier = new CountdownEvent(LIMIT);     var asyncLock =...
no comments

Rx Challenge #10 (Solution): DistinctUntilChanged with expiration period

2016/09/02

Rx Challenge #10 (Solution): DistinctUntilChanged with expiration period The solution for the DistinctUntilChanged challenge is: Code Snippet public static IObservable<TSource> DistinctUntilChanged<TSource, TKey, TPeriod>(     this IObservable<TSource> source,     Func<TSource, TKey> keySelector,     IEqualityComparer<TKey> comparer,     IObservable<TPeriod> distinctPeriod) {     var result = source.Publish(hot =>     {         var distinct = hot.TakeUntil(distinctPeriod)                           .DistinctUntilChanged(keySelector, comparer)                           .Repeat();         return distinct;     });     return result; }   The idea is to cancel the district stream after a period and resume the subscription (repeat).   You can use the following convenient overloads: Code Snippet public static IObservable<TSource> DistinctUntilChanged<TSource>(     this IObservable<TSource>...

Rx Challenge #9: Execute Async Until [Solution]

2016/08/30

Rx Challenge #9: Execute Async Until Challenge #9 was about implementing a pattern where you have to keep sending async message until you will get notification which approve that the message is handled by the other side. see the full detail at the Challenge at this post. Simple scenario For the simple scenario of you calling service and retry sending, according to it’s response. You can use the following pattern. Code Snippet private static async Task VerySimpleScenario() {     var delay = Observable.Timer(TimeSpan.FromSeconds(1));     var noDelay = Observable.Return(1L);     var xs = Observable.FromAsync(() => ExecSimpleAsync(1))...

Rx Challenge #10: DistinctUntilChanged with expiration period

2016/08/19

Rx Challenge #10: DistinctUntilChanged with expiration period This challenge is DistinctUntilChanged  cancellation after a period. When having event stream which may produce items in a row and then be idle for a period. You may want to get item after a period even those it identical the the previous one.   On challenge you need to construct it using composition of existing Rx’s operators. It can be formulate into the following pattern: Code Snippet public static IObservable<T> DistinctUntilChanged<T>(     this IObservable<T> source,     TimeSpan distinctPeriod) {     // TODO: your code }   ...
no comments

Rx Challenge #9: Execute Async Until

2016/08/16

Rx Challenge #9: Execute Async Until This time the challenge is for implementing a pattern where you have to keep sending async message until you will get notification which approve that the message is handled by the other side. The idea is to verify that the message was handled by other side without keeping open connection and waiting for response (which already implement by the TCP protocol). This pattern can be useful on many scenarios, For example, any queue mechanism like Service Bus, Rabbit MQ, etc. Using this pattern you can keep sending the message using predefine interval...

Semaphore Slim and Async

Semaphore Slim and Async On this post I will focus on async waiting on Semaphore (using SemaphoreSlim). In general .NET is having 2 Semaphore implementations: - Semaphore which is the legacy implementation (but support cross process operations) - SemaphoreSlim which is lightweight version. It bring some performance improvements but it don’t support cross process operation. this post will focus on SemaphoreSlim WaitAsync API. Let start analyzing the following code snippet, think what’s wrong with it: Code Snippet private static SemaphoreSlim _gate = new SemaphoreSlim(4); static void Main(string args) {     for (int i = 0;...
no comments

Visual Rx is having new Home and future plans

2016/08/02

.Visual Rx is having new Home and future plans Visual Rx is having new Home under GitHub. It had moved from CodePlex to GitHub and will be maintain on GitHub. The plans for Visual Rx future is to start new version from scratch. This version will have the following characteristic: - Support .NET Core. - Better organized. - API improvements. - Support more communication client out of the box. - Have Angular 2 viewer. After setting the foundation for this version I will post call for the community to join the effort. The final target of Visual Rx (if...