Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: When do released connections expire? #445

Closed
kontinuity opened this issue Mar 22, 2014 · 4 comments
Closed

Question: When do released connections expire? #445

kontinuity opened this issue Mar 22, 2014 · 4 comments

Comments

@kontinuity
Copy link

We get very infrequent bursts of redis connection requests, and what that does is that the number of connections to the Redis server to go up but stay there indefinitely. I didn't see anything in the code that cleans up such connections.

What is the best way to get rid of such released idle connections within a timeout without setting a client timeout on the server?

Appreciate the help

@andymccurdy
Copy link
Contributor

Typically I've seen this handled by setting an idle timeout on the server.

However, you could likely implement a custom ConnectionPool that close idle connections after a bit of time.

@kontinuity
Copy link
Author

Thanks Andy!

Do connections waiting for blpop count as idle on the server?

@kontinuity
Copy link
Author

Using the following modified ConnectionPool to evict idle connections.

import gevent
from gevent.lock import RLock
import redis

from commons.util.logging_helper import get_logger

logger = get_logger(__name__)


class DynamicConnectionPool(redis.ConnectionPool):
    def __init__(self, connection_class=redis.Connection, max_connections=None, idle_connections=10,
                 **connection_kwargs):
        redis.ConnectionPool.__init__(self, connection_class, max_connections, **connection_kwargs)
        self.idle_connections = idle_connections
        self.eviction_lock = RLock()
        self.evict_idle_connections()

    def get_connection(self, command_name, *keys, **options):
        try:
            self.eviction_lock.acquire()
            return redis.ConnectionPool.get_connection(self, command_name, *keys, **options)
        finally:
            self.eviction_lock.release()

    def release(self, connection):
        try:
            self.eviction_lock.acquire()
            return redis.ConnectionPool.release(self, connection)
        finally:
            self.eviction_lock.release()

    def disconnect(self):
        try:
            self.eviction_lock.acquire()
            return redis.ConnectionPool.disconnect(self)
        finally:
            self.eviction_lock.release()


    def evict_idle_connections(self):
        try:
            self.eviction_lock.acquire()
            if self._created_connections > self.idle_connections:
                diff = min(self._created_connections - self.idle_connections, len(self._available_connections))
                j = 0
                for i in range(diff):
                    try:
                        connection = self._available_connections.pop(0)
                        connection.disconnect()
                        self._created_connections -= 1
                        j += 1
                    except:
                        pass
                logger.info('Evicted %s of required %s redis connections' % (j, diff))

            logger.info('Post eviction run status max=%s;max_idle=%s;actual_idle=%s;created=%s'
                        % (self.max_connections, self.idle_connections,
                           len(self._available_connections), self._created_connections))
        finally:
            self.eviction_lock.release()

        gevent.spawn_later(60, self.evict_idle_connections)

@andymccurdy
Copy link
Contributor

Looks like that'll do it. Feel free to re-open if you need anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants