Client端请求延迟处理 -golang

package main

import (
    "fmt"
    "net/http"
    "time"
    "net"
)

var url = []string{
    "http://www.baidu.com",
    "http://google.com",
    "http://taobao.com",
}

func main()  {
    
    for _, v := range url {
        c := http.Client{
            Transport: &http.Transport{
                Dial: func (network, addr string)(net.Conn, error)  {
                    timeout := time.Second*2
                    return net.DialTimeout(network, addr, timeout)
                },
            },
        }   
        resp, err := c.Head(v)
        if err != nil {
            fmt.Printf("head %s failed, err: %v\n", v, err)
            continue
        }
        fmt.Printf("head succ, status: %v\n", resp.Status)
    } 
}

output

head succ, status: 200 OK
head http://google.com failed, err: Head http://google.com: dial tcp [2404:6800:4008:802::200e]:80: i/o timeout
head succ, status: 200 OK

你可能感兴趣的:(Client端请求延迟处理 -golang)