Search code examples
system.reactivecode-golf

Detect IsAlive on an IObservable


I'm writing a function IsAlive to take an IObservable<T>, and a timespan, and return an IObservable<bool> The canonical use case is to detect if a streaming server is still sending data.

I've come up with the following solution for it, but feel it's not the most clear as to how it works.

public static IObservable<bool> IsAlive<T>(this IObservable<T> source, 
                                           TimeSpan timeout, 
                                           IScheduler sched)
{
    return source.Window(timeout, sched)
                 .Select(wind => wind.Any())
                 .SelectMany(a => a)
                 .DistinctUntilChanged();
}

Does anyone have a better approach?

FYI - Here are the unit tests and existing approaches that I've tried: https://gist.github.com/997003


Solution

  • This should work:

    public static IObservable<bool> IsAlive<T>(this IObservable<T> source, 
                                               TimeSpan timeout, 
                                               IScheduler sched)
    {
        return source.Buffer(timeout, 1, sched)
                     .Select(l => l.Any())
                     .DistinctUntilChanged();
    }
    

    This approach makes semantic sense, too. Every time an item comes in, it fills the buffer and then true is passed along. And every timeout, an empty buffer will be created and false will be passed along.

    Edit:

    This is why the buffer-1 approach is better than windowing:

    var sched = new TestScheduler();
    var subj = new Subject<Unit>();
    
    var timeout = TimeSpan.FromTicks(10);
    
    subj
        .Buffer(timeout, 1, sched)
        .Select(Enumerable.Any)
        .Subscribe(x => Console.WriteLine("Buffer(timeout, 1): " + x));
    
    subj
        .Window(timeout, sched)
        .Select(wind => wind.Any())
        .SelectMany(a => a)
        .Subscribe(x => Console.WriteLine("Window(timeout): "+x));
    
    sched.AdvanceTo(5);
    subj.OnNext(Unit.Default);
    sched.AdvanceTo(16);
    

    yields:

    Buffer(timeout, 1): True
    Window(timeout): True
    Buffer(timeout, 1): False
    

    To be specific, the window is open for the whole timeout and doesn't close and reset as soon as an item comes in. This is where the buffer limit of 1 comes into play. As soon as an item comes in, the buffer and its timer get restarted.

    I could re-implement my buffer as a window, as buffer's implementation is a window, but a) I think buffer makes better semantic sense and b) I don't have to SelectMany. Scott's Select and SelectMany could be combined into a single SelectMany(x => x.Any()), but I can avoid the entire lambda and specify the Enumerable.Any method group, which will bind faster (trivial) anyway.