// UnixConnection sets the connection to use a Unix socket. The default
// is "/tmp/redis.sock". The default timeout to connect are 30 seconds.
506930
func UnixConnection(socket string, timeout time.Duration) Option {
	return func(d *Database) error {
		if socket == "" {
			socket = defaultSocket
		}
		d.address = socket
		d.network = "unix"
		if timeout < 0 {
			return errors.New(ErrInvalidConfiguration, errorMessages, "timeout", timeout)
		} else if timeout == 0 {
			timeout = defaultTimeout
		}
		d.timeout = timeout
		return nil
	}
}
360780
func (s *TestConnectionOutput) SetConnection(v *Connection) *TestConnectionOutput {
	s.Connection = v
	return s
}
1200417
func (zconn *zconn) connect(timeout time.Duration) (client.Connection, error) {
	connC := make(chan client.Connection, 1)
	zconn.connC <- connC
	select {
	case conn := <-connC:
		return conn, nil
	case <-time.After(timeout):
		glog.Warningf("timed out waiting for connection")
		return nil, ErrTimeout
	case <-zconn.shutdownC:
		glog.Warningf("received signal to shutdown")
		return nil, ErrShutdown
	}
}
1178380
func TcpConnection(address string, timeout time.Duration) Option {
	return func(d *Database) error {
		if address == "" {
			address = defaultAddress
		}
		d.address = address
		d.network = "tcp"
		if timeout < 0 {
			return errors.New(ErrInvalidConfiguration, errorMessages, "timeout", timeout)
		} else if timeout == 0 {
			timeout = defaultTimeout
		}
		d.timeout = timeout
		return nil
	}
}
742695
func (s *GetConnectionOutput) SetConnection(v *Connection) *GetConnectionOutput {
	s.Connection = v
	return s
}
1126080
func DialConnectTimeout(d time.Duration) DialOption {
	return DialOption{func(do *dialOptions) {
		do.dialer.Timeout = d
	}}
}
857598
func NewConnection(host string, port string) *Connection {
	return &Connection{host, port, http.DefaultClient}
}
1365429
func newSocketConnection(cfg *socketConfig) (websocketFeed, error) {
	dialer := &websocket.Dialer{
		Proxy:            http.ProxyFromEnvironment, // Same as DefaultDialer.
		HandshakeTimeout: 10 * time.Second,          // DefaultDialer is 45 seconds.
	}

	conn, _, err := dialer.Dial(cfg.address, nil)
	if err != nil {
		return nil, err
	}
	return &socketClient{
		conn: conn,
		done: make(chan struct{}),
	}, nil
}
89868
func unixSyslog() (net.Conn, error) {
	networks := []string{"unixgram", "unix"}
	logPaths := []string{"/dev/log", "/var/run/syslog"}
	var addr string
	for _, n := range networks {
		for _, p := range logPaths {
			addr = p
			if conn, err := net.Dial(n, addr); err == nil {
				return conn, nil
			}
		}
	}
	return nil, errors.New("Could not connect to local syslog socket")
}
476194
func (s *Service) createConnection(w http.ResponseWriter, r *http.Request) {
	opts := store.ConnectionOptions{
		IdleTimeout: s.ConnIdleTimeout,
		TxTimeout:   s.ConnTxTimeout,
	}

	d, b, err := txTimeout(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	if b {
		opts.TxTimeout = d
	}

	d, b, err = idleTimeout(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	if b {
		opts.IdleTimeout = d
	}

	conn, err := s.store.Connect(&opts)
	if err != nil {
		http.Error(w, err.Error(), http.StatusServiceUnavailable)
		return
	}
	w.Header().Set("Location", s.FormConnectionURL(r, conn.ID))
	w.WriteHeader(http.StatusCreated)
}
1064786
func UnixAddrToSockaddr(addr *net.UnixAddr) (Sockaddr, int) {
	t := 0
	switch addr.Net {
	case "unix":
		t = SOCK_STREAM
	case "unixgram":
		t = SOCK_DGRAM
	case "unixpacket":
		t = SOCK_SEQPACKET
	default:
		return nil, 0
	}
	return &SockaddrUnix{Name: addr.Name}, t
}
954194
func (b *redisBroker) Connect() error {
	if b.pool != nil {
		return nil
	}

	var addr string

	if len(b.opts.Addrs) == 0 || b.opts.Addrs[0] == "" {
		addr = "redis://127.0.0.1:6379"
	} else {
		addr = b.opts.Addrs[0]

		if !strings.HasPrefix("redis://", addr) {
			addr = "redis://" + addr
		}
	}

	b.addr = addr

	b.pool = &redis.Pool{
		MaxIdle:     b.bopts.maxIdle,
		MaxActive:   b.bopts.maxActive,
		IdleTimeout: b.bopts.idleTimeout,
		Dial: func() (redis.Conn, error) {
			return redis.DialURL(
				b.addr,
				redis.DialConnectTimeout(b.bopts.connectTimeout),
				redis.DialReadTimeout(b.bopts.readTimeout),
				redis.DialWriteTimeout(b.bopts.writeTimeout),
			)
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}

	return nil
}
1000267
func NewConnection(addr string, netTimeout time.Duration) (*Conn, error) {
	conn, err := net.DialTimeout("tcp", addr, netTimeout)
	if err != nil {
		return nil, err
	}

	return &Conn{
		addr:       addr,
		Conn:       conn,
		r:          bufio.NewReaderSize(deadline.NewDeadlineReader(conn, netTimeout), 512*1024),
		w:          bufio.NewWriterSize(deadline.NewDeadlineWriter(conn, netTimeout), 512*1024),
		netTimeout: netTimeout,
	}, nil
}
11588
func (c *socks5MuxBindConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
	accepter, ok := conn.(Accepter)
	if !ok {
		return nil, errors.New("wrong connection type")
	}

	return accepter.Accept()
}
1268813
func WithConnectTimeout(value time.Duration) options.Opt {
	return func(p options.Params) {
		if setter, ok := p.(connectTimeoutSetter); ok {
			setter.SetConnectTimeout(value)
		}
	}
}
819247
func newConnection(url url.URL, dialTimeout time.Duration, allowInsecure, enableV2 bool) *connection {
	var isV2 *bool
	if !enableV2 {
		v2 := false
		isV2 = &v2
	}

	var rt http.RoundTripper
	if allowInsecure {
		rt = knet.SetTransportDefaults(&http.Transport{
			Dial: (&net.Dialer{
				Timeout:   dialTimeout,
				KeepAlive: 30 * time.Second,
			}).Dial,
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		})
	} else {
		rt = knet.SetTransportDefaults(&http.Transport{
			Dial: (&net.Dialer{
				Timeout:   dialTimeout,
				KeepAlive: 30 * time.Second,
			}).Dial,
		})
	}

	rt = transport.DebugWrappers(rt)

	jar, _ := cookiejar.New(nil)
	client := &http.Client{Jar: jar, Transport: rt}
	return &connection{
		url:    url,
		client: client,
		cached: make(map[string]repository),
		isV2:   isV2,

		allowInsecure: allowInsecure,
	}
}
864401
func Connect(config *ClientConfig) (*client, error) {
	client := &client{
		config: config,
	}

	p := rand.Perm(len(config.brokers))
	for _, n := range p {
		broker := config.brokers[n]
		log.Printf("D! Output [amqp] connecting to %q", broker)
		conn, err := amqp.DialConfig(
			broker, amqp.Config{
				TLSClientConfig: config.tlsConfig,
				SASL:            config.auth, // if nil, it will be PLAIN taken from url
				Dial: func(network, addr string) (net.Conn, error) {
					return net.DialTimeout(network, addr, config.timeout)
				},
			})
		if err == nil {
			client.conn = conn
			log.Printf("D! Output [amqp] connected to %q", broker)
			break
		}
		log.Printf("D! Output [amqp] error connecting to %q - %s", broker, err.Error())
	}

	if client.conn == nil {
		return nil, errors.New("could not connect to any broker")
	}

	channel, err := client.conn.Channel()
	if err != nil {
		return nil, fmt.Errorf("error opening channel: %v", err)
	}
	client.channel = channel

	err = client.DeclareExchange()
	if err != nil {
		return nil, err
	}

	return client, nil
}