mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
6ed7f6bbf7
Add a multifunctional `Dialer` struct and reimplement the function API Reimplement the existing function interfaces of the fasthttpproxy package through Dialer. Refactor Dialer.GetDialFunc to ensure that its performance is comparable to the original function interfaces when the proxy does not change with the request address.
36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package fasthttpproxy
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
"golang.org/x/net/http/httpproxy"
|
|
)
|
|
|
|
// FasthttpHTTPDialer returns a fasthttp.DialFunc that dials using
|
|
// the provided HTTP proxy.
|
|
//
|
|
// Example usage:
|
|
//
|
|
// c := &fasthttp.Client{
|
|
// Dial: fasthttpproxy.FasthttpHTTPDialer("username:password@localhost:9050"),
|
|
// }
|
|
func FasthttpHTTPDialer(proxy string) fasthttp.DialFunc {
|
|
return FasthttpHTTPDialerTimeout(proxy, 0)
|
|
}
|
|
|
|
// FasthttpHTTPDialerTimeout returns a fasthttp.DialFunc that dials using
|
|
// the provided HTTP proxy using the given timeout.
|
|
// The timeout parameter determines both the dial timeout and the CONNECT request timeout.
|
|
//
|
|
// Example usage:
|
|
//
|
|
// c := &fasthttp.Client{
|
|
// Dial: fasthttpproxy.FasthttpHTTPDialerTimeout("username:password@localhost:9050", time.Second * 2),
|
|
// }
|
|
func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.DialFunc {
|
|
d := Dialer{Config: httpproxy.Config{HTTPProxy: proxy, HTTPSProxy: proxy}, Timeout: timeout, ConnectTimeout: timeout}
|
|
dialFunc, _ := d.GetDialFunc(false)
|
|
return dialFunc
|
|
}
|