Skip to content

Commit

Permalink
refactor: remove deprecated httpcontrol (#3410)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Oct 8, 2018
1 parent 71a11cb commit 29c6e42
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 85 deletions.
13 changes: 2 additions & 11 deletions contrib/grpcplugins/action/marathon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"path"
Expand All @@ -16,12 +14,12 @@ import (
"sync"
"time"

"github.com/facebookgo/httpcontrol"
marathon "github.com/gambol99/go-marathon"
"github.com/golang/protobuf/ptypes/empty"
"github.com/xeipuuv/gojsonschema"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/grpcplugin/actionplugin"
"github.com/ovh/cds/sdk/interpolate"
)
Expand Down Expand Up @@ -80,14 +78,7 @@ func (actPlugin *marathonActionPlugin) Run(ctx context.Context, q *actionplugin.
return fail("Error parsing timeout value : %s\n", err.Error())
}

//Custom http client with 3 retries
httpClient := &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Minute,
MaxTries: 3,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
},
}
httpClient := cdsclient.NewHTTPClient(time.Minute, insecureSkipVerify)

fmt.Printf("Connecting on %s\n", URL)
config := marathon.NewDefaultConfig()
Expand Down
13 changes: 1 addition & 12 deletions engine/hatchery/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package marathon
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"html/template"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/facebookgo/httpcontrol"
"github.com/gambol99/go-marathon"
"github.com/gorilla/mux"

Expand Down Expand Up @@ -113,15 +110,7 @@ func (h *HatcheryMarathon) CheckConfiguration(cfg interface{}) error {
}
}

//Custom http client with 3 retries
httpClient := &http.Client{
Timeout: time.Minute,
Transport: &httpcontrol.Transport{
RequestTimeout: time.Minute,
MaxTries: 3,
TLSClientConfig: &tls.Config{InsecureSkipVerify: hconfig.API.HTTP.Insecure},
},
}
httpClient := cdsclient.NewHTTPClient(time.Minute, hconfig.API.HTTP.Insecure)

config := marathon.NewDefaultConfig()
config.URL = hconfig.MarathonURL
Expand Down
10 changes: 2 additions & 8 deletions engine/vcs/bitbucket/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ import (
"strings"
"time"

"github.com/facebookgo/httpcontrol"

"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/log"
)

var (
httpClient = &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Second * 30,
MaxTries: 5,
},
}
httpClient = cdsclient.NewHTTPClient(time.Second*30, false)
)

func requestString(method string, uri string, params map[string]string) string {
Expand Down
9 changes: 2 additions & 7 deletions engine/vcs/github/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"time"

"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/log"

"github.com/facebookgo/httpcontrol"
"github.com/ovh/cds/sdk"
)

Expand All @@ -25,12 +25,7 @@ var (
RateLimitRemaining = 5000
RateLimitReset int

httpClient = &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Second * 30,
MaxTries: 5,
},
}
httpClient = cdsclient.NewHTTPClient(time.Second*30, false)
)

func (g *githubConsumer) postForm(path string, data url.Values, headers map[string][]string) (int, []byte, error) {
Expand Down
10 changes: 1 addition & 9 deletions engine/worker/cmd_update.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"crypto/tls"
"fmt"
"net/http"
"time"

"github.com/facebookgo/httpcontrol"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -45,13 +43,7 @@ func updateCmd(w *currentWorker) func(cmd *cobra.Command, args []string) {
if w.apiEndpoint == "" {
sdk.Exit("--api not provided, aborting update.")
}
w.client = cdsclient.NewWorker(w.apiEndpoint, "download", &http.Client{
Timeout: time.Second * 360,
Transport: &httpcontrol.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: FlagBool(cmd, flagInsecure)},
},
})

w.client = cdsclient.NewWorker(w.apiEndpoint, "download", cdsclient.NewHTTPClient(time.Second*360, FlagBool(cmd, flagInsecure)))
urlBinary = w.client.DownloadURLFromAPI("worker", sdk.GOOS, sdk.GOARCH)
fmt.Printf("Updating worker binary from CDS API on %s...\n", urlBinary)
} else {
Expand Down
10 changes: 1 addition & 9 deletions engine/worker/init.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package main

import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/facebookgo/httpcontrol"
"golang.org/x/net/context"
"google.golang.org/grpc"

Expand Down Expand Up @@ -196,12 +193,7 @@ func initFlags(cmd *cobra.Command, w *currentWorker) {
w.bookedPBJobID = FlagInt64(cmd, flagBookedPBJobID)
w.bookedWJobID = FlagInt64(cmd, flagBookedWorkflowJobID)

w.client = cdsclient.NewWorker(w.apiEndpoint, w.status.Name, &http.Client{
Timeout: time.Second * 360,
Transport: &httpcontrol.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: FlagBool(cmd, flagInsecure)},
},
})
w.client = cdsclient.NewWorker(w.apiEndpoint, w.status.Name, cdsclient.NewHTTPClient(time.Second*360, FlagBool(cmd, flagInsecure)))

w.autoUpdate = FlagBool(cmd, flagAutoUpdate)
w.singleUse = FlagBool(cmd, flagSingleUse)
Expand Down
48 changes: 19 additions & 29 deletions sdk/cdsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os"
"time"

"github.com/facebookgo/httpcontrol"

"github.com/ovh/cds/sdk"
)

Expand All @@ -23,17 +21,26 @@ type client struct {
service *sdk.Service
}

// NewHTTPClient returns a new HTTP Client
func NewHTTPClient(timeout time.Duration, insecureSkipVerifyTLS bool) *http.Client {
return &http.Client{
Timeout: timeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerifyTLS},
},
}
}

// New returns a client from a config struct
func New(c Config) Interface {
cli := new(client)
cli.config = c
cli.HTTPClient = &http.Client{
Timeout: time.Second * 60,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerifyTLS},
},
}
cli.HTTPClient = NewHTTPClient(time.Second*60, c.InsecureSkipVerifyTLS)
cli.init()
return cli
}
Expand All @@ -47,18 +54,7 @@ func NewService(endpoint string, timeout time.Duration, insecureSkipVerifyTLS bo
}
cli := new(client)
cli.config = conf
cli.HTTPClient = &http.Client{
Timeout: timeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
MaxIdleConns: 500,
MaxIdleConnsPerHost: 500,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.InsecureSkipVerifyTLS},
},
}
cli.HTTPClient = NewHTTPClient(timeout, conf.InsecureSkipVerifyTLS)
cli.isService = true
cli.init()
return cli
Expand All @@ -74,7 +70,7 @@ func NewWorker(endpoint string, name string, c HTTPClient) Interface {
cli.config = conf

if c == nil {
cli.HTTPClient = &http.Client{Timeout: time.Second * 360}
cli.HTTPClient = NewHTTPClient(time.Second*360, false)
} else {
cli.HTTPClient = c
}
Expand Down Expand Up @@ -110,13 +106,7 @@ func NewProviderClient(cfg ProviderConfig) ProviderClient {

cli := new(client)
cli.config = conf
cli.HTTPClient = &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Duration(cfg.RequestSecondsTimeout) * time.Second,
MaxTries: 5,
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerifyTLS},
},
}
cli.HTTPClient = NewHTTPClient(time.Duration(cfg.RequestSecondsTimeout)*time.Second, conf.InsecureSkipVerifyTLS)
cli.isProvider = true
cli.name = cfg.Name
cli.init()
Expand Down

0 comments on commit 29c6e42

Please sign in to comment.