ifconfig retry

This commit is contained in:
hhd
2021-12-01 18:00:23 +08:00
parent a9250737f1
commit 8353c1c3dd
3 changed files with 27 additions and 20 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
[English](/README.md)|中文
[English](/README.md)|中文
网站: [openp2p.cn](https://openp2p.cn)
## OpenP2P是什么
它是一个开源、免费、轻量级的P2P共享网络。任何设备接入OpenP2P,随时随地访问它们。
我们的目标是:充分利用带宽,利用共享节点转发数据,建设一个远程连接的通用基础设施。
+2 -1
View File
@@ -1,4 +1,5 @@
English|[中文](/README-ZH.md)
English|[中文](/README-ZH.md)
Website: [openp2p.cn](https://openp2p.cn)
## What is OpenP2P
It is an open source, free, and lightweight P2P sharing network. As long as any device joins in, you can access them anywhere.
Our goal is to make full use of bandwidth, use shared nodes to relay data, and build a common infrastructure for remote connections.
+23 -18
View File
@@ -113,23 +113,28 @@ func netInfo() *NetInfo {
// return d.DialContext(ctx, "tcp6", addr)
// },
}
client := &http.Client{Transport: tr, Timeout: time.Second * 5}
r, err := client.Get("https://ifconfig.co/json")
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
return nil
// sometime will be failed, retry
for i := 0; i < 2; i++ {
client := &http.Client{Transport: tr, Timeout: time.Second * 10}
r, err := client.Get("https://ifconfig.co/json")
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
continue
}
defer r.Body.Close()
buf := make([]byte, 1024*64)
n, err := r.Body.Read(buf)
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
continue
}
rsp := NetInfo{}
err = json.Unmarshal(buf[:n], &rsp)
if err != nil {
gLog.Printf(LevelERROR, "wrong NetInfo:%s", err)
continue
}
return &rsp
}
defer r.Body.Close()
buf := make([]byte, 1024*64)
n, err := r.Body.Read(buf)
if err != nil {
gLog.Println(LevelINFO, "netInfo error:", err)
return nil
}
rsp := NetInfo{}
err = json.Unmarshal(buf[:n], &rsp)
if err != nil {
gLog.Printf(LevelERROR, "wrong NetInfo:%s", err)
}
return &rsp
return nil
}