public Sender Get(string entitypath, string viaEntityPath, string namespaceName)
            {
                var buffer = MessageReceivers.GetOrAdd(entitypath + viaEntityPath + namespaceName, s =>
                {
                    var b = new CircularBuffer <EntityClientEntry>(numberOfSendersPerEntity);
                    for (var i = 0; i < numberOfSendersPerEntity; i++)
                    {
                        var e = new EntityClientEntry
                        {
                            ClientEntity = receiveFactory(entitypath, viaEntityPath, namespaceName).GetAwaiter()
                                           .GetResult()
                        };
                        b.Put(e);
                    }
                    return(b);
                });

                var entry = buffer.Get();

                if (entry.ClientEntity.IsClosed)
                {
                    lock (entry.Mutex)
                    {
                        if (entry.ClientEntity.IsClosed)
                        {
                            entry.ClientEntity = receiveFactory(entitypath, viaEntityPath, namespaceName).GetAwaiter().GetResult();
                        }
                    }
                }

                return(entry.ClientEntity);
            }
示例#2
0
        public async Task <IMessageReceiverInternal> Get(string entityPath, string namespaceAlias)
        {
            var buffer = await MessageReceivers.GetOrAdd(entityPath + namespaceAlias, async s =>
            {
                var b = new CircularBuffer <EntityClientEntry>(numberOfReceiversPerEntity);
                for (var i = 0; i < numberOfReceiversPerEntity; i++)
                {
                    var e = new EntityClientEntry
                    {
                        ClientEntity = await receiveFactory.Create(entityPath, namespaceAlias)
                                       .ConfigureAwait(false)
                    };
                    b.Put(e);
                }
                return(b);
            }).ConfigureAwait(false);

            var entry = buffer.Get();

            if (!entry.ClientEntity.IsClosed)
            {
                return(entry.ClientEntity);
            }

            try
            {
                await entry.Semaphore.WaitAsync()
                .ConfigureAwait(false);

                if (entry.ClientEntity.IsClosed)
                {
                    entry.ClientEntity = await receiveFactory.Create(entityPath, namespaceAlias)
                                         .ConfigureAwait(false);
                }
                return(entry.ClientEntity);
            }
            finally
            {
                entry.Semaphore.Release();
            }
        }
示例#3
0
 /// <summary>
 /// 设置总量
 /// </summary>
 public void SetNumber()
 {
     Total = MessageReceivers.Count();
 }