Esempio n. 1
0
func (p *peer) connect() error {
	var dialer net.Dialer
	var backoff = backoff{Max: 10 * time.Second}
	for {
		dialer.Deadline = time.Now().Add(3 * time.Second)

		if p.sock.debug {
			println("Connecting", p.network, p.address, dialer.Deadline.String())
		}
		conn, err := dialer.Dial(p.network, p.address)
		if err != nil {
			backoff.Wait()
			continue
		}

		p.conn = conn
		if p.sock.debug {
			fmt.Printf("\nConnected %s (%s) <-> %s (%s)..\n",
				conn.LocalAddr(), p.sock.ident,
				conn.RemoteAddr(), p.ident)
		}
		break
	}
	return nil
}
Esempio n. 2
0
func (d *Dialer) dialSerial(network, addr string, ips []net.IP, port string) (net.Conn, error) {

	dialer := net.Dialer{
		Timeout:   d.Timeout,
		Deadline:  d.Deadline,
		KeepAlive: d.KeepAlive,
		LocalAddr: d.LocalAddr,
	}

	// Ensure the deadline is set when a timeout is set, so that the total
	// amount of time used does not exceed the timeout even when multiple
	// requests are made.
	if dialer.Timeout > 0 {
		newDeadline := time.Now().Add(dialer.Timeout)
		if dialer.Deadline.IsZero() || newDeadline.Before(d.Deadline) {
			dialer.Deadline = newDeadline
		}
	}

	// Ensure that the timeout for each operation is small enough that
	// if connecting to the first address times out, the other addresses
	// will be tried.
	if !dialer.Deadline.IsZero() {
		totalTime := dialer.Deadline.Sub(time.Now())
		newTimeout := totalTime / time.Duration(len(ips))

		if newTimeout < 2*time.Second {
			newTimeout = 2 * time.Second
		}

		if dialer.Timeout == 0 || newTimeout < dialer.Timeout {
			dialer.Timeout = newTimeout
		}

	}

	var firstErr error
	for _, ip := range ips {
		conn, err := dialer.Dial(network, net.JoinHostPort(ip.String(), port))
		if err != nil {
			if firstErr == nil {
				firstErr = err
			}
			continue
		}
		return conn, nil
	}

	if firstErr == nil {
		firstErr = fmt.Errorf("dialer.Dial no IP addresses found: %s", addr)
	}

	return nil, firstErr
}
Esempio n. 3
0
// NewForwarder creates a new unbuffered forwarder for sending points to carbon
func NewForwarder(host string, port uint16, timeout time.Duration, dimensionOrder []string, drainingThreads uint32) (*Forwarder, error) {
	connectionAddress := net.JoinHostPort(host, strconv.FormatUint(uint64(port), 10))
	var d net.Dialer
	d.Deadline = time.Now().Add(timeout)
	conn, err := d.Dial("tcp", connectionAddress)
	if err != nil {
		return nil, err
	}
	ret := &Forwarder{
		dimensionComparor: dpdimsort.NewOrdering(dimensionOrder),
		connectionTimeout: timeout,
		connectionAddress: connectionAddress,
		dialer:            net.DialTimeout,
		pool: connPool{
			conns: make([]net.Conn, 0, drainingThreads),
		},
	}
	ret.pool.Return(conn)
	return ret, nil
}
Esempio n. 4
0
func connectTCP(node *toxNode, port int) (*net.TCPConn, error) {
	ip, err := getNodeIP(node)
	if err != nil {
		return nil, err
	}

	dialer := net.Dialer{}
	dialer.Deadline = time.Now().Add(2 * time.Second)

	tempConn, err := dialer.Dial("tcp", fmt.Sprintf("%s:%d", ip, port))
	if err != nil {
		return nil, err
	}

	conn, ok := tempConn.(*net.TCPConn)
	if !ok {
		return nil, errors.New("not a tcp conn")
	}

	return conn, nil
}