Esempio n. 1
0
func main() {
	url, _ := url.Parse("http://www.example.org")
	port := "80"
	initialConnectionPool := 1
	maxConnectionsInPool := 3

	dialer, err := redial.New(net.Dialer{}, url.Host, port, initialConnectionPool, maxConnectionsInPool)
	if err != nil {
		fmt.Println("redial.New error", err)
		return
	}

	transport := &http.Transport{
		Dial: dialer.Dial,
	}

	request, err := http.NewRequest("GET", url.String(), nil)
	if err != nil {
		fmt.Println("http.NewRequest", err)
		return
	}

	response, err := transport.RoundTrip(request)
	if err != nil {
		fmt.Println("transport.RoundTrip", err)
		return
	}
	defer response.Body.Close()

	fmt.Println("Response status", response.Status, "for", url)
}
Esempio n. 2
0
// New - returns Request Client
func New(op *Options, address, port string) (r *Client, err error) {
	if op == nil {
		op = NewOptions()
	}
	dialer, err := redial.New(net.Dialer{
		Timeout:   op.DialerTimeout,
		Deadline:  op.DialerDeadline,
		KeepAlive: op.DialerKeepAlive,
		DualStack: op.DialerDualStack,
	}, address, port, op.ConnPoolInitial, op.ConnPoolMax)
	if err != nil {
		return nil, err
	}
	r = &Client{
		RequestProto:      RequestProto,
		RequestProtoMinor: RequestProtoMinor,
		RequestProtoMajor: RequestProtoMajor,
		TLS: &tls.Config{
			InsecureSkipVerify: op.TLSInsecureSkipVerify,
		},
		ClientOptions: op, // Setting rehttp.Client options
	}

	// Setting up TRANSPORT
	r.Transport = &httpcontrol.Transport{
		Dial:                  dialer.Dial,
		TLSClientConfig:       r.TLS,
		MaxTries:              op.TransportMaxTries,
		DisableKeepAlives:     op.TransportDisableKeepAlives,
		DisableCompression:    op.TransportDisableCompression,
		MaxIdleConnsPerHost:   op.TransportMaxIdleConnsPerHost,
		RequestTimeout:        op.TransportRequestTimeout,
		ResponseHeaderTimeout: op.TransportResponseHeaderTimeout,
	}

	// Setting up CLIENT, higher level API of TRANSPORT
	r.Client = &http.Client{
		Transport: r.Transport,
		Timeout:   op.ClientTimeout,
	}
	return r, nil
}