Ejemplo n.º 1
0
    public static IObservable <long> Interval(this IMomentClock clock, IEnumerable <TimeSpan> intervals)
    {
        var e = intervals.GetEnumerator();

        return(Observable.Create <long>(async(observer, ct) => {
            var completed = false;
            try {
                var index = 0L;
                while (e.MoveNext())
                {
                    var dueAt = clock.Now + e.Current;
                    await clock.Delay(dueAt, ct).SuppressCancellation().ConfigureAwait(false);
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }
                    observer.OnNext(index++);
                }
                completed = true;
                observer.OnCompleted();
            }
            catch (Exception e) {
                if (!completed)
                {
                    observer.OnError(e);
                }
            }
            finally {
                e.Dispose();
            }
        }));
    }
Ejemplo n.º 2
0
 public FusionTime(Options?options, IServiceProvider services)
 {
     options ??= new Options();
     DefaultUpdatePeriod  = options.DefaultUpdatePeriod;
     MaxInvalidationDelay = options.MaxInvalidationDelay;
     Clock = options.Clock ?? services.SystemClock();
 }
Ejemplo n.º 3
0
        static Timeouts()
        {
            Clock = CpuClock.Instance;
            var concurrencyLevel = HardwareInfo.GetProcessorCountPo2Factor(8);

            KeepAlive = new ConcurrentTimerSet <object>(
                new ConcurrentTimerSet <object> .Options()
            {
                Quanta           = TimeSpan.FromMilliseconds(250),
                ConcurrencyLevel = concurrencyLevel,
                Clock            = Clock,
            });
            Swap = new ConcurrentTimerSet <ISwappable>(
                new ConcurrentTimerSet <ISwappable> .Options()
            {
                Quanta           = TimeSpan.FromMilliseconds(250),
                ConcurrencyLevel = concurrencyLevel,
                Clock            = Clock,
            },
                t => t.Swap());
            Invalidate = new ConcurrentTimerSet <IComputed>(
                new ConcurrentTimerSet <IComputed> .Options()
            {
                Quanta           = TimeSpan.FromMilliseconds(250),
                ConcurrencyLevel = concurrencyLevel,
                Clock            = Clock,
            },
                t => t.Invalidate());
        }
Ejemplo n.º 4
0
        public Publisher(Options options)
        {
            Id             = options.Id;
            ChannelHub     = options.ChannelHub;
            OwnsChannelHub = options.OwnsChannelHub;

            PublicationFactory           = options.PublicationFactory;
            PublicationGeneric           = options.PublicationGeneric;
            PublicationExpirationTime    = options.PublicationExpirationTime;
            PublicationIdGenerator       = options.PublicationIdGenerator;
            SubscriptionProcessorFactory = options.SubscriptionProcessorFactory;
            SubscriptionProcessorGeneric = options.SubscriptionProcessorGeneric;
            SubscriptionExpirationTime   = options.SubscriptionExpirationTime;
            Clock = options.Clock;

            var concurrencyLevel = HardwareInfo.ProcessorCount << 2;
            var capacity         = 7919;

            Publications      = new ConcurrentDictionary <ComputedInput, IPublication>(concurrencyLevel, capacity);
            PublicationsById  = new ConcurrentDictionary <Symbol, IPublication>(concurrencyLevel, capacity);
            ChannelProcessors = new ConcurrentDictionary <Channel <Message>, PublisherChannelProcessor>(concurrencyLevel, capacity);

            OnChannelAttachedHandler = OnChannelAttached;
            OnChannelDetachedHandler = OnChannelDetachedAsync;
            ChannelHub.Detached     += OnChannelDetachedHandler; // Must go first
            ChannelHub.Attached     += OnChannelAttachedHandler;
        }
Ejemplo n.º 5
0
        public Publisher(Options?options = null)
        {
            options ??= new();
            Id             = options.Id;
            ChannelHub     = options.ChannelHub;
            OwnsChannelHub = options.OwnsChannelHub;

            PublicationFactory           = options.PublicationFactory;
            PublicationGeneric           = options.PublicationGeneric;
            PublicationExpirationTime    = options.PublicationExpirationTime;
            PublicationIdGenerator       = options.PublicationIdGenerator;
            SubscriptionProcessorFactory = options.SubscriptionProcessorFactory;
            SubscriptionProcessorGeneric = options.SubscriptionProcessorGeneric;
            SubscriptionExpirationTime   = options.SubscriptionExpirationTime;
            Clock = options.Clock;

            var concurrencyLevel = HardwareInfo.GetProcessorCountPo2Factor(4);
            var capacity         = OSInfo.IsWebAssembly ? 509 : 7919;

            Publications      = new ConcurrentDictionary <ComputedInput, IPublication>(concurrencyLevel, capacity);
            PublicationsById  = new ConcurrentDictionary <Symbol, IPublication>(concurrencyLevel, capacity);
            ChannelProcessors = new ConcurrentDictionary <Channel <BridgeMessage>, PublisherChannelProcessor>(concurrencyLevel, capacity);

            OnChannelAttachedHandler = OnChannelAttached;
            OnChannelDetachedHandler = OnChannelDetachedAsync;
            ChannelHub.Detached     += OnChannelDetachedHandler; // Must go first
            ChannelHub.Attached     += OnChannelAttachedHandler;
        }
Ejemplo n.º 6
0
 public SubscriptionProcessor(
     IPublication <T> publication,
     Channel <BridgeMessage> outgoingChannel,
     TimeSpan expirationTime,
     IMomentClock clock,
     ILoggerFactory loggerFactory)
     : base(publication, outgoingChannel, expirationTime, clock, loggerFactory)
     => Publication = publication;
Ejemplo n.º 7
0
 public FusionTime(Options?options          = null,
                   IMomentClock?momentClock = null)
 {
     options ??= new Options();
     DefaultUpdatePeriod  = options.DefaultUpdatePeriod;
     MaxInvalidationDelay = options.MaxInvalidationDelay;
     Clock = options.Clock ?? momentClock ?? SystemClock.Instance;
 }
Ejemplo n.º 8
0
 public LiveClock(Options?options          = null,
                  IPluralize?pluralize     = null,
                  IMomentClock?momentClock = null)
 {
     options ??= new Options();
     DefaultUpdatePeriod  = options.DefaultUpdatePeriod;
     MaxInvalidationDelay = options.MaxInvalidationDelay;
     Pluralize            = options.Pluralize ?? pluralize ?? new Pluralizer();
     Clock = options.Clock ?? momentClock ?? SystemClock.Instance;
 }
Ejemplo n.º 9
0
 public UpdateDelayer(Options?options = null)
 {
     options            = options.OrDefault();
     Delay              = options.Delay;
     MinExtraErrorDelay = options.MinExtraErrorDelay;
     MaxExtraErrorDelay = options.MaxExtraErrorDelay;
     CancelDelaysDelay  = options.CancelDelaysDelay;
     Clock              = options.Clock;
     CancelDelays(true);
 }
Ejemplo n.º 10
0
 public static Task Delay(this IMomentClock clock, long dueInMilliseconds, CancellationToken cancellationToken = default)
 {
     if (dueInMilliseconds == Timeout.Infinite)
     {
         return(clock.Delay(Timeout.InfiniteTimeSpan, cancellationToken));
     }
     if (dueInMilliseconds < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(dueInMilliseconds));
     }
     return(clock.Delay(TimeSpan.FromMilliseconds(dueInMilliseconds), cancellationToken));
 }
Ejemplo n.º 11
0
 public SimpleSwapService(Options?options = null)
 {
     options           = options.OrDefault();
     SerializerFactory = options.SerializerFactory;
     ExpirationTime    = options.ExpirationTime;
     Clock             = options.Clock ?? CoarseCpuClock.Instance;
     Storage           = new ConcurrentDictionary <string, string>(
         options.ConcurrencyLevel,
         ComputedRegistry.Options.DefaultInitialCapacity);
     ExpirationTimers = new ConcurrentTimerSet <string>(new ConcurrentTimerSet <string> .Options()
     {
         Clock            = Clock,
         Quanta           = options.TimerQuanta,
         ConcurrencyLevel = options.ConcurrencyLevel,
         FireHandler      = key => Storage.TryRemove(key, out _)
     });
 }
Ejemplo n.º 12
0
 public SimpleSwapService(Options?options, IServiceProvider services)
 {
     options ??= new();
     SerializerFactory = options.SerializerFactory;
     ExpirationTime    = options.ExpirationTime;
     Clock             = options.Clock ?? services.GetRequiredService <MomentClockSet>().CoarseCpuClock;
     Storage           = new ConcurrentDictionary <string, string>(
         options.ConcurrencyLevel,
         ComputedRegistry.Options.DefaultInitialCapacity,
         StringComparer.Ordinal);
     ExpirationTimers = new ConcurrentTimerSet <string>(
         new ConcurrentTimerSet <string> .Options()
     {
         Clock            = Clock,
         Quanta           = options.TimerQuanta,
         ConcurrencyLevel = options.ConcurrencyLevel,
     },
         key => Storage.TryRemove(key, out _));
 }
Ejemplo n.º 13
0
        static Timeouts()
        {
            Clock = CoarseCpuClock.Instance;
            var concurrencyLevel = HardwareInfo.ProcessorCountPo2 << 4;

            KeepAlive = new ConcurrentTimerSet <object>(
                new ConcurrentTimerSet <object> .Options()
            {
                Quanta           = TimeSpan.FromMilliseconds(250),
                ConcurrencyLevel = concurrencyLevel,
                Clock            = Clock,
            });
            Swap = new ConcurrentTimerSet <ISwappable>(
                new ConcurrentTimerSet <ISwappable> .Options()
            {
                Quanta           = TimeSpan.FromMilliseconds(250),
                ConcurrencyLevel = concurrencyLevel,
                Clock            = Clock,
            },
                t => t.SwapAsync());
        }
Ejemplo n.º 14
0
 public static IObservable <long> Timer(this IMomentClock clock, TimeSpan dueIn)
 {
     if (clock is SystemClock)
     {
         return(Observable.Timer(dueIn)); // Perf. optimization
     }
     return(Observable.Create <long>(async observer => {
         var completed = false;
         try {
             await clock.Delay(dueIn).ConfigureAwait(false);
             observer.OnNext(0);
             completed = true;
             observer.OnCompleted();
         }
         catch (Exception e) {
             if (!completed)
             {
                 observer.OnError(e);
             }
         }
     }));
 }
Ejemplo n.º 15
0
        public ComputedRegistry(Options?options = null)
        {
            options ??= new Options();
            _storage = new ConcurrentDictionary <ComputedInput, Entry>(options.ConcurrencyLevel, options.InitialCapacity);
            var locksProvider = options.LocksProvider;

            if (locksProvider == null)
            {
                var locks = new AsyncLockSet <ComputedInput>(ReentryMode.CheckedFail);
                locksProvider = _ => locks;
            }
            _locksProvider = locksProvider;
            _gcHandlePool  = options.GCHandlePool ?? new GCHandlePool(GCHandleType.Weak);
            if (_gcHandlePool.HandleType != GCHandleType.Weak)
            {
                throw new ArgumentOutOfRangeException(
                          $"{nameof(options)}.{nameof(options.GCHandlePool)}.{nameof(_gcHandlePool.HandleType)}");
            }
            _opCounter = new StochasticCounter();
            _clock     = options.Clock;
            UpdatePruneCounterThreshold();
        }
Ejemplo n.º 16
0
 public static IObservable <long> Interval(this IMomentClock clock, long intervalInMilliseconds)
 => clock.Interval(TimeSpan.FromMilliseconds(intervalInMilliseconds));
Ejemplo n.º 17
0
 public IPublication Create(Type genericType,
                            IPublisher publisher, IComputed computed,
                            Symbol publicationId, IMomentClock clock)
 => ConstructorCache
 .GetOrAddChecked(genericType, CreateCache)
 .Invoke(publisher, computed, publicationId, clock);
Ejemplo n.º 18
0
 public ClockBasedVersionGenerator(IMomentClock clock)
 => _clock = clock;
 public InProcessAuthService(IMomentClock clock)
 => Clock = clock;
 public MomentsAgoService(IPluralize pluralize, IMomentClock?clock = null)
 {
     Pluralize = pluralize;
     Clock     = clock ??= SystemClock.Instance;
 }
Ejemplo n.º 21
0
 public SubscriptionProcessor Create(Type genericType,
                                     IPublication publication, Channel <Message> outgoingMessages,
                                     TimeSpan subscribeTimeout, IMomentClock clock, ILoggerFactory loggerFactory)
 => ConstructorCache
 .GetOrAddChecked(genericType, CreateCache)
 .Invoke(publication, outgoingMessages, subscribeTimeout, clock, loggerFactory);
Ejemplo n.º 22
0
 public static IAsyncEnumerable <long> IntervalAsync(this IMomentClock clock, IEnumerable <TimeSpan> intervals)
 => clock.Interval(intervals).ToAsyncEnumerable();
Ejemplo n.º 23
0
 public static IAsyncEnumerable <long> IntervalAsync(this IMomentClock clock, long intervalInMilliseconds)
 => clock.Interval(intervalInMilliseconds).ToAsyncEnumerable();
Ejemplo n.º 24
0
 public static IObservable <long> Timer(this IMomentClock clock, long delayInMilliseconds)
 => clock.Timer(TimeSpan.FromMilliseconds(delayInMilliseconds));
Ejemplo n.º 25
0
 public static Task Delay(this IMomentClock clock, Moment dueAt, CancellationToken cancellationToken = default)
 => clock.Delay((dueAt - clock.Now).NonNegative(), cancellationToken);
Ejemplo n.º 26
0
 public static IObservable <long> Interval(this IMomentClock clock, TimeSpan interval)
 => clock is SystemClock
         ? Observable.Interval(interval) // Perf. optimization
         : clock.Interval(Intervals.Fixed(interval));
Ejemplo n.º 27
0
 public static IAsyncEnumerable <long> TimerAsync(this IMomentClock clock, long delayInMilliseconds)
 => clock.Timer(delayInMilliseconds).ToAsyncEnumerable();
Ejemplo n.º 28
0
 public static IAsyncEnumerable <long> TimerAsync(this IMomentClock clock, TimeSpan dueIn)
 => clock.Timer(dueIn).ToAsyncEnumerable();