-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
190 changed files
with
11,617 additions
and
1,566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !1.13 | ||
// +build !1.13 | ||
|
||
package client | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var DefaultTransport http.RoundTripper = &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: defaultDialFunc, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build 1.13 | ||
// +build 1.13 | ||
|
||
package client | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var DefaultTransport http.RoundTripper = &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: defaultDialFunc, | ||
ForceAttemptHTTP2: true, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build !unit && !integration | ||
// +build !unit,!integration | ||
|
||
package client | ||
|
||
const testRuntime = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build unit || integration | ||
// +build unit integration | ||
|
||
package client | ||
|
||
const testRuntime = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
) | ||
|
||
type ( | ||
resolverContextKey struct{} | ||
dialTimeoutContextKey struct{} | ||
keepAliveIntervalContextKey struct{} | ||
resolverContextValue struct { | ||
domain string | ||
ips []net.IP | ||
} | ||
) | ||
|
||
func defaultDialFunc(ctx context.Context, network string, address string) (net.Conn, error) { | ||
host, port, err := net.SplitHostPort(address) | ||
if err != nil { | ||
host = address | ||
} | ||
|
||
dialTimeout, ok := ctx.Value(dialTimeoutContextKey{}).(time.Duration) | ||
if !ok { | ||
dialTimeout = 30 * time.Second | ||
} | ||
keepAliveInterval, ok := ctx.Value(keepAliveIntervalContextKey{}).(time.Duration) | ||
if !ok { | ||
keepAliveInterval = 15 * time.Second | ||
} | ||
if resolved, ok := ctx.Value(resolverContextKey{}).(resolverContextValue); ok && len(resolved.ips) > 0 && resolved.domain == host { | ||
dialer := net.Dialer{Timeout: dialTimeout / time.Duration(len(resolved.ips)), KeepAlive: keepAliveInterval} | ||
for _, ip := range resolved.ips { | ||
newAddr := ip.String() | ||
if port != "" { | ||
newAddr = net.JoinHostPort(newAddr, port) | ||
} | ||
if conn, err := dialer.DialContext(ctx, network, newAddr); err == nil { | ||
return conn, nil | ||
} | ||
} | ||
} | ||
return (&net.Dialer{Timeout: dialTimeout, KeepAlive: keepAliveInterval}).DialContext(ctx, network, address) | ||
} | ||
|
||
func WithResolvedIPs(ctx context.Context, domain string, ips []net.IP) context.Context { | ||
return context.WithValue(ctx, resolverContextKey{}, resolverContextValue{domain: domain, ips: ips}) | ||
} | ||
|
||
func WithDialTimeout(ctx context.Context, timeout time.Duration) context.Context { | ||
return context.WithValue(ctx, dialTimeoutContextKey{}, timeout) | ||
} | ||
|
||
func WithKeepAliveInterval(ctx context.Context, interval time.Duration) context.Context { | ||
return context.WithValue(ctx, keepAliveIntervalContextKey{}, interval) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build unit | ||
// +build unit | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestDefaultDialer(t *testing.T) { | ||
var responseBody struct { | ||
Status string `json:"status"` | ||
} | ||
mux := http.NewServeMux() | ||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Type", "application/json") | ||
w.Write([]byte(`{"status":"ok"}`)) | ||
})) | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
port := server.Listener.Addr().(*net.TCPAddr).Port | ||
|
||
ctx := WithResolvedIPs(context.Background(), "www.qiniu.com", []net.IP{net.IPv4(127, 0, 0, 1)}) | ||
err := DefaultClient.Call(ctx, &responseBody, http.MethodGet, fmt.Sprintf("http://www.qiniu.com:%d/", port), nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if responseBody.Status != "ok" { | ||
t.Fatal("unexpected response") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.