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(); } })); }
public FusionTime(Options?options, IServiceProvider services) { options ??= new Options(); DefaultUpdatePeriod = options.DefaultUpdatePeriod; MaxInvalidationDelay = options.MaxInvalidationDelay; Clock = options.Clock ?? services.SystemClock(); }
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()); }
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; }
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; }
public SubscriptionProcessor( IPublication <T> publication, Channel <BridgeMessage> outgoingChannel, TimeSpan expirationTime, IMomentClock clock, ILoggerFactory loggerFactory) : base(publication, outgoingChannel, expirationTime, clock, loggerFactory) => Publication = publication;
public FusionTime(Options?options = null, IMomentClock?momentClock = null) { options ??= new Options(); DefaultUpdatePeriod = options.DefaultUpdatePeriod; MaxInvalidationDelay = options.MaxInvalidationDelay; Clock = options.Clock ?? momentClock ?? SystemClock.Instance; }
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; }
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); }
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)); }
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 _) }); }
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 _)); }
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()); }
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); } } })); }
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(); }
public static IObservable <long> Interval(this IMomentClock clock, long intervalInMilliseconds) => clock.Interval(TimeSpan.FromMilliseconds(intervalInMilliseconds));
public IPublication Create(Type genericType, IPublisher publisher, IComputed computed, Symbol publicationId, IMomentClock clock) => ConstructorCache .GetOrAddChecked(genericType, CreateCache) .Invoke(publisher, computed, publicationId, clock);
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; }
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);
public static IAsyncEnumerable <long> IntervalAsync(this IMomentClock clock, IEnumerable <TimeSpan> intervals) => clock.Interval(intervals).ToAsyncEnumerable();
public static IAsyncEnumerable <long> IntervalAsync(this IMomentClock clock, long intervalInMilliseconds) => clock.Interval(intervalInMilliseconds).ToAsyncEnumerable();
public static IObservable <long> Timer(this IMomentClock clock, long delayInMilliseconds) => clock.Timer(TimeSpan.FromMilliseconds(delayInMilliseconds));
public static Task Delay(this IMomentClock clock, Moment dueAt, CancellationToken cancellationToken = default) => clock.Delay((dueAt - clock.Now).NonNegative(), cancellationToken);
public static IObservable <long> Interval(this IMomentClock clock, TimeSpan interval) => clock is SystemClock ? Observable.Interval(interval) // Perf. optimization : clock.Interval(Intervals.Fixed(interval));
public static IAsyncEnumerable <long> TimerAsync(this IMomentClock clock, long delayInMilliseconds) => clock.Timer(delayInMilliseconds).ToAsyncEnumerable();
public static IAsyncEnumerable <long> TimerAsync(this IMomentClock clock, TimeSpan dueIn) => clock.Timer(dueIn).ToAsyncEnumerable();