67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
|
|
package helper
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"log"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func CheckDBExists(endpoint string) bool {
|
||
|
|
|
||
|
|
const ConnectMaxWaitTime = 30 * time.Second
|
||
|
|
const RequestMaxWaitTime = 60 * time.Second
|
||
|
|
|
||
|
|
client := http.Client{
|
||
|
|
Transport: &http.Transport{
|
||
|
|
DialContext: (&net.Dialer{
|
||
|
|
Timeout: ConnectMaxWaitTime,
|
||
|
|
}).DialContext,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
ctx, cancel := context.WithTimeout(context.Background(), RequestMaxWaitTime)
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
||
|
|
req.Proto = "HTTP/1.0"
|
||
|
|
if err != nil {
|
||
|
|
log.Println(err.Error())
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
q := req.URL.Query()
|
||
|
|
req.URL.RawQuery = q.Encode()
|
||
|
|
req.Header.Set("Connection", "close")
|
||
|
|
req.Close = true
|
||
|
|
client.CloseIdleConnections()
|
||
|
|
|
||
|
|
response, err := client.Do(req)
|
||
|
|
|
||
|
|
// handle timeout
|
||
|
|
if e, ok := err.(net.Error); ok && e.Timeout() {
|
||
|
|
if response != nil {
|
||
|
|
response.Body.Close()
|
||
|
|
}
|
||
|
|
log.Println(err.Error())
|
||
|
|
return false
|
||
|
|
} else if err != nil {
|
||
|
|
if response != nil {
|
||
|
|
response.Body.Close()
|
||
|
|
}
|
||
|
|
log.Println(err.Error())
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
if response != nil {
|
||
|
|
response.Body.Close()
|
||
|
|
}
|
||
|
|
|
||
|
|
if response.StatusCode == http.StatusOK {
|
||
|
|
return true
|
||
|
|
} else {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|