<commit_before>package common

import (
	"time"

	"github.com/garyburd/redigo/redis"
)

// RedisConnector ...
type RedisConnector struct{}

// NewPool returns a new pool of Redis connections
func (rc *RedisConnector) NewPool(socketPath, host, password string, db int) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := rc.open(socketPath, host, password, db)
			if err != nil {
				return nil, err
			}

			if db != 0 {
				_, err = c.Do("SELECT", db)
			}

			if err != nil {
				return nil, err
			}
			return c, err
		},
		// PINGs connections that have been idle more than 15 seconds
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if time.Since(t) < time.Duration(15*time.Second) {
				return nil
			}
			_, err := c.Do("PING")
			return err
		},
	}
}

// Open a new Redis connection
func (rc *RedisConnector) open(socketPath, host, password string, db int) (redis.Conn, error) {
	var opts = []redis.DialOption{redis.DialDatabase(db)}

	if password != "" {
		opts = append(opts, redis.DialPassword(password))<commit_msg>Set read and write timeouts on the redis connection<commit_after>package common

import (
	"time"

	"github.com/garyburd/redigo/redis"
)

// RedisConnector ...
type RedisConnector struct{}

// NewPool returns a new pool of Redis connections
func (rc *RedisConnector) NewPool(socketPath, host, password string, db int) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := rc.open(socketPath, host, password, db)
			if err != nil {
				return nil, err
			}

			if db != 0 {
				_, err = c.Do("SELECT", db)
			}

			if err != nil {
				return nil, err
			}
			return c, err
		},
		// PINGs connections that have been idle more than 15 seconds
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if time.Since(t) < time.Duration(15*time.Second) {
				return nil
			}
			_, err := c.Do("PING")
			return err
		},
	}
}

// Open a new Redis connection
func (rc *RedisConnector) open(socketPath, host, password string, db int) (redis.Conn, error) {
	var opts = []redis.DialOption{
		redis.DialDatabase(db),
		redis.DialReadTimeout(15 * time.Second),
		redis.DialWriteTimeout(15 * time.Second),
		redis.DialConnectTimeout(15 * time.Second),
	}

	if password != "" {
		opts = append(opts, redis.DialPassword(password))
{'fraction_non_alphanumeric': 0.13341594382486577, 'fraction_numerical': 0.009913258983890954, 'mean_word_length': 3.2416812609457093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5960240', 'n_tokens_mistral': 979, 'n_tokens_neox': 865, 'n_words': 242}