fancyhttpclient

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2020 License: MIT Imports: 5 Imported by: 0

README

Fancy HTTP Client

Golang HTTP Client that satisfies the Doer interface, i.e.

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

With configurable throttling and / or connection limiting.

Quick Start

Base client with no throttling and connection limit, this is effectively the same as using the base client itself.

baseHC := &http.Client{
    Timeout: 30 * time.Second
}

fhc := fancyhttpclient.New(baseHC)

Client with delay between outgoing requests, this will space out outgoing requests on a 50ms interval. The delay is applied across threads if threads share the same instance of fhc.

fhc := fancyhttpclient.New(baseHC, fancyhttpclient.WithDelay(50*time.Millisecond))

Client with limit to the max number of connections it has to the server, will limit to max 5 connections

fhc := fancyhttpclient.New(baseHC, fancyhttpclient.WithMaxCoon(5))

Or both! This will limit max connection to 5 and space out the outgoing requests by 50ms

fhc := fancyhttpclient.New(
    baseHC, 
    fancyhttpclient.WithDelay(50*time.Millisecond),
    fancyhttpclient.WithMaxCoon(5),
)

It can then be passed to multiple goroutines that each performs individual Do()s, or

reqs := []*http.Request{bunch, of, requests}
responsers, _ := fhc.DoBunch(reqs)
res0, err0 := responsers[0].Response()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientOption

type ClientOption func(fhc *FancyHTTPClient)

ClientOption apply custom options to the client

func WithDelay

func WithDelay(delay time.Duration) ClientOption

WithDelay sets up the client to space out each requests sent

func WithMaxConn

func WithMaxConn(maxConn int) ClientOption

WithMaxConn sets up the client to limit its connection to a set number

type Doer

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

Doer performs an HTTP request

type FancyHTTPClient

type FancyHTTPClient struct {
	// contains filtered or unexported fields
}

FancyHTTPClient is a custom HTTP client that is able to handle

func New

func New(hc Doer, options ...ClientOption) *FancyHTTPClient

New returns an instance of FancyHTTPClient according to the delay and timeout provided

func (*FancyHTTPClient) Destroy added in v0.2.0

func (c *FancyHTTPClient) Destroy()

Destroy waits for existing work to finish and stops the workerpool, function will only return after all work has been done

func (*FancyHTTPClient) Do

func (c *FancyHTTPClient) Do(req *http.Request) (*http.Response, error)

Do fires off one single request

func (*FancyHTTPClient) DoBunch

func (c *FancyHTTPClient) DoBunch(reqs []*http.Request) ([]*ResponseGetter, error)

DoBunch fires off a bunch of requests with the configured delay and returns all, reserving the order of slice passed in

type ResponseGetter

type ResponseGetter struct {
	// contains filtered or unexported fields
}

ResponseGetter contains either an http response or an error

func (*ResponseGetter) Response

func (rg *ResponseGetter) Response() (*http.Response, error)

Response returns the response or error wrapped in the ResponseGetter

Jump to

Keyboard shortcuts

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