pester

package module
v0.0.0-...-028a328 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 10 Imported by: 0

README

pester

forked from https://github.com/sethgrid/pester

add feature

  1. should input timeout explicitly for every request
  2. add asynPester, and add callbackFun when asyn finish

demo

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"testing"
	"time"
)

func TestAsynGetCallBack(t *testing.T)  {
	c := New()
	c.Concurrency = 3
	c.KeepLog = true
	c.Backoff = ExponentialBackoff
	c.MaxRetries = 3

	url := "http://localhost/mock"

	stopChan := make(chan string)

	callBack := func (resp *http.Response, err error, logs string, transmissionParams ...interface{} ) {
		if err != nil {
			t.Errorf("err:%v\nlogs:%v, \ntransmissionParams:%v", err, logs, transmissionParams)
			stopChan <- ""
			return
		}
		if nil == resp || nil == resp.Body{
			t.Errorf("response nil, logs:%s,  transmissionParams:%v", logs, transmissionParams)
			stopChan <- ""
			return
		}
		body,er := ioutil.ReadAll(resp.Body)
		t.Logf("resp: %v, err:%v, logs:%s, transmissionParams:%v\n", string(body), er, logs, transmissionParams)
		stopChan <- ""
	}

	err := c.GetAsyn(url, 50*time.Millisecond, callBack, 111)
	if err != nil {
		t.Errorf(c.LogString())
		return
	}

	<-stopChan
}


var postStopChan = make(chan string)

func asynPostCallBack(resp *http.Response, err error, logs string,  transmissionParams ...interface{} ) {
	if err != nil {
		fmt.Printf("err:%v\nlogs:%v, \ntransmissionParams:%v", err, logs, transmissionParams)
		postStopChan <- ""
		return
	}
	if nil == resp || nil == resp.Body {
		fmt.Printf("response nil, logs:%s,  transmissionParams:%v", logs, transmissionParams)
		postStopChan <- ""
		return
	}
	body,er := ioutil.ReadAll(resp.Body)
	fmt.Printf("resp: %v, err:%v, logs:%s, transmissionParams:%v\n", string(body), er, logs, transmissionParams)
	postStopChan <- ""
}

func TestAsynPostCallBack(t *testing.T) {
	c := New()
	c.KeepLog = true
	c.Backoff = DefaultBackoff
	c.MaxRetries = 5

	url := "http://localhost:8081/mock"

	bodyParam := map[string]interface{}{"slotId":"1000"}
	body, _ := json.Marshal(bodyParam)
	err := c.PostAsyn(url, body,  100*time.Millisecond, asynPostCallBack, 111, 112,"sss")
	if err != nil {
		t.Errorf(c.LogString())
		return
	}
	t.Logf("waiting for callback")
	<-postStopChan
}


Documentation

Overview

Package pester provides additional resiliency over the standard http client methods by allowing you to control concurrency, retries, and a backoff strategy.

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &Client{Concurrency: 1, MaxRetries: 3, Backoff: DefaultBackoff, ErrLog: []ErrEntry{}}

DefaultClient provides sensible defaults

View Source
var ErrNoTimeOutSet = errors.New("error no timeout set for params")

no timeout param when calling pester

View Source
var ErrPanicDuringRequest = errors.New("error panic happened during request")

panic happened during request

View Source
var ErrReadingBody = errors.New("error reading body")

ErrReadingBody happens when we cannot read the body bytes

View Source
var ErrReadingRequestBody = errors.New("error reading request body")

ErrReadingRequestBody happens when we cannot read the request body bytes

View Source
var ErrUnexpectedMethod = errors.New("unexpected client method, must be one of Do, Get, Head, Post, or PostFrom")

ErrUnexpectedMethod occurs when an http.Client method is unable to be mapped from a calling method in the pester client

Functions

func DefaultBackoff

func DefaultBackoff(_ int) time.Duration

DefaultBackoff always returns 1 second

func Do

func Do(req *http.Request, timeout time.Duration) (resp *http.Response, err error)

Do provides the same functionality as http.Client.Do and creates its own constructor

func ExponentialBackoff

func ExponentialBackoff(i int) time.Duration

ExponentialBackoff returns ever increasing backoffs by a power of 2

func ExponentialJitterBackoff

func ExponentialJitterBackoff(i int) time.Duration

ExponentialJitterBackoff returns ever increasing backoffs by a power of 2 with +/- 0-33% to prevent sychronized reuqests.

func Get

func Get(url string, timeout time.Duration) (resp *http.Response, err error)

Get provides the same functionality as http.Client.Get and creates its own constructor

func Head(url string, timeout time.Duration) (resp *http.Response, err error)

Head provides the same functionality as http.Client.Head and creates its own constructor

func LinearBackoff

func LinearBackoff(i int) time.Duration

LinearBackoff returns increasing durations, each a second longer than the last

func LinearJitterBackoff

func LinearJitterBackoff(i int) time.Duration

LinearJitterBackoff returns increasing durations, each a second longer than the last with +/- 0-33% to prevent sychronized reuqests.

func Post

func Post(url string, body []byte, timeout time.Duration) (resp *http.Response, err error)

Post provides the same functionality as http.Client.Post and creates its own constructor

func PostForm

func PostForm(url string, data url.Values, timeout time.Duration) (resp *http.Response, err error)

PostForm provides the same functionality as http.Client.PostForm and creates its own constructor

Types

type BackoffStrategy

type BackoffStrategy func(retry int) time.Duration

BackoffStrategy is used to determine how long a retry request should wait until attempted

type Client

type Client struct {
	Transport     http.RoundTripper
	CheckRedirect func(req *http.Request, via []*http.Request) error
	Jar           http.CookieJar

	// pester specific
	Concurrency int
	MaxRetries  int
	Backoff     BackoffStrategy
	KeepLog     bool
	LogHook     LogHook

	SuccessReqNum   int
	SuccessRetryNum int

	sync.Mutex
	ErrLog         []ErrEntry
	RetryOnHTTP429 bool
	// contains filtered or unexported fields
}

Client wraps the http client and exposes all the functionality of the http.Client. Additionally, Client provides pester specific values for handling resiliency. Concurrency only useful for GET

func New

func New() *Client

New constructs a new DefaultClient with sensible default values

func NewExtendedClient

func NewExtendedClient(hc *http.Client) *Client

NewExtendedClient allows you to pass in an http.Client that is previously set up and extends it to have Pester's features of concurrency and retries.

func (*Client) Do

func (c *Client) Do(req *http.Request, timeout time.Duration) (resp *http.Response, err error)

Do provides the same functionality as http.Client.Do

func (*Client) DoAsyn

func (c *Client) DoAsyn(req *http.Request, timeout time.Duration,
	fun RequestCallBackFun, transmissionParams ...interface{}) error

Do provides the same functionality as http.Client.Do

func (*Client) EmbedHTTPClient

func (c *Client) EmbedHTTPClient(hc *http.Client)

EmbedHTTPClient allows you to extend an existing Pester client with an underlying http.Client, such as https://godoc.org/golang.org/x/oauth2/google#DefaultClient

func (*Client) FormatError

func (c *Client) FormatError(e ErrEntry) string

Format the Error to human readable string

func (*Client) Get

func (c *Client) Get(url string, timeout time.Duration) (resp *http.Response, err error)

Get provides the same functionality as http.Client.Get

func (*Client) GetAsyn

func (c *Client) GetAsyn(url string, timeout time.Duration,
	fun RequestCallBackFun, transmissionParams ...interface{}) error

Get provides the same functionality as http.Client.Get

func (*Client) Head

func (c *Client) Head(url string, timeout time.Duration) (resp *http.Response, err error)

Head provides the same functionality as http.Client.Head

func (*Client) LogErrCount

func (c *Client) LogErrCount() int

LogErrCount is a helper method used primarily for test validation

func (*Client) LogString

func (c *Client) LogString() string

LogString provides a string representation of the errors the client has seen

func (*Client) Post

func (c *Client) Post(url string, body []byte, timeout time.Duration) (resp *http.Response, err error)

Post provides the same functionality as http.Client.Post Post json

func (*Client) PostAsyn

func (c *Client) PostAsyn(url string, body []byte, timeout time.Duration,
	fun RequestCallBackFun, transmissionParams ...interface{}) error

Post provides the same functionality as http.Client.Post Post json

func (*Client) PostForm

func (c *Client) PostForm(url string, data url.Values, timeout time.Duration) (resp *http.Response, err error)

PostForm provides the same functionality as http.Client.PostForm

func (*Client) PostFormAsyn

func (c *Client) PostFormAsyn(url string, data url.Values, timeout time.Duration,
	fun RequestCallBackFun, transmissionParams ...interface{}) error

PostForm provides the same functionality as http.Client.PostForm

func (*Client) SetRetryOnHTTP429

func (c *Client) SetRetryOnHTTP429(flag bool)

set RetryOnHTTP429 for clients,

func (*Client) Wait

func (c *Client) Wait()

Wait blocks until all pester requests have returned Probably not that useful outside of testing.

type ErrEntry

type ErrEntry struct {
	Time    time.Time
	Method  string
	URL     string
	Verb    string
	Request int
	Retry   int
	Attempt int
	Err     error
}

ErrEntry is used to provide the LogString() data and is populated each time an error happens if KeepLog is set. ErrEntry.Retry is deprecated in favor of ErrEntry.Attempt

type LogHook

type LogHook func(e ErrEntry)

LogHook is used to log attempts as they happen. This function is never called, however, if KeepLog is set to true.

type RequestCallBackFun

type RequestCallBackFun func(resp *http.Response, err error, logs string, transmissionParams ...interface{})

asyc回调

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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