Example #1
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());
        }
 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 _)
     });
 }
Example #3
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 _));
 }
        public async Task BasicTest()
        {
            var clock = SystemClock.Instance;

            await using var timerSet = new ConcurrentTimerSet <Timer>(new ConcurrentTimerSet <Timer> .Options()
            {
                Quanta      = TimeSpan.FromMilliseconds(10),
                Clock       = clock,
                FireHandler = t => t.FiredAt = CoarseCpuClock.Now,
            });

            // AddOrUpdateToLater
            var t = new Timer();

            timerSet.AddOrUpdate(t, clock.Now + TimeSpan.FromMilliseconds(100));
            timerSet.AddOrUpdateToEarlier(t, clock.Now + TimeSpan.FromMilliseconds(200))
            .Should().BeFalse();
            timerSet.AddOrUpdateToLater(t, clock.Now + TimeSpan.FromMilliseconds(200))
            .Should().BeTrue();
            t.FiredAt.Should().Be(default);
Example #5
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());
        }
Example #6
0
    public async Task BasicTest()
    {
        var clock = MomentClockSet.Default.CpuClock;

        await using var timerSet = new ConcurrentTimerSet <Timer>(
                        new ConcurrentTimerSet <Timer> .Options()
        {
            Quanta = TimeSpan.FromMilliseconds(10),
            Clock  = clock,
        },
                        timer => timer.FiredAt = clock.Now);

        // AddOrUpdateToLater
        var t = new Timer();

        timerSet.AddOrUpdate(t, clock.Now + TimeSpan.FromMilliseconds(100));
        timerSet.AddOrUpdateToEarlier(t, clock.Now + TimeSpan.FromMilliseconds(200))
        .Should().BeFalse();
        timerSet.AddOrUpdateToLater(t, clock.Now + TimeSpan.FromMilliseconds(200))
        .Should().BeTrue();
        t.FiredAt.Should().Be(default);