5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-11-24 12:11:35 +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 rumored sync.Map
const MAX_RETRY = 3
func dial() (net.Conn, error) {
return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second)
}
@ -29,6 +31,8 @@ func doRequest(request map[string]interface{}) map[string]interface{} {
if err != nil {
panic(err)
}
var res map[string]interface{}
for idx := 0; idx < MAX_RETRY; idx++ {
sock, err := dial()
if err != nil {
panic(err)
@ -42,10 +46,17 @@ func doRequest(request map[string]interface{}) map[string]interface{} {
panic(bs)
}
bs = bs[:n]
var res map[string]interface{}
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
}