<commit_before>package config

import (
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"net/url"

	"time"

	"github.com/Sirupsen/logrus"
	"github.com/go-errors/errors"
	"github.com/ory-am/hydra/pkg"
	"github.com/spf13/viper"
	r "gopkg.in/dancannon/gorethink.v2"
)

type MemoryConnection struct{}

type RethinkDBConnection struct {
	session *r.Session
	URL     *url.URL
}

func (c *RethinkDBConnection) GetSession() *r.Session {
	if c.session != nil {
		return c.session
	}

	var err error
	var username, password string
	database := c.URL.Path[1:]
	if c.URL.User != nil {
		password, _ = c.URL.User.Password()
		username = c.URL.User.Username()
	}

	if err := pkg.Retry(time.Second*15, time.Minute*2, func() error {
		logrus.Infof("Connecting with RethinkDB: %s (%s) (%s)", c.URL.String(), c.URL.Host, database)

		options := r.ConnectOpts{
			Address:  c.URL.Host,
			Username: username,
			Password: password,
		}

		importRethinkDBRootCA(&options)

		if c.session, err = r.Connect(options); err != nil {
			return errors.Errorf("Could not connect to RethinkDB: %s", err)
		}
<commit_msg>rethink: set keep alive, close #146<commit_after>package config

import (
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"net/url"

	"time"

	"github.com/Sirupsen/logrus"
	"github.com/go-errors/errors"
	"github.com/ory-am/hydra/pkg"
	"github.com/spf13/viper"
	r "gopkg.in/dancannon/gorethink.v2"
)

type MemoryConnection struct{}

type RethinkDBConnection struct {
	session *r.Session
	URL     *url.URL
}

func (c *RethinkDBConnection) GetSession() *r.Session {
	if c.session != nil {
		return c.session
	}

	var err error
	var username, password string
	if len(c.URL.Path) <= 1 {
		logrus.Fatalf("Database hostname specified, but database name is missing.")
	}

	database := c.URL.Path[1:]
	if c.URL.User != nil {
		password, _ = c.URL.User.Password()
		username = c.URL.User.Username()
	}

	if err := pkg.Retry(time.Second*15, time.Minute*2, func() error {
		logrus.Infof("Connecting with RethinkDB: %s (%s) (%s)", c.URL.String(), c.URL.Host, database)

		options := r.ConnectOpts{
			Address:  c.URL.Host,
			Username: username,
			Password: password,
			KeepAlivePeriod: 10 * time.Second,
		}

		importRethinkDBRootCA(&options)

		if c.session, err = r.Connect(options); err != nil {
			return errors.Errorf("Could not connect to RethinkDB: %s", err)
		}

{'fraction_non_alphanumeric': 0.1520717642033319, 'fraction_numerical': 0.011106364801366937, 'mean_word_length': 3.731313131313131, 'pattern_counts': {'":': 0, '<': 4, '<?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': '790424', 'n_tokens_mistral': 971, 'n_tokens_neox': 906, 'n_words': 195}