rehttp

package module
v0.0.0-...-2fb5764 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2016 License: BSD-3-Clause Imports: 12 Imported by: 0

README

rehttp

Golang HTTP client wrapping httpcontrol.Transport, http.Client & redial.Dialer

Build Status GoDoc BSD License

Features
  • Connection pool
  • Layered timeout's
  • Request stats
  • Lower/Higher level API
  • Cached DNS records with round-robin or random access
  • Request retry
  • Keep alive
  • Compression
  • HTTPS
Wrapper

This package wraps underlying layers of http request procedure into readable & configurable options. Package exposes 2 major components http Client & Transport. HTTP Client is swappable where Transport but satisfies standard library interface.

License

Copyright (c) 2015, linkosmos All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of rehttp nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Index

Constants

View Source
const (
	RequestProto      = "HTTP/1.1"
	RequestProtoMinor = 0
	RequestProtoMajor = 1
)

-

View Source
const (
	GET    = "GET"
	HEAD   = "HEAD"
	POST   = "POST"
	PUT    = "PUT"
	PATCH  = "PATCH"
	DELETE = "DELETE"
)

Request methods

View Source
const (
	DefaultDialerTimeout                = 30 * time.Second
	DefaultDialerDualStack              = false
	DefaultDialerKeepAlive              = 30 * time.Second
	DefaultTransportMaxTries            = 3
	DefaultTransportDisableKeepAlives   = false
	DefaultTransportDisableCompression  = false
	DefaultTransportMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
	DefaultTransportRetryAfterTimeout   = true
	DefaultClientTimeout                = 1 * time.Minute // Default 3 min
	DefaultTLSInsecureSkipVerify        = false
	DefaultConnPoolInitial              = 2
	DefaultConnPoolMax                  = 5
)

RequestClient options default values

View Source
const DefaultUserAgent = "Golang rehttp.Client"

DefaultUserAgent - default user agent for this request client package

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	RequestProto string

	RequestProtoMinor, RequestProtoMajor int

	// A Config structure is used to configure a TLS client or server.
	// After one has been passed to a TLS function it must not be
	// modified. A Config may be reused; the tls package will also not
	// modify it.
	TLS *tls.Config

	// Transport is an implementation of RoundTripper that supports HTTP,
	// HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT).
	// Transport can also cache connections for future re-use.
	Transport *httpcontrol.Transport

	// A Client is an HTTP client. Its zero value (DefaultClient) is a
	// usable client that uses DefaultTransport.
	//
	// The Client's Transport typically has internal state (cached TCP
	// connections), so Clients should be reused instead of created as
	// needed. Clients are safe for concurrent use by multiple goroutines.
	//
	// A Client is higher-level than a RoundTripper (such as Transport)
	// and additionally handles HTTP details such as cookies and
	// redirects.
	Client ClientRequester

	// rehttp.Client options
	ClientOptions *Options
}

Client - http.: Transport, Client wrapper

func New

func New(op *Options, address, port string) (r *Client, err error)

New - returns Request Client

func (*Client) Do

func (r *Client) Do(req *http.Request) (*http.Response, error)

Do - sends an HTTP request and returns an HTTP response, following policy (e.g. redirects, cookies, auth) as configured on the client.

An error is returned if caused by client policy (such as CheckRedirect), or if there was an HTTP protocol error. A non-2xx response doesn't cause an error.

When err is nil, resp always contains a non-nil resp.Body.

Callers should close resp.Body when done reading from it. If resp.Body is not closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.

The request Body, if non-nil, will be closed by the underlying Transport, even on errors.

Generally Get, Post, or PostForm will be used instead of Do.

func (*Client) GET

func (r *Client) GET(u *url.URL) *http.Request

GET - request

func (*Client) HEAD

func (r *Client) HEAD(u *url.URL) *http.Request

HEAD - request

func (*Client) NewRequest

func (r *Client) NewRequest(method string, u *url.URL, body io.Reader) (req *http.Request)

NewRequest returns a new Request given a method, URL, and optional body.

If the provided body is also an io.Closer, the returned Request.Body is set to body and will be closed by the Client methods Do, Post, and PostForm, and Transport.RoundTrip.

func (Client) RoundTrip

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

RoundTrip implements the RoundTripper interface.

For higher-level HTTP client support (such as handling of cookies and redirects), see Get, Post, and the Client type.

type ClientRequester

type ClientRequester interface {
	Do(req *http.Request) (*http.Response, error)
}

ClientRequester - higher level API that implements http.Client

type Options

type Options struct {

	////////////////////////////////
	// Connection Pool
	////////////////////////////////
	// Connection pool based on buffered channels with an initial
	// capacity and maximum capacity. Factory is used when initial capacity is
	// greater than zero to fill the pool. A zero initialCap doesn't fill the Pool
	// until a new Get() is called. During a Get(), If there is no new connection
	// available in the pool, a new connection will be created via the Factory()
	// method.
	ConnPoolInitial int
	ConnPoolMax     int

	// KeepAlive specifies the keep-alive period for an active
	// network connection.
	// If zero, keep-alives are not enabled. Network protocols
	// that do not support keep-alives ignore this field.
	KeepAlive time.Duration

	////////////////////////////////
	// Dialer
	////////////////////////////////
	// Timeout is the maximum amount of time a dial will wait for
	// a connect to complete.
	//
	// With or without a timeout, the operating system may impose
	// its own earlier timeout. For instance, TCP timeouts are
	// often around 3 minutes.
	DialerTimeout time.Duration // The default is no timeout.

	// Deadline is the absolute point in time after which dials
	// will fail. If Timeout is set, it may fail earlier.
	// Zero means no deadline, or dependent on the operating system
	// as with the Timeout option.
	DialerDeadline time.Time

	// DualStack allows a single dial to attempt to establish
	// multiple IPv4 and IPv6 connections and to return the first
	// established connection when the network is "tcp" and the
	// destination is a host name that has multiple address family
	// DNS records.
	DialerDualStack bool

	// KeepAlive specifies the keep-alive period for an active
	// network connection.
	// If zero, keep-alives are not enabled. Network protocols
	// that do not support keep-alives ignore this field.
	DialerKeepAlive time.Duration

	// FallbackDelay specifies the length of time to wait before
	// spawning a fallback connection, when DualStack is enabled.
	// If zero, a default delay of 300ms is used.
	DialerFallbackDelay time.Duration

	//
	////////////////////////////////
	// Transport
	////////////////////////////////
	//
	// MaxTries, if non-zero, specifies the number of times we will retry on
	// failure. Retries are only attempted for temporary network errors or known
	// safe failures.
	TransportMaxTries uint

	// DisableKeepAlives, if true, prevents re-use of TCP connections
	// between different HTTP requests.
	TransportDisableKeepAlives bool

	// DisableCompression, if true, prevents the Transport from
	// requesting compression with an "Accept-Encoding: gzip"
	TransportDisableCompression bool

	// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
	// (keep-alive) to keep per-host.
	TransportMaxIdleConnsPerHost int

	// RequestTimeout, if non-zero, specifies the amount of time for the entire
	// request. This includes dialing (if necessary), the response header as well
	// as the entire body.
	TransportRequestTimeout time.Duration

	// ResponseHeaderTimeout, if non-zero, specifies the amount of
	// time to wait for a server's response headers after fully
	// writing the request (including its body, if any). This
	// time does not include the time to read the response body.
	TransportResponseHeaderTimeout time.Duration

	// RetryAfterTimeout, if true, will enable retries for a number of failures
	// that are probably safe to retry for most cases but, depending on the
	// context, might not be safe. Retried errors: net.Errors where Timeout()
	// returns `true` or timeouts that bubble up as url.Error but were originally
	// net.Error, OpErrors where the request was cancelled (either by this lib or
	// by the calling code, or finally errors from requests that were cancelled
	// before the remote side was contacted.
	TransportRetryAfterTimeout bool

	// Timeout specifies a time limit for requests made by this
	// Client. The timeout includes connection time, any
	// redirects, and reading the response body. The timer remains
	// running after Get, Head, Post, or Do return and will
	// interrupt reading of the Response.Body.
	//
	// A Timeout of zero means no timeout.
	//
	// The Client's Transport must support the CancelRequest
	// method or Client will return errors when attempting to make
	// a request with Get, Head, Post, or Do. Client's default
	// Transport (DefaultTransport) supports CancelRequest.
	ClientTimeout time.Duration

	// InsecureSkipVerify controls whether a client verifies the
	// server's certificate chain and host name.
	TLSInsecureSkipVerify bool

	// Request headers
	Headers http.Header
}

Options - keep options for continuous requests

func NewOptions

func NewOptions() (op *Options)

NewOptions - options struct initialized with default values

func (*Options) GetUserAgent

func (o *Options) GetUserAgent() (ua string)

GetUserAgent - sets request user agent

func (*Options) SetHeaderKeepAlive

func (o *Options) SetHeaderKeepAlive()

SetHeaderKeepAlive - sets keep-alive instruction in header

func (*Options) SetHeaderUserAgent

func (o *Options) SetHeaderUserAgent(ua string)

SetHeaderUserAgent - sets request user agent

func (*Options) String

func (o *Options) String() string

type Requester

type Requester interface {
	Do(*http.Request) (*http.Response, error)
	RoundTrip(*http.Request) (*http.Response, error)
}

Requester - wraps RoundTripper & ClientRequester

type RoundTripper

type RoundTripper interface {
	// RoundTrip executes a single HTTP transaction, returning
	// the Response for the request req.  RoundTrip should not
	// attempt to interpret the response.  In particular,
	// RoundTrip must return err == nil if it obtained a response,
	// regardless of the response's HTTP status code.  A non-nil
	// err should be reserved for failure to obtain a response.
	// Similarly, RoundTrip should not attempt to handle
	// higher-level protocol details such as redirects,
	// authentication, or cookies.
	//
	// RoundTrip should not modify the request, except for
	// consuming and closing the Body, including on errors. The
	// request's URL and Header fields are guaranteed to be
	// initialized.
	RoundTrip(*http.Request) (*http.Response, error)
}

RoundTripper is an interface representing the ability to execute a single HTTP transaction, obtaining the Response for a given Request.

A RoundTripper must be safe for concurrent use by multiple goroutines.

Jump to

Keyboard shortcuts

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