http

package
v2.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 18 Imported by: 66

Documentation

Index

Constants

View Source
const (
	RequestIDHandlerKey string        = "RequestID"
	LogHandlerKey       string        = "Log"
	ResponseWriteGrace  time.Duration = 100 * time.Millisecond
)
View Source
const (
	DefaultRequestTimeout = 10 * time.Second
)

Variables

View Source
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

View Source
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 DrainBody

func DrainBody(r *http.Request)

DrainBody drains the body of the given of the given HTTP request.

func GetFreePort added in v2.7.0

func GetFreePort() (port int, err error)

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) Do

func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do calls ctxhttp.Do with the addition of retries with exponential backoff

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*http.Response, error)

Get calls Do with a GET.

func (*Client) GetMaxRetries

func (c *Client) GetMaxRetries() int

GetMaxRetries gets the HTTP request maximum number of retries.

func (*Client) GetPathsWithNoRetries

func (c *Client) GetPathsWithNoRetries() (paths []string)

GetPathsWithNoRetries gets a list of paths that will HTTP request will not retry on error.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string) (*http.Response, error)

Head calls Do with a HEAD.

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) PostForm

func (c *Client) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)

PostForm calls Post with the appropriate form content-type.

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) RoundTrip

func (c *Client) RoundTrip(req *http.Request) (*http.Response, error)

Clienter roundtripper calls the httpclient roundtripper.

func (*Client) SetMaxRetries

func (c *Client) SetMaxRetries(maxRetries int)

SetMaxRetries sets HTTP request maximum number of retries.

func (*Client) SetPathsWithNoRetries

func (c *Client) SetPathsWithNoRetries(paths []string)

SetPathsWithNoRetries sets a list of paths that will HTTP request will not retry on error.

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout sets HTTP request timeout (timeout 'per' try/request)

func (*Client) SetTotalTimeout added in v2.8.0

func (c *Client) SetTotalTimeout(timeout time.Duration)

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

func ClientWithListOfNonRetriablePaths(c Clienter, paths []string) Clienter

ClientWithListOfNonRetriablePaths facilitates creating a client and setting a list of paths that should not be retried on failure.

func ClientWithTimeout

func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter

ClientWithTimeout creates a client and sets per try/request timeout.

func ClientWithTimeouts added in v2.8.0

func ClientWithTimeouts(c Clienter, perRequest time.Duration, total time.Duration) Clienter

ClientWithTimeouts creates a client with both (per-request + total) timeout values

func ClientWithTotalTimeout added in v2.8.0

func ClientWithTotalTimeout(c Clienter, timeout time.Duration) Clienter

ClientWithTotalTimeout creates a client with overall timeout (spans all retries)

func NewClient

func NewClient() Clienter

NewClient returns a copy of DefaultClient.

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) Do

func (mock *ClienterMock) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do calls DoFunc.

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) Get

func (mock *ClienterMock) Get(ctx context.Context, url string) (*http.Response, error)

Get calls GetFunc.

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) Head

func (mock *ClienterMock) Head(ctx context.Context, url string) (*http.Response, error)

Head calls HeadFunc.

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) RoundTrip

func (mock *ClienterMock) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip calls RoundTripFunc.

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 Doer

type Doer = func(context.Context, *http.Client, *http.Request) (*http.Response, error)

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 NewServer

func NewServer(bindAddr string, router http.Handler) *Server

NewServer creates a new server

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

func (s *Server) ListenAndServe() error

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

func (s *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS sets KeyFile and CertFile, then calls ListenAndServe

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown will gracefully shutdown the server, using a default shutdown timeout if a context is not provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL