add timeout to http.get

This commit is contained in:
Matthew R. Kasun 2022-05-13 06:04:27 -04:00
parent 4f0f3a1c15
commit c8ec507484
3 changed files with 14 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"time"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
@ -99,7 +100,10 @@ func getGoogleUserInfo(state string, code string) (*googleOauthUser, error) {
if err != nil {
return nil, fmt.Errorf("failed to convert token to json: %s", err.Error())
}
response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
client := &http.Client{
Timeout: time.Second * 30,
}
response, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
if err != nil {
return nil, fmt.Errorf("failed getting user info: %s", err.Error())
}

View file

@ -130,7 +130,10 @@ func GetPublicIP() (string, error) {
endpoint := ""
var err error
for _, ipserver := range iplist {
resp, err := http.Get(ipserver)
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Get(ipserver)
if err != nil {
continue
}

View file

@ -7,6 +7,7 @@ import (
"os"
"strconv"
"strings"
"time"
"github.com/gravitl/netmaker/config"
)
@ -411,7 +412,10 @@ func GetPublicIP() (string, error) {
iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
for _, ipserver := range iplist {
resp, err := http.Get(ipserver)
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Get(ipserver)
if err != nil {
continue
}