Esempio n. 1
0
        /// <inheritdoc />
        public void ReleaseSession(ICoreServerSession session)
        {
            lock (_lock)
            {
                var removeCount = 0;
                for (var i = 0; i < _pool.Count; i++)
                {
                    var pooledSession = _pool[i];
                    if (IsAboutToExpire(pooledSession))
                    {
                        pooledSession.Dispose();
                        removeCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                _pool.RemoveRange(0, removeCount);

                if (IsAboutToExpire(session))
                {
                    session.Dispose();
                }
                else
                {
                    _pool.Add(session);
                }
            }
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="CoreSession" /> class.
 /// </summary>
 /// <param name="serverSession">The server session.</param>
 /// <param name="options">The options.</param>
 public CoreSession(
     ICoreServerSession serverSession,
     CoreSessionOptions options)
 {
     _serverSession = Ensure.IsNotNull(serverSession, nameof(serverSession));
     _options       = Ensure.IsNotNull(options, nameof(options));
 }
 // private methods
 private CoreSession CreateSubject(
     ICluster cluster = null,
     ICoreServerSession serverSession = null,
     CoreSessionOptions options       = null)
 {
     serverSession = serverSession ?? Mock.Of <ICoreServerSession>();
     options       = options ?? new CoreSessionOptions();
     return(new CoreSession(serverSession, options));
 }
Esempio n. 4
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="CoreSession" /> class.
 /// </summary>
 /// <param name="cluster">The cluster.</param>
 /// <param name="serverSession">The server session.</param>
 /// <param name="options">The options.</param>
 public CoreSession(
     ICluster cluster,
     ICoreServerSession serverSession,
     CoreSessionOptions options)
 {
     _cluster       = Ensure.IsNotNull(cluster, nameof(cluster));
     _serverSession = Ensure.IsNotNull(serverSession, nameof(serverSession));
     _options       = Ensure.IsNotNull(options, nameof(options));
 }
 public CoreSession(
     ICluster cluster,
     ICoreServerSession serverSession,
     CoreSessionOptions options)
     : this(cluster, options : options)
 {
     Ensure.IsNotNull(serverSession, nameof(serverSession));
     _serverSession = new Lazy <ICoreServerSession>(() => serverSession);
 }
Esempio n. 6
0
 private CoreSession CreateSubject(
     ICluster cluster = null,
     ICoreServerSession serverSession = null,
     CoreSessionOptions options       = null)
 {
     cluster       = cluster ?? CreateMockReplicaSetCluster();
     serverSession = serverSession ?? Mock.Of <ICoreServerSession>();
     options       = options ?? new CoreSessionOptions();
     return(new CoreSession(cluster, serverSession, options));
 }
Esempio n. 7
0
        private CoreSession CreateSubject(
            ICluster cluster = null,
            ICoreServerSession serverSession = null,
            CoreSessionOptions options       = null)
        {
            cluster       = cluster ?? CreateMockReplicaSetCluster();
            serverSession = serverSession ?? Mock.Of <ICoreServerSession>();
            options       = options ?? new CoreSessionOptions();
#pragma warning disable CS0618 // Type or member is obsolete
            return(new CoreSession(cluster, serverSession, options));

#pragma warning restore CS0618 // Type or member is obsolete
        }
        /// <inheritdoc />
        public void ReleaseSession(ICoreServerSession session)
        {
            // if session is not expired - put it in the FIFO pool

            if (IsAboutToExpire(session))
            {
                session.Dispose();
            }
            else
            {
                _pool.Enqueue(session);
            }
        }
Esempio n. 9
0
        // private methods
        private ICoreSessionHandle CreateCoreSession(
            ICoreServerSession serverSession = null,
            CoreSessionOptions options       = null)
        {
            serverSession = serverSession ?? new CoreServerSession();
            options       = options ?? new CoreSessionOptions();

            var mockCoreSession = new Mock <ICoreSessionHandle>();

            mockCoreSession.SetupGet(m => m.Options).Returns(options);
            mockCoreSession.SetupGet(m => m.ServerSession).Returns(serverSession);
            mockCoreSession.Setup(m => m.Fork()).Returns(() => CreateCoreSession(serverSession: serverSession, options: options));
            return(mockCoreSession.Object);
        }
Esempio n. 10
0
        // private methods
        private Mock <ICoreSessionHandle> CreateCoreSessionMock(
            ICoreServerSession serverSession = null,
            CoreSessionOptions options       = null)
        {
            serverSession = serverSession ?? new CoreServerSession();
            options       = options ?? new CoreSessionOptions();

            var mockCoreSession = new Mock <ICoreSessionHandle>();

            mockCoreSession.Setup(m => m.CurrentTransaction).Returns(new CoreTransaction(It.IsAny <long>(), It.IsAny <TransactionOptions>()));
            mockCoreSession.SetupGet(m => m.Options).Returns(options);
            mockCoreSession.SetupGet(m => m.ServerSession).Returns(serverSession);
            mockCoreSession.Setup(m => m.Fork()).Returns(() => CreateCoreSession(serverSession: serverSession, options: options));
            return(mockCoreSession);
        }
Esempio n. 11
0
        // private methods
        private bool IsAboutToExpire(ICoreServerSession session)
        {
            var logicalSessionTimeout = _cluster.Description.LogicalSessionTimeout;

            if (!session.LastUsedAt.HasValue || !logicalSessionTimeout.HasValue)
            {
                return(true);
            }
            else
            {
                var expiresAt     = session.LastUsedAt.Value + logicalSessionTimeout.Value;
                var timeRemaining = expiresAt - DateTime.UtcNow;
                return(timeRemaining < TimeSpan.FromMinutes(1));
            }
        }
        // private methods
        private bool IsAboutToExpire(ICoreServerSession session)
        {
            var logicalSessionTimeout = _cluster.Description.LogicalSessionTimeout;
            var clusterType           = _cluster.Description.Type;

            if (clusterType == ClusterType.LoadBalanced)
            {
                return(false);  // sessions never expire in load balancing mode
            }
            else if (session.LastUsedAt.HasValue && logicalSessionTimeout.HasValue)
            {
                var expiresAt     = session.LastUsedAt.Value + logicalSessionTimeout.Value;
                var timeRemaining = expiresAt - DateTime.UtcNow;
                return(timeRemaining < TimeSpan.FromMinutes(1));
            }
            else
            {
                return(true);
            }
        }
Esempio n. 13
0
 private ICoreSessionHandle CreateCoreSession(
     ICoreServerSession serverSession = null,
     CoreSessionOptions options       = null)
 {
     return(CreateCoreSessionMock(serverSession, options).Object);
 }
Esempio n. 14
0
 // constructors
 public ServerSession(ICoreServerSession coreServerSession)
 {
     _coreServerSession = Ensure.IsNotNull(coreServerSession, nameof(coreServerSession));
 }
        public static bool IsAboutToExpire(this CoreServerSessionPool obj, ICoreServerSession session)
        {
            var methodInfo = typeof(CoreServerSessionPool).GetMethod("IsAboutToExpire", BindingFlags.NonPublic | BindingFlags.Instance);

            return((bool)methodInfo.Invoke(obj, new object[] { session }));
        }
Esempio n. 16
0
 // constructors
 public WrappingCoreServerSession(ICoreServerSession wrapped, bool ownsWrapped)
 {
     _wrapped     = Ensure.IsNotNull(wrapped, nameof(wrapped));
     _ownsWrapped = ownsWrapped;
 }
Esempio n. 17
0
 public MockWrappingCoreServerSession(ICoreServerSession wrapped, bool ownsWrapped)
     : base(wrapped, ownsWrapped)
 {
 }
Esempio n. 18
0
 // constructors
 public ReleaseOnDisposeCoreServerSession(ICoreServerSession wrapped, ICoreServerSessionPool pool)
     : base(wrapped, ownsWrapped: false)
 {
     _pool = Ensure.IsNotNull(pool, nameof(pool));
 }
 private bool IsAboutToExpireOrDirty(ICoreServerSession session)
 {
     return(IsAboutToExpire(session) || session.IsDirty);
 }