github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/net/http/client.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package xhttp
    22  
    23  import (
    24  	"net"
    25  	"net/http"
    26  	"net/url"
    27  	"time"
    28  )
    29  
    30  // ProxyFunc is a type alias for the proxy function used by the standard
    31  // library's http.Transport struct.
    32  type ProxyFunc func(*http.Request) (*url.URL, error)
    33  
    34  // HTTPClientOptions specify HTTP Client options.
    35  type HTTPClientOptions struct {
    36  	RequestTimeout     time.Duration `yaml:"requestTimeout"`
    37  	ConnectTimeout     time.Duration `yaml:"connectTimeout"`
    38  	KeepAlive          time.Duration `yaml:"keepAlive"`
    39  	IdleConnTimeout    time.Duration `yaml:"idleConnTimeout"`
    40  	MaxIdleConns       int           `yaml:"maxIdleConns"`
    41  	DisableCompression bool          `yaml:"disableCompression"`
    42  	Proxy              ProxyFunc     `yaml:"proxy"`
    43  }
    44  
    45  // NewHTTPClient constructs a new HTTP Client.
    46  func NewHTTPClient(o HTTPClientOptions) *http.Client {
    47  	// Before we added the Proxy option, we unconditionally set the field in
    48  	// the http.Transport we construct below to http.ProxyFromEnvironment. To
    49  	// keep that behavior unchanged now that we added the field, we need to
    50  	// set the option if it is nil.
    51  	if o.Proxy == nil {
    52  		o.Proxy = http.ProxyFromEnvironment
    53  	}
    54  
    55  	return &http.Client{
    56  		Timeout: o.RequestTimeout,
    57  		Transport: &http.Transport{
    58  			Proxy: o.Proxy,
    59  			Dial: (&net.Dialer{
    60  				Timeout:   o.ConnectTimeout,
    61  				KeepAlive: o.KeepAlive,
    62  				DualStack: true,
    63  			}).Dial,
    64  			TLSHandshakeTimeout:   o.ConnectTimeout,
    65  			ExpectContinueTimeout: o.ConnectTimeout,
    66  			MaxIdleConns:          o.MaxIdleConns,
    67  			MaxIdleConnsPerHost:   o.MaxIdleConns,
    68  			DisableCompression:    o.DisableCompression,
    69  		},
    70  	}
    71  }
    72  
    73  // DefaultHTTPClientOptions returns default options.
    74  func DefaultHTTPClientOptions() HTTPClientOptions {
    75  	return HTTPClientOptions{
    76  		RequestTimeout:  60 * time.Second,
    77  		ConnectTimeout:  5 * time.Second,
    78  		KeepAlive:       60 * time.Second,
    79  		IdleConnTimeout: 60 * time.Second,
    80  		MaxIdleConns:    100,
    81  		// DisableCompression is true by default since we have seen
    82  		// a large amount of overhead with compression.
    83  		DisableCompression: true,
    84  		Proxy:              ProxyFunc(http.ProxyFromEnvironment),
    85  	}
    86  }