httpclient

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 4 Imported by: 0

README

http-client

Simple golang module with fasthttp client and prometheus metric.

Example

// metric
var NetSourcesLatencyHistogram = func() *prometheus.HistogramVec {
    var metric = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Namespace: "service",
            Subsystem: "net",
            Name:      "sources_latency",
            Help:      "Third party response latency histogram.",
            Buckets:   prometheus.ExponentialBuckets(0.05, 2, 8),
        }, []string{"source"})
    prometheus.MustRegister(metric)

    return metric
}()

// create
client := httpclient.NewWithMetric("example", metrics.NetSourcesLatencyHistogram)

// request
err := p.client.DoTimeout(req, resp)
if err != nil {
    // error handling
}

Documentation

Overview

Package httpclient provides easy way to crate http client with retry and timeout.

Index

Constants

View Source
const (
	// DefaultRetry - количество ретраев под текущую реализацию переключения между основными api и failover.
	DefaultRetry = 2
	// DefaultTimeout - стандартное время ожидания ответа от сервера.
	DefaultTimeout = 3 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff struct {
	Min       time.Duration
	Max       time.Duration
	Calculace func(int) time.Duration
}

Backoff - provide essention backoff parameters.

func NewBackoff

func NewBackoff(min, max time.Duration) *Backoff

NewBackoff - return function for new backoff calculation.

type Client

type Client struct {
	RetryMax int              // Количество попыток запроса.
	Timeout  time.Duration    // Таймаут для http запроса.
	Backoff  *Backoff         // Рассчёт времени ожидания между попытками запроса.
	HTTP     *fasthttp.Client // HTTP клиент.
	// contains filtered or unexported fields
}

Client http.

func New

func New(retry int, timeout time.Duration) Client

New - return new http client with given params & backoff.

func NewDefault

func NewDefault() Client

NewDefault - return new http client with default params & backoff.

func NewWithMetric

func NewWithMetric(domain string, latencyMetric *prometheus.HistogramVec) Client

NewDefault - return new http client with default params & backoff.

func (*Client) DoTimeout

func (c *Client) DoTimeout(req *fasthttp.Request, resp *fasthttp.Response) (err error)

Jump to

Keyboard shortcuts

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