Documentation ¶
Index ¶
- Constants
- Variables
- func DrainBody(r *http.Request)
- func GetFreePort() (port int, err error)
- type Client
- func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, url string) (*http.Response, error)
- func (c *Client) GetMaxRetries() int
- func (c *Client) GetPathsWithNoRetries() (paths []string)
- func (c *Client) Head(ctx context.Context, url string) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
- func (c *Client) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)
- func (c *Client) Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
- func (c *Client) RoundTrip(req *http.Request) (*http.Response, error)
- func (c *Client) SetMaxRetries(maxRetries int)
- func (c *Client) SetPathsWithNoRetries(paths []string)
- func (c *Client) SetTimeout(timeout time.Duration)
- func (c *Client) SetTotalTimeout(timeout time.Duration)
- type Clienter
- func ClientWithListOfNonRetriablePaths(c Clienter, paths []string) Clienter
- func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter
- func ClientWithTimeouts(c Clienter, perRequest time.Duration, total time.Duration) Clienter
- func ClientWithTotalTimeout(c Clienter, timeout time.Duration) Clienter
- func NewClient() Clienter
- func NewClientWithTransport(transport http.RoundTripper) Clienter
- type ClienterMock
- func (mock *ClienterMock) Do(ctx context.Context, req *http.Request) (*http.Response, error)
- func (mock *ClienterMock) DoCalls() []struct{ ... }
- func (mock *ClienterMock) Get(ctx context.Context, url string) (*http.Response, error)
- func (mock *ClienterMock) GetCalls() []struct{ ... }
- func (mock *ClienterMock) GetMaxRetries() int
- func (mock *ClienterMock) GetMaxRetriesCalls() []struct{}
- func (mock *ClienterMock) GetPathsWithNoRetries() []string
- func (mock *ClienterMock) GetPathsWithNoRetriesCalls() []struct{}
- func (mock *ClienterMock) Head(ctx context.Context, url string) (*http.Response, error)
- func (mock *ClienterMock) HeadCalls() []struct{ ... }
- func (mock *ClienterMock) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
- func (mock *ClienterMock) PostCalls() []struct{ ... }
- func (mock *ClienterMock) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)
- func (mock *ClienterMock) PostFormCalls() []struct{ ... }
- func (mock *ClienterMock) Put(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error)
- func (mock *ClienterMock) PutCalls() []struct{ ... }
- func (mock *ClienterMock) RoundTrip(req *http.Request) (*http.Response, error)
- func (mock *ClienterMock) RoundTripCalls() []struct{ ... }
- func (mock *ClienterMock) SetMaxRetries(n int)
- func (mock *ClienterMock) SetMaxRetriesCalls() []struct{ ... }
- func (mock *ClienterMock) SetPathsWithNoRetries(strings []string)
- func (mock *ClienterMock) SetPathsWithNoRetriesCalls() []struct{ ... }
- func (mock *ClienterMock) SetTimeout(timeout time.Duration)
- func (mock *ClienterMock) SetTimeoutCalls() []struct{ ... }
- func (mock *ClienterMock) SetTotalTimeout(timeout time.Duration)
- func (mock *ClienterMock) SetTotalTimeoutCalls() []struct{ ... }
- type Doer
- type Server
Constants ¶
const ( RequestIDHandlerKey string = "RequestID" LogHandlerKey string = "Log" ResponseWriteGrace time.Duration = 100 * time.Millisecond )
const (
DefaultRequestTimeout = 10 * time.Second
)
Variables ¶
var DefaultClient = &Client{ MaxRetries: 3, RetryTime: 20 * time.Millisecond, HTTPClient: &http.Client{ Timeout: 10 * time.Second, Transport: DefaultTransport, }, }
DefaultClient is a dp-net specific http client with sensible timeouts, exponential backoff, and a contextual dialer. NOTE: This is needed in dp-deployer's unit tests
var DefaultTransport = &http.Transport{ DialContext: (&net.Dialer{ Timeout: 5 * time.Second, }).DialContext, TLSHandshakeTimeout: 5 * time.Second, MaxIdleConns: 10, IdleConnTimeout: 30 * time.Second, }
DefaultTransport is the default implementation of Transport and is used by DefaultClient.
Functions ¶
func GetFreePort ¶ added in v2.7.0
GetFreePort is simple utility to find a free port on the "localhost" interface of the host machine for a local server to use. This is especially useful for testing purposes
Types ¶
type Client ¶
type Client struct { MaxRetries int RetryTime time.Duration PathsWithNoRetries map[string]bool HTTPClient *http.Client TotalTimeout time.Duration }
Client is an extension of the net/http client with ability to add timeouts, exponential backoff and context-based cancellation.
func (*Client) GetMaxRetries ¶
GetMaxRetries gets the HTTP request maximum number of retries.
func (*Client) GetPathsWithNoRetries ¶
GetPathsWithNoRetries gets a list of paths that will HTTP request will not retry on error.
func (*Client) Post ¶
func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
Post calls Do with a POST and the appropriate content-type and body.
func (*Client) Put ¶
func (c *Client) Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
Put calls Do with a PUT and the appropriate content-type and body.
func (*Client) SetMaxRetries ¶
SetMaxRetries sets HTTP request maximum number of retries.
func (*Client) SetPathsWithNoRetries ¶
SetPathsWithNoRetries sets a list of paths that will HTTP request will not retry on error.
func (*Client) SetTimeout ¶
SetTimeout sets HTTP request timeout (timeout 'per' try/request)
func (*Client) SetTotalTimeout ¶ added in v2.8.0
SetTotalTimeout sets the overall timeout (spans all retries)
type Clienter ¶
type Clienter interface { // Sets the overall timeout (spans all retries) SetTotalTimeout(timeout time.Duration) // Sets HTTP request timeout (timeout 'per' try/request) SetTimeout(timeout time.Duration) SetMaxRetries(int) GetMaxRetries() int SetPathsWithNoRetries([]string) GetPathsWithNoRetries() []string Get(ctx context.Context, url string) (*http.Response, error) Head(ctx context.Context, url string) (*http.Response, error) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error) Do(ctx context.Context, req *http.Request) (*http.Response, error) RoundTrip(req *http.Request) (*http.Response, error) }
Clienter provides an interface for methods on an HTTP Client.
func ClientWithListOfNonRetriablePaths ¶
ClientWithListOfNonRetriablePaths facilitates creating a client and setting a list of paths that should not be retried on failure.
func ClientWithTimeout ¶
ClientWithTimeout creates a client and sets per try/request timeout.
func ClientWithTimeouts ¶ added in v2.8.0
ClientWithTimeouts creates a client with both (per-request + total) timeout values
func ClientWithTotalTimeout ¶ added in v2.8.0
ClientWithTotalTimeout creates a client with overall timeout (spans all retries)
func NewClientWithTransport ¶
func NewClientWithTransport(transport http.RoundTripper) Clienter
NewClientWithTransport returns a copy of default client, plus changes to the transport layer
type ClienterMock ¶
type ClienterMock struct { // DoFunc mocks the Do method. DoFunc func(ctx context.Context, req *http.Request) (*http.Response, error) // GetFunc mocks the Get method. GetFunc func(ctx context.Context, url string) (*http.Response, error) // GetMaxRetriesFunc mocks the GetMaxRetries method. GetMaxRetriesFunc func() int // GetPathsWithNoRetriesFunc mocks the GetPathsWithNoRetries method. GetPathsWithNoRetriesFunc func() []string // HeadFunc mocks the Head method. HeadFunc func(ctx context.Context, url string) (*http.Response, error) // PostFunc mocks the Post method. PostFunc func(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) // PostFormFunc mocks the PostForm method. PostFormFunc func(ctx context.Context, uri string, data url.Values) (*http.Response, error) // PutFunc mocks the Put method. PutFunc func(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error) // RoundTripFunc mocks the RoundTrip method. RoundTripFunc func(req *http.Request) (*http.Response, error) // SetMaxRetriesFunc mocks the SetMaxRetries method. SetMaxRetriesFunc func(n int) // SetPathsWithNoRetriesFunc mocks the SetPathsWithNoRetries method. SetPathsWithNoRetriesFunc func(strings []string) // SetTimeoutFunc mocks the SetTimeout method. SetTimeoutFunc func(timeout time.Duration) // SetTotalTimeoutFunc mocks the SetTotalTimeout method. SetTotalTimeoutFunc func(timeout time.Duration) // contains filtered or unexported fields }
ClienterMock is a mock implementation of Clienter.
func TestSomethingThatUsesClienter(t *testing.T) { // make and configure a mocked Clienter mockedClienter := &ClienterMock{ DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) { panic("mock out the Do method") }, GetFunc: func(ctx context.Context, url string) (*http.Response, error) { panic("mock out the Get method") }, GetMaxRetriesFunc: func() int { panic("mock out the GetMaxRetries method") }, GetPathsWithNoRetriesFunc: func() []string { panic("mock out the GetPathsWithNoRetries method") }, HeadFunc: func(ctx context.Context, url string) (*http.Response, error) { panic("mock out the Head method") }, PostFunc: func(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) { panic("mock out the Post method") }, PostFormFunc: func(ctx context.Context, uri string, data url.Values) (*http.Response, error) { panic("mock out the PostForm method") }, PutFunc: func(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error) { panic("mock out the Put method") }, RoundTripFunc: func(req *http.Request) (*http.Response, error) { panic("mock out the RoundTrip method") }, SetMaxRetriesFunc: func(n int) { panic("mock out the SetMaxRetries method") }, SetPathsWithNoRetriesFunc: func(strings []string) { panic("mock out the SetPathsWithNoRetries method") }, SetTimeoutFunc: func(timeout time.Duration) { panic("mock out the SetTimeout method") }, SetTotalTimeoutFunc: func(timeout time.Duration) { panic("mock out the SetTotalTimeout method") }, } // use mockedClienter in code that requires Clienter // and then make assertions. }
func (*ClienterMock) DoCalls ¶
func (mock *ClienterMock) DoCalls() []struct { Ctx context.Context Req *http.Request }
DoCalls gets all the calls that were made to Do. Check the length with:
len(mockedClienter.DoCalls())
func (*ClienterMock) GetCalls ¶
func (mock *ClienterMock) GetCalls() []struct { Ctx context.Context URL string }
GetCalls gets all the calls that were made to Get. Check the length with:
len(mockedClienter.GetCalls())
func (*ClienterMock) GetMaxRetries ¶
func (mock *ClienterMock) GetMaxRetries() int
GetMaxRetries calls GetMaxRetriesFunc.
func (*ClienterMock) GetMaxRetriesCalls ¶
func (mock *ClienterMock) GetMaxRetriesCalls() []struct { }
GetMaxRetriesCalls gets all the calls that were made to GetMaxRetries. Check the length with:
len(mockedClienter.GetMaxRetriesCalls())
func (*ClienterMock) GetPathsWithNoRetries ¶
func (mock *ClienterMock) GetPathsWithNoRetries() []string
GetPathsWithNoRetries calls GetPathsWithNoRetriesFunc.
func (*ClienterMock) GetPathsWithNoRetriesCalls ¶
func (mock *ClienterMock) GetPathsWithNoRetriesCalls() []struct { }
GetPathsWithNoRetriesCalls gets all the calls that were made to GetPathsWithNoRetries. Check the length with:
len(mockedClienter.GetPathsWithNoRetriesCalls())
func (*ClienterMock) HeadCalls ¶
func (mock *ClienterMock) HeadCalls() []struct { Ctx context.Context URL string }
HeadCalls gets all the calls that were made to Head. Check the length with:
len(mockedClienter.HeadCalls())
func (*ClienterMock) Post ¶
func (mock *ClienterMock) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
Post calls PostFunc.
func (*ClienterMock) PostCalls ¶
func (mock *ClienterMock) PostCalls() []struct { Ctx context.Context URL string ContentType string Body io.Reader }
PostCalls gets all the calls that were made to Post. Check the length with:
len(mockedClienter.PostCalls())
func (*ClienterMock) PostForm ¶
func (mock *ClienterMock) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)
PostForm calls PostFormFunc.
func (*ClienterMock) PostFormCalls ¶
func (mock *ClienterMock) PostFormCalls() []struct { Ctx context.Context URI string Data url.Values }
PostFormCalls gets all the calls that were made to PostForm. Check the length with:
len(mockedClienter.PostFormCalls())
func (*ClienterMock) Put ¶
func (mock *ClienterMock) Put(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error)
Put calls PutFunc.
func (*ClienterMock) PutCalls ¶
func (mock *ClienterMock) PutCalls() []struct { Ctx context.Context UrlMoqParam string ContentType string Body io.Reader }
PutCalls gets all the calls that were made to Put. Check the length with:
len(mockedClienter.PutCalls())
func (*ClienterMock) RoundTripCalls ¶
func (mock *ClienterMock) RoundTripCalls() []struct { Req *http.Request }
RoundTripCalls gets all the calls that were made to RoundTrip. Check the length with:
len(mockedClienter.RoundTripCalls())
func (*ClienterMock) SetMaxRetries ¶
func (mock *ClienterMock) SetMaxRetries(n int)
SetMaxRetries calls SetMaxRetriesFunc.
func (*ClienterMock) SetMaxRetriesCalls ¶
func (mock *ClienterMock) SetMaxRetriesCalls() []struct { N int }
SetMaxRetriesCalls gets all the calls that were made to SetMaxRetries. Check the length with:
len(mockedClienter.SetMaxRetriesCalls())
func (*ClienterMock) SetPathsWithNoRetries ¶
func (mock *ClienterMock) SetPathsWithNoRetries(strings []string)
SetPathsWithNoRetries calls SetPathsWithNoRetriesFunc.
func (*ClienterMock) SetPathsWithNoRetriesCalls ¶
func (mock *ClienterMock) SetPathsWithNoRetriesCalls() []struct { Strings []string }
SetPathsWithNoRetriesCalls gets all the calls that were made to SetPathsWithNoRetries. Check the length with:
len(mockedClienter.SetPathsWithNoRetriesCalls())
func (*ClienterMock) SetTimeout ¶
func (mock *ClienterMock) SetTimeout(timeout time.Duration)
SetTimeout calls SetTimeoutFunc.
func (*ClienterMock) SetTimeoutCalls ¶
func (mock *ClienterMock) SetTimeoutCalls() []struct { Timeout time.Duration }
SetTimeoutCalls gets all the calls that were made to SetTimeout. Check the length with:
len(mockedClienter.SetTimeoutCalls())
func (*ClienterMock) SetTotalTimeout ¶ added in v2.8.0
func (mock *ClienterMock) SetTotalTimeout(timeout time.Duration)
SetTotalTimeout calls SetTotalTimeoutFunc.
func (*ClienterMock) SetTotalTimeoutCalls ¶ added in v2.8.0
func (mock *ClienterMock) SetTotalTimeoutCalls() []struct { Timeout time.Duration }
SetTotalTimeoutCalls gets all the calls that were made to SetTotalTimeout. Check the length with:
len(mockedClienter.SetTotalTimeoutCalls())
type Server ¶
type Server struct { http.Server Alice *alice.Chain CertFile string KeyFile string DefaultShutdownTimeout time.Duration HandleOSSignals bool RequestTimeout time.Duration TimeoutMessage string // contains filtered or unexported fields }
Server is a http.Server with sensible defaults, which supports configurable middleware and timeouts, and shuts down cleanly on SIGINT/SIGTERM
func NewServerWithTimeout ¶ added in v2.8.0
func NewServerWithTimeout(bindAddr string, router http.Handler, timeout time.Duration, timeoutMessage string) *Server
NewServerWithTimeout creates a new server with request timeout duration and a message that will be in the response body
func (*Server) ListenAndServe ¶
ListenAndServe sets up SIGINT/SIGTERM signals, builds the middleware chain, and creates/starts a http.Server instance
If CertFile/KeyFile are both set, the http.Server instance is started using ListenAndServeTLS. Otherwise, ListenAndServe is used.
Specifying one of CertFile/KeyFile without the other will panic.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS sets KeyFile and CertFile, then calls ListenAndServe