Browse Source

single http client

Matthew R. Kasun 3 years ago
parent
commit
d83be55d70
3 changed files with 18 additions and 9 deletions
  1. 12 5
      netclient/functions/common.go
  2. 4 4
      netclient/functions/join.go
  3. 2 0
      netclient/main.go

+ 12 - 5
netclient/functions/common.go

@@ -30,6 +30,16 @@ const LINUX_APP_DATA_PATH = "/etc/netmaker"
 // HTTP_TIMEOUT - timeout in seconds for http requests
 // HTTP_TIMEOUT - timeout in seconds for http requests
 const HTTP_TIMEOUT = 30
 const HTTP_TIMEOUT = 30
 
 
+// HTTPClient - http client to be reused by all
+var HTTPClient http.Client
+
+// SetHTTPClient -sets http client with sane default
+func SetHTTPClient() {
+	HTTPClient = http.Client{
+		Timeout: HTTP_TIMEOUT * time.Second,
+	}
+}
+
 // 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()
@@ -308,7 +318,7 @@ func GetNetmakerPath() string {
 	return LINUX_APP_DATA_PATH
 	return LINUX_APP_DATA_PATH
 }
 }
 
 
-//API function to interact with netmaker api endpoints. response from endpoint is returned
+// API function to interact with netmaker api endpoints. response from endpoint is returned
 func API(data any, method, url, authorization string) (*http.Response, error) {
 func API(data any, method, url, authorization string) (*http.Response, error) {
 	var request *http.Request
 	var request *http.Request
 	var err error
 	var err error
@@ -331,10 +341,7 @@ 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{
-		Timeout: HTTP_TIMEOUT * time.Second,
-	}
-	return client.Do(request)
+	return HTTPClient.Do(request)
 }
 }
 
 
 // Authenticate authenticates with api to permit subsequent interactions with the api
 // Authenticate authenticates with api to permit subsequent interactions with the api

+ 4 - 4
netclient/functions/join.go

@@ -205,16 +205,16 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error {
 	if err != nil {
 	if err != nil {
 		logger.Log(0, "error setting route for netmaker: "+err.Error())
 		logger.Log(0, "error setting route for netmaker: "+err.Error())
 	}
 	}
+	cfg.Node = node
+	if err := Register(cfg); err != nil {
+		return err
+	}
 
 
 	logger.Log(0, "starting wireguard")
 	logger.Log(0, "starting wireguard")
 	err = wireguard.InitWireguard(&node, privateKey, nodeGET.Peers[:], false)
 	err = wireguard.InitWireguard(&node, privateKey, nodeGET.Peers[:], false)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	cfg.Node = node
-	if err := Register(cfg); err != nil {
-		return err
-	}
 	if cfg.Server.Server == "" {
 	if cfg.Server.Server == "" {
 		return errors.New("did not receive broker address from registration")
 		return errors.New("did not receive broker address from registration")
 	}
 	}

+ 2 - 0
netclient/main.go

@@ -10,6 +10,7 @@ import (
 
 
 	"github.com/gravitl/netmaker/netclient/cli_options"
 	"github.com/gravitl/netmaker/netclient/cli_options"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/config"
+	"github.com/gravitl/netmaker/netclient/functions"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncwindows"
 	"github.com/gravitl/netmaker/netclient/ncwindows"
 	"github.com/urfave/cli/v2"
 	"github.com/urfave/cli/v2"
@@ -29,6 +30,7 @@ func main() {
 	app.UsageText = "netclient [global options] command [command options] [arguments...]. Adjust verbosity of given command with -v, -vv or -vvv (max)."
 	app.UsageText = "netclient [global options] command [command options] [arguments...]. Adjust verbosity of given command with -v, -vv or -vvv (max)."
 
 
 	setGarbageCollection()
 	setGarbageCollection()
+	functions.SetHTTPClient()
 
 
 	if ncutils.IsWindows() {
 	if ncutils.IsWindows() {
 		ncwindows.InitWindows()
 		ncwindows.InitWindows()