<commit_before>package etcdstoreadapter

import (
	"crypto/tls"
	"errors"
	"fmt"
	"net"
	"net/http"
	"sync"
	"time"

	"code.cloudfoundry.org/workpool"
	"github.com/cloudfoundry/storeadapter"
	"github.com/coreos/go-etcd/etcd"
	"github.com/nu7hatch/gouuid"
)

type ETCDStoreAdapter struct {
	client            *etcd.Client
	workPool          *workpool.WorkPool
	inflightWatches   map[chan bool]bool
	inflightWatchLock *sync.Mutex
}

func New(options *ETCDOptions, workPool *workpool.WorkPool) (*ETCDStoreAdapter, error) {
	if options.IsSSL {
		return newTLSClient(options, workPool)
	}

	return newHTTPClient(options, workPool), nil
}

func newHTTPClient(options *ETCDOptions, workPool *workpool.WorkPool) *ETCDStoreAdapter {
	client := etcd.NewClient(options.ClusterUrls)
	tr := &http.Transport{
		Dial: (&net.Dialer{
			Timeout:   time.Second,
			KeepAlive: time.Second,
		}).Dial,
		MaxIdleConns:        options.MaxIdleConns,
		MaxIdleConnsPerHost: options.MaxIdleConns,
	}
	client.SetTransport(tr)
	return newAdapter(client, workPool)
}

func newTLSClient(options *ETCDOptions, workPool *workpool.WorkPool) (*ETCDStoreAdapter, error) {
	client, err := NewETCDTLSClient(options)
	if err != nil {
		return nil, err
	}

	return newAdapter(client, workPool), nil
}

func NewETCDTLSClient(options *ETCDOptions) (*etcd.Client, error) {
	client := etcd.NewClient(options.ClusterUrls)
	tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
	if err != nil {
		return nil, err
	}

	tlsConfig := &tls.Config{
		Certificates:       []tls.Certificate{tlsCert},
		InsecureSkipVerify: false,
	}

	tr := &http.Transport{
		TLSClientConfig: tlsConfig,
		Dial: (&net.Dialer{
			Timeout:   time.Second,
			KeepAlive: time.Second,
		}).Dial,
		MaxIdleConns:        options.MaxIdleConns,
		MaxIdleConnsPerHost: options.MaxIdleConns,
	}
	client.SetTransport(tr)<commit_msg>Set a default Idle connection timeout<commit_after>package etcdstoreadapter

import (
	"crypto/tls"
	"errors"
	"fmt"
	"net"
	"net/http"
	"sync"
	"time"

	"code.cloudfoundry.org/workpool"
	"github.com/cloudfoundry/storeadapter"
	"github.com/coreos/go-etcd/etcd"
	"github.com/nu7hatch/gouuid"
)

type ETCDStoreAdapter struct {
	client            *etcd.Client
	workPool          *workpool.WorkPool
	inflightWatches   map[chan bool]bool
	inflightWatchLock *sync.Mutex
}

func New(options *ETCDOptions, workPool *workpool.WorkPool) (*ETCDStoreAdapter, error) {
	if options.IsSSL {
		return newTLSClient(options, workPool)
	}

	return newHTTPClient(options, workPool), nil
}

func newHTTPClient(options *ETCDOptions, workPool *workpool.WorkPool) *ETCDStoreAdapter {
	client := etcd.NewClient(options.ClusterUrls)
	tr := &http.Transport{
		Dial: (&net.Dialer{
			Timeout:   time.Second,
			KeepAlive: time.Second,
		}).Dial,
		MaxIdleConns:        options.MaxIdleConns,
		MaxIdleConnsPerHost: options.MaxIdleConns,
		IdleConnTimeout:     2 * time.Minute,
	}
	client.SetTransport(tr)
	return newAdapter(client, workPool)
}

func newTLSClient(options *ETCDOptions, workPool *workpool.WorkPool) (*ETCDStoreAdapter, error) {
	client, err := NewETCDTLSClient(options)
	if err != nil {
		return nil, err
	}

	return newAdapter(client, workPool), nil
}

func NewETCDTLSClient(options *ETCDOptions) (*etcd.Client, error) {
	client := etcd.NewClient(options.ClusterUrls)
	tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
	if err != nil {
		return nil, err
	}

	tlsConfig := &tls.Config{
		Certificates:       []tls.Certificate{tlsCert},
		InsecureSkipVerify: false,
	}

	tr := &http.Transport{
		TLSClientConfig: tlsConfig,
		Dial: (&net.Dialer{
			Timeout:   time.Second,
			KeepAlive: time.Second,
		}).Dial,
		MaxIdleConns:        options.MaxIdleConns,
		MaxIdleConnsPerHost: options.MaxIdleConns,
		IdleConnTimeout:     2 * time.Minute,
	}
	client.SetTransport(tr)
{'fraction_non_alphanumeric': 0.11914672216441206, 'fraction_numerical': 0.0026014568158168575, 'mean_word_length': 4.492857142857143, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7574031', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1413, 'n_words': 262}