Exemplo n.º 1
0
            public async Task CreateReservedSessions()
            {
                var s = ReserveSessionsCount;

                for (var i = 0; i < s; i++)
                {
                    _readonlySessions.Add(await EsentSession.CreateReadOnlySession(MainSession, Waiters));
                }
            }
Exemplo n.º 2
0
            public async ValueTask <(IEsentSession session, IDisposable disposable)> GetAvailableSession(bool autoUse)
            {
                async Task <Nothing> DoDisposeExccessSessions()
                {
                    async Task DoDispose(EsentSession s)
                    {
                        try
                        {
                            await s.DisposeInternal();
                        }
                        catch
                        {
                            // Игнорируем ошибки
                        }
                    }

                    List <EsentSession> toDispose = new List <EsentSession>();

                    lock (Waiters.UsingLock)
                    {
                        var allSessions  = _readonlySessions.Count;
                        var freeSessions = _readonlySessions.Where(s => s.UsingsCount == 0).ToArray();
                        if (allSessions > MaxReserveSessionsCount)
                        {
                            toDispose.AddRange(freeSessions.Skip(ReserveSessionsCount));
                        }
                        foreach (var s in toDispose)
                        {
                            _readonlySessions.Remove(s);
                        }
                    }
                    if (toDispose.Count > 0)
                    {
                        var t1 = Task.WhenAll(toDispose.Select(DoDispose));
                        var t2 = Task.Delay(TimeSpan.FromSeconds(15));
                        await Task.WhenAny(t1, t2);
                    }
                    return(Nothing.Value);
                }

                lock (Waiters.UsingLock)
                {
                    var freeSessions = _readonlySessions.Where(s => s.UsingsCount == 0).ToArray();
                    if (freeSessions.Length > 0)
                    {
                        EsentSession r;
                        IDisposable  d = null;
                        if (freeSessions.Length == 1)
                        {
                            r = freeSessions[0];
                        }
                        else
                        {
                            r = freeSessions[_rnd.Next(0, freeSessions.Length)];
                        }
                        if (autoUse)
                        {
                            r.UsingsCount++;
                            d = r.CreateUsageDisposable();
                        }
                        return(r, d);
                    }
                }
                var newSession = await EsentSession.CreateReadOnlySession(MainSession, Waiters);

                IDisposable d2 = null;

                lock (Waiters.UsingLock)
                {
                    _readonlySessions.Add(newSession);
                    if (autoUse)
                    {
                        newSession.UsingsCount++;
                        d2 = newSession.CreateUsageDisposable();
                    }
                }
                CoreTaskHelper.RunUnawaitedTaskAsync(DoDisposeExccessSessions);
                return(newSession, d2);
            }