Skip to content

Commit

Permalink
use another module for creating http client
Browse files Browse the repository at this point in the history
  • Loading branch information
Piszmog committed Sep 5, 2018
1 parent 251a286 commit 4f37227
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 41 deletions.
4 changes: 2 additions & 2 deletions client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package client

import (
"github.com/Piszmog/cfservices"
"github.com/Piszmog/cloudconfigclient/net"
"github.com/Piszmog/httpclient"
"github.com/pkg/errors"
"os"
"strings"
Expand All @@ -24,7 +24,7 @@ func CreateLocalClient() (*ConfigClient, error) {
configClients := make([]CloudClient, len(serviceCredentials.Credentials))
for index, cred := range serviceCredentials.Credentials {
configUri := cred.Uri
client := net.CreateDefaultHttpClient()
client := httpclient.CreateDefaultHttpClient()
configClients[index] = Client{configUri: configUri, httpClient: client}
}
return &ConfigClient{Clients: configClients}, nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/Piszmog/cloudconfigclient

require (
github.com/Piszmog/cfservices v1.3.1
github.com/Piszmog/httpclient v1.0.1
github.com/golang/protobuf v1.1.0 // indirect
github.com/pkg/errors v0.8.0
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Piszmog/cfservices v1.3.1 h1:xiX4kjYJUxJYUBDgUJ+OgdqUsLX48yFq8YMZs/gdGyE=
github.com/Piszmog/cfservices v1.3.1/go.mod h1:uxLe5gKNnODE9Bt1aJd5WjaeTg8/Qr6Dibd1wFgfAwk=
github.com/Piszmog/httpclient v1.0.1 h1:LBgUxwb2zrwv/c1qx0D4lSwmz28ZNf7k91EsNbclJk0=
github.com/Piszmog/httpclient v1.0.1/go.mod h1:FcqjAm4HhYGYfQaFY8kZ2Y+SGiO51s4H3K6GEtAHOn4=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 h1:mEsFm194MmS9vCwxFy+zwu0EU7ZkxxMD1iH++vmGdUY=
Expand Down
32 changes: 0 additions & 32 deletions net/net.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net

import (
"net"
"net/http"
"strings"
"time"
)

// CreateUrl creates a full URL from the specified base URL and the array of URI variables.
Expand All @@ -22,32 +19,3 @@ func CreateUrl(baseUrl string, uriVariables ...string) string {
func JoinProfiles(profiles []string) string {
return strings.Join(profiles, ",")
}

// CreateDefaultHttpClient creates a default http.Client.
//
// Timeout set to 5 seconds, keep alive set to 30 seconds, TLS handshake timeout set to 5 seconds, and idleConnection set to
// 90 seconds.
func CreateDefaultHttpClient() *http.Client {
return CreateHttpClient(5*time.Second, 30*time.Second, 5*time.Second, 90*time.Second)
}

// CreateHttpClient creates a http.Client from the specified timeouts and keep alive.
//
// The client also has the maximum number of idle connections set to 100 and number of connections per host as 100.
func CreateHttpClient(timeout time.Duration, keepAlive time.Duration, tlsHandshakeTimeout time.Duration, idleConnection time.Duration) *http.Client {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: keepAlive,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: tlsHandshakeTimeout,
IdleConnTimeout: idleConnection,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
}
return &http.Client{
Transport: transport,
Timeout: timeout,
}
}
7 changes: 0 additions & 7 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ func TestJoinProfiles(t *testing.T) {
t.Errorf("profiles were not append")
}
}

func TestCreateDefaultHttpClient(t *testing.T) {
client := CreateDefaultHttpClient()
if client == nil {
t.Errorf("failed to create configuration client")
}
}

0 comments on commit 4f37227

Please sign in to comment.