0

I'm currently implementing a session pool for WinScp sessions, which intended purpose is to open multiple sftp sessions concurrently. While my current solution works, I'm not sure if the solution is fool proof during heavy traffic. Also, from what I've read it's the resposibility of the service using the session pool to release the sessions after use. Is this the generally accepted approach or should this be the responsibility of the session pool itself? If this is the responsibility of the session pool, how would I go about implementing this?

Any help and other tips regarding the current implementation are much appreciated!

public class WinScpSessionPool
    {
        private int _connectionCount;

        private readonly ConcurrentQueue<Session> _winScpSessionQueue;
        private readonly int _maxPoolSize;
        private readonly SessionOptions _sessionOptions;

        public WinScpSessionPool(SessionOptions sessionOptions, int maxPoolSize)
        {
            _maxPoolSize = maxPoolSize;
            _connectionCount = maxPoolSize;
            _sessionOptions = sessionOptions;
            _winScpSessionQueue = new ConcurrentQueue<Session>();

            for (int i = 0; i < _maxPoolSize; i++)
            {
                _winScpSessionQueue.Enqueue(new Session());
            }
        }

        public Session GetSession()
        {
            Session session;

            bool sessionAvailable = _winScpSessionQueue.TryDequeue(out session);

            while (!sessionAvailable)
            {
                Thread.Sleep(200);
                sessionAvailable = _winScpSessionQueue.TryDequeue(out session);
            }

            DecrementConnectionCount();

            session.Open(_sessionOptions);

            return session;
        }

        public void ReleaseSession(Session session)
        {
            Require.IsNotNull(session, nameof(session));

            if (session.Opened) session.Close();

            if (Interlocked.CompareExchange(ref _connectionCount, 0, 0) < _maxPoolSize)
            {
                _winScpSessionQueue.Enqueue(session);
                IncrementConnectionCount();
            }
        }

        private void IncrementConnectionCount()
        {
            int incrementedValue = Interlocked.Increment(ref _connectionCount);

            if (incrementedValue > _maxPoolSize)
            {
                _winScpSessionQueue.TryDequeue(out Session _);
            }
        }

        private void DecrementConnectionCount()
        {
            int decrementedValue = Interlocked.Decrement(ref _connectionCount);

            if (decrementedValue < 0)
            {
                _winScpSessionQueue.Enqueue(new Session());
            }
        }
    }
1
  • Hmm... what kind of application is that you are implementing ? Web app? Win Forms? Console app? WPF?
    – Athadu
    Jul 22 at 2:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.