public AmqpUnit CreateAmqpUnit(
     DeviceIdentity deviceIdentity,
     Func <MethodRequestInternal, Task> methodHandler,
     Action <Twin, string, TwinCollection> twinMessageListener,
     Func <string, Message, Task> eventListener)
 {
     if (Logging.IsEnabled)
     {
         Logging.Enter(this, deviceIdentity, $"{nameof(CreateAmqpUnit)}");
     }
     if (deviceIdentity.AuthenticationModel != AuthenticationModel.X509 && (deviceIdentity.AmqpTransportSettings?.AmqpConnectionPoolSettings?.Pooling ?? false))
     {
         IAmqpConnectionHolder amqpConnectionHolder;
         lock (Lock)
         {
             ISet <IAmqpConnectionHolder> amqpConnectionHolders = ResolveConnectionGroup(deviceIdentity, true);
             if (amqpConnectionHolders.Count < deviceIdentity.AmqpTransportSettings.AmqpConnectionPoolSettings.MaxPoolSize)
             {
                 amqpConnectionHolder = new AmqpConnectionHolder(deviceIdentity);
                 amqpConnectionHolder.OnConnectionDisconnected += (o, args) => RemoveConnection(amqpConnectionHolders, o as IAmqpConnectionHolder);
                 amqpConnectionHolders.Add(amqpConnectionHolder);
                 if (Logging.IsEnabled)
                 {
                     Logging.Associate(this, amqpConnectionHolder, "amqpConnectionHolders");
                 }
             }
             else
             {
                 amqpConnectionHolder = GetLeastUsedConnection(amqpConnectionHolders);
             }
         }
         if (Logging.IsEnabled)
         {
             Logging.Exit(this, deviceIdentity, $"{nameof(CreateAmqpUnit)}");
         }
         return(amqpConnectionHolder.CreateAmqpUnit(deviceIdentity, methodHandler, twinMessageListener, eventListener));
     }
     else
     {
         if (Logging.IsEnabled)
         {
             Logging.Exit(this, deviceIdentity, $"{nameof(CreateAmqpUnit)}");
         }
         return(new AmqpConnectionHolder(deviceIdentity)
                .CreateAmqpUnit(deviceIdentity, methodHandler, twinMessageListener, eventListener));
     }
 }
        private AmqpConnectionHolder ResolveConnectionByHashing(AmqpConnectionHolder[] pool, DeviceIdentity deviceIdentity)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, deviceIdentity, $"{nameof(ResolveConnectionByHashing)}");
            }
            int index = Math.Abs(deviceIdentity.GetHashCode()) % pool.Length;

            if (pool[index] == null)
            {
                pool[index] = new AmqpConnectionHolder(deviceIdentity);
            }

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, deviceIdentity, $"{nameof(ResolveConnectionByHashing)}");
            }
            return(pool[index]);
        }
        private AmqpConnectionHolder ResolveConnectionByHashing(AmqpConnectionHolder[] pool, IDeviceIdentity deviceIdentity)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, deviceIdentity, nameof(ResolveConnectionByHashing));
            }

            int index = GetDeviceIdentityIndex(deviceIdentity, pool.Length);

            if (pool[index] == null)
            {
                pool[index] = new AmqpConnectionHolder(deviceIdentity);
            }

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, deviceIdentity, nameof(ResolveConnectionByHashing));
            }

            return(pool[index]);
        }
Beispiel #4
0
        private AmqpConnectionHolder[] ResolveConnectionGroup(DeviceIdentity deviceIdentity)
        {
            if (deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual)
            {
                if (_amqpSasIndividualPool == null)
                {
                    _amqpSasIndividualPool = new AmqpConnectionHolder[deviceIdentity.AmqpTransportSettings.AmqpConnectionPoolSettings.MaxPoolSize];
                }

                return(_amqpSasIndividualPool);
            }
            else
            {
                string scope = deviceIdentity.IotHubConnectionString.SharedAccessKeyName;
                _amqpSasGroupedPool.TryGetValue(scope, out AmqpConnectionHolder[] amqpConnectionHolders);
                if (amqpConnectionHolders == null)
                {
                    amqpConnectionHolders = new AmqpConnectionHolder[deviceIdentity.AmqpTransportSettings.AmqpConnectionPoolSettings.MaxPoolSize];
                    _amqpSasGroupedPool.Add(scope, amqpConnectionHolders);
                }

                return(amqpConnectionHolders);
            }
        }