protected abstract IStreamQueryDistributor CreateDistributor(
     Func<Lease, Task> onReceive = null,
     LeasableResource[] leasableResources = null,
     int maxDegreesOfParallelism = 5,
     [CallerMemberName] string name = null,
     TimeSpan? waitInterval = null,
     string scope = null);
        protected override IStreamQueryDistributor CreateDistributor(
            Func<Lease, Task> onReceive = null,
            LeasableResource[] leasableResources = null,
            int maxDegreesOfParallelism = 1,
            string name = null,
            TimeSpan? waitInterval = null,
            string scope = null)
        {
            leasableResources = leasableResources ?? DefaultLeasableResources;

            scope = scope ?? DateTimeOffset.UtcNow.Ticks.ToString();
            distributor = new SqlBrokeredStreamQueryDistributor(
                leasableResources,
                settings,
                scope,
                maxDegreesOfParallelism,
                waitInterval,
                DefaultLeaseDuration);

            if (onReceive != null)
            {
                distributor.OnReceive(onReceive);
            }

            ProvisionLeasableResources(leasableResources, scope);

            return distributor;
        }
 public InMemoryStreamQueryDistributor(
     LeasableResource[] leasableResources,
     string scope,
     int maxDegreesOfParallelism = 5,
     TimeSpan? waitInterval = null) :
         base(leasableResources,
              maxDegreesOfParallelism,
              waitInterval)
 {
     if (scope == null)
     {
         throw new ArgumentNullException("scope");
     }
     workInProgress = workInProgressGlobal.GetOrAdd(scope, s => new ConcurrentDictionary<LeasableResource, Lease>());
 }
 protected StreamQueryDistributorBase(
     LeasableResource[] leasableResources,
     int maxDegreesOfParallelism = 5,
     TimeSpan? waitInterval = null)
 {
     if (leasableResources == null)
     {
         throw new ArgumentNullException("leasableResources");
     }
     if (maxDegreesOfParallelism <= 0)
     {
         throw new ArgumentException("maxDegreesOfParallelism must be at least 1.");
     }
     this.leasableResources = leasableResources;
     this.maxDegreesOfParallelism = Math.Min(maxDegreesOfParallelism, leasableResources.Count());
     this.waitInterval = waitInterval ?? TimeSpan.FromSeconds(.5);
 }
Esempio n. 5
0
        public Lease(
            LeasableResource leasableResource,
            TimeSpan duration,
            dynamic ownerToken = null,
            Func<TimeSpan, Task> extend = null)
        {
            if (leasableResource == null)
            {
                throw new ArgumentNullException("leasableResource");
            }

            this.leasableResource = leasableResource;
            this.duration = duration;
            this.ownerToken = ownerToken;
            this.extend = extend;

            cancellationTokenSource.CancelAfter(Duration);
        }
 protected override IStreamQueryDistributor CreateDistributor(
     Func<Lease, Task> onReceive = null,
     LeasableResource[] leasableResources = null,
     int maxDegreesOfParallelism = 5,
     string name = null,
     TimeSpan? waitInterval = null,
     string scope = null)
 {
     distributor = new InMemoryStreamQueryDistributor(
         leasableResources ?? DefaultLeasableResources,
         scope ?? DateTimeOffset.UtcNow.Ticks.ToString(),
         maxDegreesOfParallelism,
         waitInterval);
     if (onReceive != null)
     {
         distributor.OnReceive(onReceive);
     }
     return distributor;
 }
        private void ProvisionLeasableResources(LeasableResource[] leasableResources, string scope)
        {
            using (var connection = new SqlConnection(settings.ConnectionString))
            {
                connection.Open();

                foreach (var resource in leasableResources)
                {
                    var cmd = connection.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = @"
IF NOT EXISTS (SELECT * FROM [Alluvial].[Leases] 
               WHERE Scope = @scope AND 
               ResourceName = @resourceName)
    BEGIN
        INSERT INTO [Alluvial].[Leases]
                        ([ResourceName],
                         [Scope],
                         [LastGranted],
                         [LastReleased],
                         [Expires])
                 VALUES 
                        (@resourceName, 
                         @scope,
                         @lastGranted,
                         @lastReleased,
                         @expires)
    END";
                    cmd.Parameters.AddWithValue(@"@resourceName", resource.Name);
                    cmd.Parameters.AddWithValue(@"@scope", scope);
                    cmd.Parameters.AddWithValue(@"@lastGranted", resource.LeaseLastGranted);
                    cmd.Parameters.AddWithValue(@"@lastReleased", resource.LeaseLastReleased);
                    cmd.Parameters.AddWithValue(@"@expires", DateTimeOffset.MinValue);
                    cmd.ExecuteScalar();
                }
            }
        }