本文整理汇总了Golang中net.Dialer.Deadline方法的典型用法代码示例。如果您正苦于以下问题:Golang Dialer.Deadline方法的具体用法?Golang Dialer.Deadline怎么用?Golang Dialer.Deadline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.Dialer
的用法示例。
在下文中一共展示了Dialer.Deadline方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: connect
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
}
示例2: dialSerial
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
}
示例3: NewForwarder
// 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
}
示例4: connectTCP
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
}