request

package
v0.0.0-...-ae86ed1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 18 Imported by: 0

README

GoCryptoTrader package Request

Build Status Software License GoDoc Coverage Status Go Report Card

This request package is part of the GoCryptoTrader codebase.

This is still in active development

You can track ideas, planned features and what's in progress on this Trello board: https://trello.com/b/ZAhMhpOy/gocryptotrader.

Join our slack to discuss all things related to GoCryptoTrader! GoCryptoTrader Slack

Current Features for request

  • This package services the exchanges package with request handling.
    • Throttling of requests for an individual exchange
Please click GoDocs chevron above to view current GoDoc information for this package

Contribution

Please feel free to submit any pull requests or suggest any desired features to be added.

When submitting a PR, please abide by our coding guidelines:

  • Code must adhere to the official Go formatting guidelines (i.e. uses gofmt).
  • Code must be documented adhering to the official Go commentary guidelines.
  • Code must adhere to our coding style.
  • Pull requests need to be based on and opened against the master branch.

Donations

If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:

bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc

Documentation

Index

Constants

View Source
const (
	DefaultMaxRequestJobs   int32 = 50
	DefaultMaxRetryAttempts       = 3
	DefaultMutexLockTimeout       = 50 * time.Millisecond
)

Const vars for rate limiter

Variables

View Source
var (
	MaxRequestJobs   = DefaultMaxRequestJobs
	MaxRetryAttempts = DefaultMaxRetryAttempts
)

Vars for rate limiter

Functions

func DefaultRetryPolicy

func DefaultRetryPolicy(resp *http.Response, err error) (bool, error)

DefaultRetryPolicy determines whether the request should be retried, implemented with a default strategy.

func NewRateLimit

func NewRateLimit(interval time.Duration, actions int) *rate.Limiter

NewRateLimit creates a new RateLimit based of time interval and how many actions allowed and breaks it down to an actions-per-second basis -- Burst rate is kept as one as this is not supported for out-bound requests.

func RetryAfter

func RetryAfter(resp *http.Response, now time.Time) time.Duration

RetryAfter parses the Retry-After header in the response to determine the minimum duration needed to wait before retrying.

Types

type Backoff

type Backoff func(n int) time.Duration

Backoff determines how long to wait between request attempts.

func DefaultBackoff

func DefaultBackoff() Backoff

DefaultBackoff is a default strategy for backoff after a retryable request failure.

func LinearBackoff

func LinearBackoff(base, max time.Duration) Backoff

LinearBackoff applies a backoff increasing by a base amount with each retry capped at a maximum duration.

type BasicLimit

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

BasicLimit denotes basic rate limit that implements the Limiter interface does not need to set endpoint functionality.

func (*BasicLimit) Limit

func (b *BasicLimit) Limit(_ EndpointLimit) error

Limit executes a single rate limit set by NewRateLimit

type EndpointLimit

type EndpointLimit int

EndpointLimit defines individual endpoint rate limits that are set when New is called.

const (
	Unset EndpointLimit = iota
	Auth
	UnAuth
)

Const here define individual functionality sub types for rate limiting

type Item

type Item struct {
	Method        string
	Path          string
	Headers       map[string]string
	Body          io.Reader
	Result        interface{}
	AuthRequest   bool
	NonceEnabled  bool
	Verbose       bool
	HTTPDebugging bool
	HTTPRecording bool
	IsReserved    bool
	// HeaderResponse for inspection of header contents package side useful for
	// pagination
	HeaderResponse *http.Header
	Endpoint       EndpointLimit
}

Item is a temp item for requests

type Limiter

type Limiter interface {
	Limit(EndpointLimit) error
}

Limiter interface groups rate limit functionality defined in the REST wrapper for extended rate limiting configuration i.e. Shells of rate limits with a global rate for sub rates.

func NewBasicRateLimit

func NewBasicRateLimit(interval time.Duration, actions int) Limiter

NewBasicRateLimit returns an object that implements the limiter interface for basic rate limit

type Requester

type Requester struct {
	HTTPClient *http.Client

	Name      string
	UserAgent string

	Nonce nonce.Nonce
	// contains filtered or unexported fields
}

Requester struct for the request client

func New

func New(name string, httpRequester *http.Client, opts ...RequesterOption) *Requester

New returns a new Requester

func (*Requester) DisableRateLimiter

func (r *Requester) DisableRateLimiter() error

DisableRateLimiter disables the rate limiting system for the exchange

func (*Requester) EnableRateLimiter

func (r *Requester) EnableRateLimiter() error

EnableRateLimiter enables the rate limiting system for the exchange

func (*Requester) GetNonce

func (r *Requester) GetNonce(isNano bool) nonce.Value

GetNonce returns a nonce for requests. This locks and enforces concurrent nonce FIFO on the buffered job channel

func (*Requester) GetNonceMilli

func (r *Requester) GetNonceMilli() nonce.Value

GetNonceMilli returns a nonce for requests. This locks and enforces concurrent nonce FIFO on the buffered job channel this is for millisecond

func (*Requester) InitiateRateLimit

func (r *Requester) InitiateRateLimit(e EndpointLimit) error

InitiateRateLimit sleeps for designated end point rate limits

func (*Requester) SendPayload

func (r *Requester) SendPayload(ctx context.Context, i *Item) error

SendPayload handles sending HTTP/HTTPS requests

func (*Requester) SetProxy

func (r *Requester) SetProxy(p *url.URL) error

SetProxy sets a proxy address to the client transport

type RequesterOption

type RequesterOption func(*Requester)

RequesterOption is a function option that can be applied to configure a Requester when creating it.

func WithBackoff

func WithBackoff(b Backoff) RequesterOption

WithBackoff configures the backoff strategy for a Requester.

func WithLimiter

func WithLimiter(l Limiter) RequesterOption

WithLimiter configures the rate limiter for a Requester.

func WithRetryPolicy

func WithRetryPolicy(p RetryPolicy) RequesterOption

WithRetryPolicy configures the retry policy for a Requester.

type RetryPolicy

type RetryPolicy func(resp *http.Response, err error) (bool, error)

RetryPolicy determines whether the request should be retried.

Jump to

Keyboard shortcuts

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