Skip to content

Commit

Permalink
add session options
Browse files Browse the repository at this point in the history
  • Loading branch information
wnanbei committed Sep 28, 2019
1 parent 4b49f34 commit 956d9ef
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 34 deletions.
4 changes: 3 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package direwolf
// method.
func Request(reqSetting *RequestSetting) (*Response, error) {
session := NewSession()
session.transport.DisableKeepAlives = true
sessionOptions := DefaultSessionOptions()
sessionOptions.DisableDialKeepAlives = true
sessionOptions.DisableCookieJar = true
resp, err := session.Request(reqSetting)
if err != nil {
return nil, err
Expand Down
17 changes: 10 additions & 7 deletions datatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ func TestHeaders(t *testing.T) {
}

//func TestParams(t *testing.T) {
// params := NewParams("xxx", "yyy")
// proxy := &Proxy{
// HTTPS: "xxx",
// HTTP: "xxx",
// }
// req := NewRequestSetting("Post", "http://www.baidu.com", params, proxy)
// fmt.Printf("%+v", req)
// //params := NewParams("xxx", "yyy")
// //proxy := &Proxy{
// // HTTPS: "xxx",
// // HTTP: "xxx",
// //}
// //req := NewRequestSetting("Post", "http://www.baidu.com", params, proxy)
// //fmt.Printf("%+v", req)
// option := &SessionOptions{}
// *option.MaxIdleConns = 10
// fmt.Print(option)
//}

func TestStringSliceMap(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

var (
ErrRequestBody = errors.New("request body can`t coexists with PostForm")

)

type RedirectError struct {
Expand All @@ -23,8 +22,8 @@ func (e *RedirectError) Error() string {

type Error struct {
// wrapped error
err error
msg string
err error
msg string
// file path and name
file string
fileLine int
Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestError(t *testing.T) {
err1 := ErrRequestBody
err2 := WrapErr(err1, "second testing")
err3 := WrapErr(err2, "third testing")
err3 := WrapErrf(err2, "==%s==", "third testing")

if !errors.Is(err3, ErrRequestBody) {
t.Fatal("Test errors.Is failed.")
Expand Down
140 changes: 119 additions & 21 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,39 +120,137 @@ func (session *Session) DisableCookieJar() {
}

// NewSession new a Session object, and set a default Client and Transport.
func NewSession() *Session {
defaultTransport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
func NewSession(options ...*SessionOptions) *Session {
var sessionOptions *SessionOptions
if len(options) > 0 {
sessionOptions = options[0]
} else {
sessionOptions = DefaultSessionOptions()
}

// set CookieJar
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
trans := &http.Transport{
DialContext: (&net.Dialer{
Timeout: sessionOptions.DialTimeout,
KeepAlive: sessionOptions.DialKeepAlive,
}).DialContext,
MaxIdleConns: sessionOptions.MaxIdleConns,
MaxIdleConnsPerHost: sessionOptions.MaxIdleConnsPerHost,
MaxConnsPerHost: sessionOptions.MaxConnsPerHost,
IdleConnTimeout: sessionOptions.IdleConnTimeout,
TLSHandshakeTimeout: sessionOptions.TLSHandshakeTimeout,
ExpectContinueTimeout: sessionOptions.ExpectContinueTimeout,
}
jar, err := cookiejar.New(&options)
if err != nil {
return nil
if sessionOptions.DisableDialKeepAlives {
trans.DisableKeepAlives = true
}

client := &http.Client{
Transport: defaultTransport,
Jar: jar,
client := &http.Client{Transport: trans}

// set CookieJar
if sessionOptions.DisableCookieJar == false {
cookieJarOptions := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&cookieJarOptions)
if err != nil {
return nil
}
client.Jar = jar
}

// Set default user agent
headers := http.Header{}
headers.Add("User-Agent", "direwolf - winter is coming")

return &Session{
client: client,
transport: defaultTransport,
transport: trans,
Headers: headers,
}
}

type SessionOptions struct {
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. If Deadline is also set, it may fail
// earlier.
//
// The default is 30.
//
// When using TCP and dialing a host name with multiple IP
// addresses, the timeout may be divided between them.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
DialTimeout time.Duration

// KeepAlive specifies the interval between keep-alive
// probes for an active network connection.
// If zero, keep-alive probes are sent with a default value
// (currently 15 seconds), if supported by the protocol and operating
// system. Network protocols or operating systems that do
// not support keep-alives ignore this field.
// If negative, keep-alive probes are disabled.
DialKeepAlive time.Duration

// MaxConnsPerHost optionally limits the total number of
// connections per host, including connections in the dialing,
// active, and idle states. On limit violation, dials will block.
//
// Zero means no limit.
MaxConnsPerHost int

// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int

// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int

// IdleConnTimeout is the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// Zero means no limit.
IdleConnTimeout time.Duration

// TLSHandshakeTimeout specifies the maximum amount of time waiting to
// wait for a TLS handshake. Zero means no timeout.
TLSHandshakeTimeout time.Duration

// ExpectContinueTimeout, if non-zero, specifies the amount of
// time to wait for a server's first response headers after fully
// writing the request headers if the request has an
// "Expect: 100-continue" header. Zero means no timeout and
// causes the body to be sent immediately, without
// waiting for the server to approve.
// This time does not include the time to send the request header.
ExpectContinueTimeout time.Duration

// Whether disable session cookiejar.
// Default value is false
DisableCookieJar bool

// DisableDialKeepAlives, if true, disables HTTP keep-alives and
// will only use the connection to the server for a single
// HTTP request.
//
// This is unrelated to the similarly named TCP keep-alives.
DisableDialKeepAlives bool
}

func DefaultSessionOptions() *SessionOptions {
return &SessionOptions{
DialTimeout: 30 * time.Second,
DialKeepAlive: 30 * time.Second,
MaxConnsPerHost: 0,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCookieJar: false,
DisableDialKeepAlives: false,
}
}
2 changes: 1 addition & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestSessionCookies(t *testing.T) {
t.Fatal(err)
}
cookies := session.Cookies(ts.URL)
if cookies[0].Name != "key"{
if cookies[0].Name != "key" {
t.Fatal("Session.Cookies() failed.")
}
t.Log("Session.Cookies() passed.")
Expand Down

0 comments on commit 956d9ef

Please sign in to comment.