Skip to content

Commit

Permalink
chore: adjust http client
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriBeck committed May 16, 2024
1 parent 7f6ced5 commit f3b4342
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
30 changes: 14 additions & 16 deletions http/connection_pool.go → http/client.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package goload_http

import (
"math/rand"
"net/http"
)

type ClientPool struct {
clients []*http.Client
rand *rand.Rand
import "net/http"

var defaultClient = http.DefaultClient

func SetHTTPClient(client *http.Client) {
defaultClient = client
}

type HTTPTransportOption = func(request *http.Request)

// WithUserAgent sets the user agent on each of the request for the client pool.
func WithUserAgent(userAgent string) HTTPTransportOption {
// WithClientUserAgent sets the user agent on each of the request
func WithClientUserAgent(userAgent string) HTTPTransportOption {
return func(request *http.Request) {
if request.Header.Get("user-agent") == "" {
request.Header.Set("user-agent", userAgent)
}
}
}

// WithHeader sets a custom header on each request.
func WithHeader(key string, value string) HTTPTransportOption {
// WithClientHeader sets a custom header on each request.
func WithClientHeader(key string, value string) HTTPTransportOption {
return func(request *http.Request) {
request.Header.Add(key, value)
}
}

// WithBasicAuth sets basic auth credentials on each request.
func WithBasicAuth(username string, password string) HTTPTransportOption {
// WithClientBasicAuth sets basic auth credentials on each request.
func WithClientBasicAuth(username string, password string) HTTPTransportOption {
return func(request *http.Request) {
request.SetBasicAuth(username, password)
}
Expand All @@ -49,10 +47,10 @@ func (transport *transport) RoundTrip(request *http.Request) (*http.Response, er
return transport.innerTransport.RoundTrip(request)
}

// NewHTTPClient creates a new http client for loadtesting.
// NewClient creates a new http client for loadtesting with custom options.
//
// It allows to specify various request options which will be applied to all requests.
func NewHTTPClient(options ...HTTPTransportOption) *http.Client {
func NewClient(options ...HTTPTransportOption) *http.Client {
return &http.Client{
Transport: &transport{
options: options,
Expand Down
13 changes: 11 additions & 2 deletions http/http_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type EndpointOptions struct {
getUrl func() url.URL
method string
body io.Reader
header http.Header

validateResponse func(response *http.Response) error
}
Expand All @@ -38,7 +39,7 @@ func (options *EndpointOptions) Name() string {

type HTTPEndpointOption func(options *EndpointOptions)

// WithHTTPClient configures a static http client to be used for the loadtest endpoint.
// WithHTTPClient configures a static http defaultClient to be used for the loadtest endpoint.
func WithHTTPClient(client *http.Client) HTTPEndpointOption {
return func(options *EndpointOptions) {
options.client = client
Expand All @@ -54,6 +55,12 @@ func WithHTTPMethod(method string) HTTPEndpointOption {
}
}

func WithHeader(key string, value string) HTTPEndpointOption {
return func(options *EndpointOptions) {
options.header.Add(key, value)
}
}

// WithRequestsPerMinute configures the targeted requests per minute compared to other endpoints.
func WithRequestsPerMinute(rpm int32) HTTPEndpointOption {
return func(options *EndpointOptions) {
Expand Down Expand Up @@ -117,7 +124,7 @@ func NewEndpoint(opts ...HTTPEndpointOption) goload.Endpoint {
options := &EndpointOptions{
method: http.MethodGet,
body: http.NoBody,
client: http.DefaultClient,
client: defaultClient,
validateResponse: func(res *http.Response) error {
if res.StatusCode < 200 || res.StatusCode > 299 {
return fmt.Errorf("received non 200 status code from the server")
Expand All @@ -140,6 +147,8 @@ func NewEndpoint(opts ...HTTPEndpointOption) goload.Endpoint {
return err
}

req.Header = options.header

res, err := options.client.Do(req)
if err != nil {
return err
Expand Down

0 comments on commit f3b4342

Please sign in to comment.