reqstrategy

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 4 Imported by: 0

README

reqstrategy

Package reqstrategy provides functions for coordinating http.Client calls. It wraps typical call strategies like making simultaneous requests, retrying, racing etc.

First, define what request counts successful, there are two helpers adding rules to the request context

req, _ = http.NewRequest("GET", "http://localhost/", nil)
req = WithStatusRequired(req, 200, 404)

or do adavanced validation using custom logic

req, _ = http.NewRequest("GET", "http://localhost/", nil)
req = WithValidator(req, func(resp *http.Response) error {
  if resp.StatusCode != http.StatusOK {
    return fmt.Error("oops")
  }
  return nil
})

Then, make a call

Do() is not much different from calling client.Do(request) except it runs the response validation. See WithValidator and WithSTatusRequired

resp, err := Do(http.DefaultClient, req)

Retry() re-attempts request with provided intervals. By manually providing intervals sequence you can have different wait strategies like exponential back-off (time.Second, 2 * time.Second, 4 * time.Second) or just multiple reties after same interval (time.Second, time.Second, time.Second). If Request had a context with timeout cancelation then it will be applied to entire chain

resp, err := Retry(http.DefaultClient, req, time.Second, time.Second, time.Second)

Race() runs multiple requests simultaneously returning first successulf result or error if all failed. Once result is determined all requests are cancelled through the context.

resps, err := Race(http.DefaultClient, req0, req1, reqX)

All() runs requests simultaneously returning responses in same order or error if at least one request failed. Once result is determined all requests are cancelled through the context.

resps, err := All(http.DefaultClient, req0, req1, reqX)

Some() runs requests simultaneously returning responses for successful requests and <nil> for failed ones. Error is returned only if all requests failed.

resps, err := Some(http.DefaultClient, req0, req1, reqX)

Documentation

Overview

Package reqstrategy provides functions for coordinating http.Client calls. It wraps typical call strategies like making simultaneous requests, retrying, racing etc. Package is not aiming to replace standard library's client and Request types only provides the way to invoke them in different manners

First, define what response counts successful, there are two helpers adding rules to request context

req, _ = http.NewRequest("GET", "http://localhost/", nil)
req = WithStatusRequired(req, 200, 404)

or do adavanced validation using custom logic

req, _ = http.NewRequest("GET", "http://localhost/", nil)
req = WithValidator(req, func(resp *http.Response) error {
  if resp.StatusCode != http.StatusOK {
    return fmt.Error("oops")
  }
  return nil
})

then use one of the Do, Race, All, Retry to invoke the request with following response validation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(client *http.Client, requests ...*http.Request) ([]*http.Response, error)

All runs requests simultaneously returning responses in same order or error if at least one request failed. Once result is determined all requests are cancelled through the context.

func Do

func Do(client *http.Client, request *http.Request) (*http.Response, error)

Do is not much different from calling client.Do(request) except it runs the response validation. See WithValidator and WithSTatusRequired

func Race

func Race(client *http.Client, requests ...*http.Request) (*http.Response, error)

Race runs requests simultaneously returning first successulf result or error if all failed. Once result is determined all requests are cancelled through the context.

func Retry

func Retry(client *http.Client, request *http.Request, intervals ...time.Duration) (*http.Response, error)

Retry re-attempts request with provided intervals. By manually providing intervals sequence you can have different wait strategies like exponential back-off (time.Second, 2 * time.Second, 4 * time.Second) or just multiple reties after same interval (time.Second, time.Second, time.Second). If Request had a context with timeout cancelation then it will be applied to entire chain

func Some

func Some(client *http.Client, requests ...*http.Request) ([]*http.Response, error)

Some runs requests simultaneously returning responses for successful requests and <nil> for failed ones. Error is returned only if all requests failed.

func WithStatusRequired

func WithStatusRequired(r *http.Request, codes ...int) *http.Request

WithStatusRequired adds the response validator by listing acceptable status codes

func WithValidator

func WithValidator(r *http.Request, validate validator) *http.Request

WithValidator introduces a response validator function to the context to be used in Do/Race/All/Some/Retry

Types

This section is empty.

Jump to

Keyboard shortcuts

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