Browse Source

add timeout for http requests

Matthew R. Kasun 3 years ago
parent
commit
a17173fa0f
1 changed files with 7 additions and 1 deletions
  1. 7 1
      netclient/functions/common.go

+ 7 - 1
netclient/functions/common.go

@@ -11,6 +11,7 @@ import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"strings"
 	"strings"
+	"time"
 
 
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
@@ -25,6 +26,9 @@ import (
 // LINUX_APP_DATA_PATH - linux path
 // LINUX_APP_DATA_PATH - linux path
 const LINUX_APP_DATA_PATH = "/etc/netmaker"
 const LINUX_APP_DATA_PATH = "/etc/netmaker"
 
 
+// HTTP_TIMEOUT - timeout in seconds for http requests
+const HTTP_TIMEOUT = 30
+
 // ListPorts - lists ports of WireGuard devices
 // ListPorts - lists ports of WireGuard devices
 func ListPorts() error {
 func ListPorts() error {
 	wgclient, err := wgctrl.New()
 	wgclient, err := wgctrl.New()
@@ -339,7 +343,9 @@ func API(data any, method, url, authorization string) (*http.Response, error) {
 	if authorization != "" {
 	if authorization != "" {
 		request.Header.Set("authorization", "Bearer "+authorization)
 		request.Header.Set("authorization", "Bearer "+authorization)
 	}
 	}
-	client := http.Client{}
+	client := http.Client{
+		Timeout: HTTP_TIMEOUT * time.Second,
+	}
 	return client.Do(request)
 	return client.Do(request)
 }
 }