5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-11-09 16:00:27 +00:00

retry requests a few times before failing

This commit is contained in:
Arceliar 2021-07-06 19:12:06 -05:00
parent b744a6ab1a
commit 7b8b0e0ba6

View File

@ -12,6 +12,8 @@ var waitgroup sync.WaitGroup
var visited sync.Map var visited sync.Map
var rumored sync.Map var rumored sync.Map
const MAX_RETRY = 3
func dial() (net.Conn, error) { func dial() (net.Conn, error) {
return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second) return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second)
} }
@ -29,22 +31,31 @@ func doRequest(request map[string]interface{}) map[string]interface{} {
if err != nil { if err != nil {
panic(err) panic(err)
} }
sock, err := dial()
if err != nil {
panic(err)
}
if _, err = sock.Write(req); err != nil {
panic(err)
}
bs := make([]byte, 65535)
n, err := sock.Read(bs)
if err != nil {
panic(bs)
}
bs = bs[:n]
var res map[string]interface{} var res map[string]interface{}
if err = json.Unmarshal(bs, &res); err != nil { for idx := 0; idx < MAX_RETRY; idx++ {
panic(err) sock, err := dial()
if err != nil {
panic(err)
}
if _, err = sock.Write(req); err != nil {
panic(err)
}
bs := make([]byte, 65535)
n, err := sock.Read(bs)
if err != nil {
panic(bs)
}
bs = bs[:n]
if err = json.Unmarshal(bs, &res); err != nil {
panic(err)
}
// TODO parse res, check if there's an error
if res, ok := res["response"]; ok {
if _, isIn := res.(map[string]interface{})["error"]; isIn {
continue
}
}
break
} }
return res return res
} }