sessions icon indicating copy to clipboard operation
sessions copied to clipboard

Pings

Open sloweclair opened this issue 7 years ago • 2 comments

Every time a Get/Set is called it pings the redis server first which is a huge waste of time. This is happening because every Get/Set request calls NewRediStore from redistore and that causes a ping.

From my understanding it should only be calling NewRediStore on boot since that is when it makes the pool, not every request.

Thanks! Spencer

sloweclair avatar Dec 15 '16 21:12 sloweclair

The session store gets created once, so why should there be an issue within this middleware?

tboerger avatar Dec 16 '16 15:12 tboerger

@sloweclair That's actually caused by redistore, the superset library which this middleware relies on. It creates a Redis connection pool with a setting which pings the DB every time request occurs.

To avoid this and add ability to customize the behavior, I have written a new API sessions.NewRedisStoreWithPool which allows you to use your own redis.Pool as a backend connection pool. The API has already been merged (#5) so you can now fix the problem by simply changing your API call to use the custom Redis pool.

Here's an example of custom Redis pool:

&redis.Pool{
	MaxIdle: 10,
	IdleTimeout: 30 * time.Second,
	// ...
	TestOnBorrow: func(conn redis.Conn, t time.Time) error {
		if time.Since(t) < 5*time.Second {
			return nil
		}
		_, err := conn.Do("PING")
		return err
	},
})

This example code skips the connection check for five seconds from the last successful connection on same redis.Conn (There could obviously be up to 10 simultaneous connections, so you might still see multiple pings until TestOnBorrow hits the reused connection).

ktogo avatar Jan 07 '17 08:01 ktogo