client

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package client contains a simple implementation of HTTP 1 client which returns a streaming body.

By default fasthttp puts whole response into the memory. This is fine for small responses but if we use this library to stream huge files, this can cause a lot of problems.

WHen fasthttp will introduce support of streaming bodies, this package should be considered as obsolete.

Index

Constants

View Source
const (
	// DefaultHTTPTimeout is a default timeout for processing of a single
	// HTTP request.
	DefaultHTTPTimeout = 3 * time.Minute

	// DefaultHTTPPort is a default port for HTTP.
	DefaultHTTPPort = "80"

	// DefaultHTTPSPort is a default port for HTTPS.
	DefaultHTTPSPort = "443"
)
View Source
const (
	// DefaultDialTimeout is a  timeout for dialing to the host
	DefaultDialTimeout = 20 * time.Second

	// DefaultLimit constraints an amount of connections to the hostname
	// for PooledDialer.
	DefaultLimit = 1024
)

Variables

This section is empty.

Functions

func FastHTTPBaseDialer

func FastHTTPBaseDialer(addr string, dialTimeout time.Duration) (net.Conn, error)

FastHTTPBaseDialer is a BaseDialer which uses fasthttp.

func StdBaseDialer

func StdBaseDialer(addr string, dialTimeout time.Duration) (net.Conn, error)

StdBaseDialer is a BaseDialer which uses net.Dial.

Types

type BaseDialer

type BaseDialer func(string, time.Duration) (net.Conn, error)

BaseDialer connects to the hostname.

type Client

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

Client is the implementation of HTTP1 client which sets body as a stream.

func NewClient

func NewClient(dialer Dialer) *Client

NewClient creates a new instance of HTTP1 client.

func (*Client) Do

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

Do does HTTP request with the default timeout.

func (*Client) DoTimeout

func (c *Client) DoTimeout(req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error

DoTimeout does HTTP request with the given timeout.

type Dialer

type Dialer interface {
	// Dial establishes connection to the given host.
	Dial(string) (net.Conn, error)

	// Release returns net.Conn back to the client. Second parameter is
	// hostname which was used for Dial.
	Release(net.Conn, string)

	// NotifyClosed tells Dialer that net.Conn for address is broken.
	NotifyClosed(string)
}

Dialer is a common interface for data structure which dials to hostnames. It's main purpose is to release connections when necessary or notify that some net.Conn is broken.

type PooledDialer

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

PooledDialer is a dialer which manages pools of connections to different hosts.

func NewPooledDialer

func NewPooledDialer(dialer BaseDialer, timeout time.Duration, limit int) (*PooledDialer, error)

NewPooledDialer creates new instance of PooledDialer.

func (*PooledDialer) Dial

func (d *PooledDialer) Dial(addr string) (net.Conn, error)

Dial establishes connection to the given host.

func (*PooledDialer) NotifyClosed

func (d *PooledDialer) NotifyClosed(addr string)

NotifyClosed tells Dialer that net.Conn for address is broken.

func (*PooledDialer) Release

func (d *PooledDialer) Release(conn net.Conn, addr string)

Release returns net.Conn back to the client. Second parameter is hostname which was used for Dial.

func (*PooledDialer) Run

func (d *PooledDialer) Run()

Run runs background worker.

type SimpleDialer

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

SimpleDialer is a dialer which always closes connections when they are not needed. It does not do any pooling.

func NewSimpleDialer

func NewSimpleDialer(base BaseDialer, timeout time.Duration) (*SimpleDialer, error)

NewSimpleDialer returns new instance of SimpleDialer.

func (*SimpleDialer) Dial

func (s *SimpleDialer) Dial(addr string) (net.Conn, error)

Dial establishes connection to the given host.

func (*SimpleDialer) NotifyClosed

func (s *SimpleDialer) NotifyClosed(_ string)

NotifyClosed tells Dialer that net.Conn for address is broken.

func (*SimpleDialer) Release

func (s *SimpleDialer) Release(conn net.Conn, _ string)

Release returns net.Conn back to the client. Second parameter is hostname which was used for Dial.

Jump to

Keyboard shortcuts

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