-
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
11 changed files
with
181 additions
and
34 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
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,64 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type ( | ||
resolverContextKey struct{} | ||
dialTimeoutContextKey struct{} | ||
keepAliveIntervalContextKey struct{} | ||
resolverValues struct { | ||
ips []net.IP | ||
domain string | ||
} | ||
) | ||
|
||
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{}).(resolverValues); ok { | ||
if resolved.domain == host && len(resolved.ips) > 0 { | ||
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) | ||
} | ||
fmt.Printf("******* 1: dialer.DialContext(%s, %s), address = %s\n", network, newAddr, address) | ||
if conn, err := dialer.DialContext(ctx, network, newAddr); err == nil { | ||
return conn, nil | ||
} | ||
} | ||
} else { | ||
fmt.Printf("******* 2: resolved = %#v, host = %s\n", resolved, host) | ||
} | ||
} | ||
fmt.Printf("******* 3: dialer.DialContext(%s, %s)\n", network, address) | ||
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{}, resolverValues{ips: ips, domain: domain}) | ||
} | ||
|
||
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
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
2 changes: 1 addition & 1 deletion
2
storagev2/http_client/resolver.go → storagev2/resolver/resolver.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package http_client | ||
package resolver | ||
|
||
import ( | ||
"context" | ||
|
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