request

package
v0.0.0-...-ca62b0a Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 19 Imported by: 199

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 our GoCryptoTrader Kanban board.

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 (
	// UnsetRequest is an unset request authentication level
	UnsetRequest AuthType = 0
	// UnauthenticatedRequest denotes a request with no credentials
	UnauthenticatedRequest = iota << 1
	// AuthenticatedRequest denotes a request using API credentials
	AuthenticatedRequest
)
View Source
const (
	DefaultMaxRetryAttempts = 3
	DefaultMutexLockTimeout = 50 * time.Millisecond
)

Const vars for rate limiter

Variables

View Source
var (
	ErrRateLimiterAlreadyDisabled = errors.New("rate limiter already disabled")
	ErrRateLimiterAlreadyEnabled  = errors.New("rate limiter already enabled")
)

Defines rate limiting errors

View Source
var (
	// ErrRequestSystemIsNil defines and error if the request system has not
	// been set up yet.
	ErrRequestSystemIsNil = errors.New("request system is nil")
	// ErrAuthRequestFailed is a wrapping error to denote that it's an auth request that failed
	ErrAuthRequestFailed = errors.New("authenticated request failed")
	// ErrBadStatus is a wrapping error to denote that the HTTP status code was unsuccessful
	ErrBadStatus = errors.New("unsuccessful HTTP status code")
)
View Source
var (
	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 IsVerbose

func IsVerbose(ctx context.Context, verbose bool) bool

IsVerbose checks main verbosity first then checks context verbose values for specific request verbosity.

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 RateLimit

func RateLimit(ctx context.Context, rateLimiter *RateLimiterWithWeight) error

RateLimit is a function that will rate limit a request based on the rate limiter provided. It will return an error if the context is cancelled or deadline exceeded.

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.

func SetupGlobalReporter

func SetupGlobalReporter(r Reporter)

SetupGlobalReporter sets a reporter interface to be used for all exchange requests

func WithVerbose

func WithVerbose(ctx context.Context) context.Context

WithVerbose adds verbosity to a request context so that specific requests can have distinct verbosity without impacting all requests.

Types

type AuthType

type AuthType uint8

AuthType helps distinguish the purpose of a HTTP request

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, maxDuration time.Duration) Backoff

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

type EndpointLimit

type EndpointLimit uint16

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 Generate

type Generate func() (*Item, error)

Generate defines a closure for functionality outside the requester to generate a new *http.Request on every attempt. This minimizes the chance of being outside the receive window if application rate limiting reduces outbound requests.

type Item

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

Item is a temp item for requests

type RateLimitDefinitions

type RateLimitDefinitions map[interface{}]*RateLimiterWithWeight

RateLimitDefinitions is a map of endpoint limits to rate limiters

func NewBasicRateLimit

func NewBasicRateLimit(interval time.Duration, actions int, weight Weight) RateLimitDefinitions

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

type RateLimiterWithWeight

type RateLimiterWithWeight struct {
	*rate.Limiter
	Weight
}

RateLimiterWithWeight is a rate limiter coupled with a weight count which refers to the number or weighting of the request. This is used to define the rate limit for a specific endpoint.

func GetRateLimiterWithWeight

func GetRateLimiterWithWeight(l *rate.Limiter, weight Weight) *RateLimiterWithWeight

GetRateLimiterWithWeight couples a rate limiter with a weight count into an accepted defined rate limiter with weight struct

func NewRateLimitWithWeight

func NewRateLimitWithWeight(interval time.Duration, actions int, weight Weight) *RateLimiterWithWeight

NewRateLimitWithWeight creates a new RateLimit based of time interval and how many actions allowed. This also has a weight count which refers to the number or weighting of the request. This is used to define the rate limit for a specific endpoint.

func NewWeightedRateLimitByDuration

func NewWeightedRateLimitByDuration(interval time.Duration) *RateLimiterWithWeight

NewWeightedRateLimitByDuration creates a new RateLimit based of time interval. This equates to 1 action per interval. The weight is set to 1.

type Reporter

type Reporter interface {
	Latency(name, method, path string, t time.Duration)
}

Reporter interface groups observability functionality over HTTP request latency.

type Requester

type Requester struct {
	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, error)

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) GetHTTPClientUserAgent

func (r *Requester) GetHTTPClientUserAgent() (string, error)

GetHTTPClientUserAgent gets the exchanges HTTP user agent

func (*Requester) GetNonce

func (r *Requester) GetNonce(set nonce.Setter) nonce.Value

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

func (*Requester) GetRateLimiterDefinitions

func (r *Requester) GetRateLimiterDefinitions() RateLimitDefinitions

GetRateLimiterDefinitions returns the rate limiter definitions for the requester

func (*Requester) InitiateRateLimit

func (r *Requester) InitiateRateLimit(ctx context.Context, e EndpointLimit) error

InitiateRateLimit sleeps for designated end point rate limits

func (*Requester) SendPayload

func (r *Requester) SendPayload(ctx context.Context, ep EndpointLimit, newRequest Generate, requestType AuthType) error

SendPayload handles sending HTTP/HTTPS requests

func (*Requester) SetHTTPClient

func (r *Requester) SetHTTPClient(newClient *http.Client) error

SetHTTPClient sets exchanges HTTP client

func (*Requester) SetHTTPClientTimeout

func (r *Requester) SetHTTPClientTimeout(timeout time.Duration) error

SetHTTPClientTimeout sets the timeout value for the exchanges HTTP Client and also the underlying transports idle connection timeout

func (*Requester) SetHTTPClientUserAgent

func (r *Requester) SetHTTPClientUserAgent(userAgent string) error

SetHTTPClientUserAgent sets the exchanges HTTP user agent

func (*Requester) SetProxy

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

SetProxy sets a proxy address for the client transport

func (*Requester) Shutdown

func (r *Requester) Shutdown() error

Shutdown releases persistent memory for garbage collection.

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(def RateLimitDefinitions) RequesterOption

WithLimiter configures the rate limiter for a Requester.

func WithReporter

func WithReporter(rep Reporter) RequesterOption

WithReporter configures the reporter for a Requester.

func WithRetryPolicy

func WithRetryPolicy(p RetryPolicy) RequesterOption

WithRetryPolicy configures the retry policy for a Requester.

type Reservations

type Reservations []*rate.Reservation

Reservations is a slice of rate reservations

func (Reservations) CancelAll

func (r Reservations) CancelAll()

CancelAll cancels all potential reservations to free up rate limiter for context cancellations and deadline exceeded cases.

type RetryPolicy

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

RetryPolicy determines whether the request should be retried.

type Weight

type Weight uint8

Weight defines the number of reservations to be used. This is a generalised weight for rate limiting. e.g. n weight = n request. i.e. 50 Weight = 50 requests.

Jump to

Keyboard shortcuts

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