Rx Challenge #3

2015/03/06

no comments

this Rx’s Challenge is actually real-life problem.
I had to solve for one of my customers.
it’s actually a piece of infrastructure which may fit on many scenarios.
the scenario I was encounter is fault tolerant for remote logging 
service. we used to send our logs to central log viewer in order to
inspect the heart bit of our client’s apps and keeping it healthy.

the problem starts when for some reason the client’s apps cannot
sent the information (offline, wrong  URL, etc.), in those cases we
wand to suspend sending for awhile and resume later.

so we react to send’s failures and if it become too often
we suspend the it for awhile.

the same logic can be port for addressing DDoS attack.
for example: when you’re getting too many request on a short time period
from the same client, maybe it is better to suspend it for awhile.

I’m pretty sure that you can think on other scenario where this pattern
can be apply.

Let’s define the challenge,

for the following marble diagram (each ‘-‘ represent a second and the number
represent values):

-1———2———3-4-5-6-7——–8———9——

you should suspend the output stream (for 15 seconds)
when value intensively meet the limit of 3 values within 5 seconds.

for the above input, which represent the values:
# 1: after 2 second
# 2: after 12 seconds
# 3: after 23 seconds
# 4: after 25 seconds
# 5: after 27 seconds (intensively cross the limit )
# 6: after 29 seconds
# 7: after 31 seconds
# 8: after 40 seconds (still within the 15 seconds suspension period)
# 9: after 50 seconds (beyond the suspension period)

so the expected output for
-1———2———3-4-5-6-7——–8———9——
is
-1———2———3-4————————–9——

you can check your solution against the following test (remember to use Rx-Testing NuGet)

[TestMethod]
public void SuspendResume_2_Test()
{
    /**********************************************************************
        * 
        * source -1---------2---------3-4-5-6-7--------8---------9------
        * Result -1---------2---------3-4------------------------9------
        *                     
        **********************************************************************/

    int suspendOnCount = 3;
    TimeSpan suspendCountDuration = TimeSpan.FromSeconds(5);
    TimeSpan suspendDuration = TimeSpan.FromSeconds(15);

    // arrange
    var scheduler = new TestScheduler();
    var observer = scheduler.CreateObserver<int>();

    var source = new Subject<int>();    

    // act
    source // TODO: Your solution goes here
        .Subscribe(observer);

    /**********************************************************************
        * 
        * source 1---------2----------3-4-5-6-7--------8---------9------
        *                     
        **********************************************************************/

    scheduler.AdvanceBy(TimeSpan.FromSeconds(2).Ticks);
    source.OnNext(1);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
    source.OnNext(2);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(11).Ticks);
    source.OnNext(3);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(2).Ticks);
    source.OnNext(4);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(2).Ticks);
    source.OnNext(5);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(2).Ticks);
    source.OnNext(6);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(2).Ticks);
    source.OnNext(7);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(9).Ticks);
    source.OnNext(8);
    scheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
    source.OnNext(9);

    // verify
    var results = observer.
        Messages 
        .Where(m => m.Value.Kind == NotificationKind.OnNext)
        .Select(m => m.Value.Value).ToArray();

    Assert.AreEqual(1, results[0]);
    Assert.AreEqual(2, results[1]);
    Assert.AreEqual(3, results[2]);
    Assert.AreEqual(4, results[3]);
    Assert.AreEqual(9, results[4]);
}

solving this challenge and take different approach,
you’re more than welcome so share your solution on this Rx’s forum thread.

I will post my solution next week.
and I may also peek some of solution for the next post.

Good Luck

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*