Skip to content

Commit 9d98ac7

Browse files
authored
Releases/v7.21.0 (#128)
1 parent 47dfe16 commit 9d98ac7

File tree

190 files changed

+11617
-1566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+11617
-1566
lines changed

.github/workflows/ci-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
GOPATH=$GITHUB_WORKSPACE go get golang.org/x/sync/singleflight
2626
GOPATH=$GITHUB_WORKSPACE go get github.com/qiniu/dyn
2727
GOPATH=$GITHUB_WORKSPACE go get github.com/gofrs/flock
28+
GOPATH=$GITHUB_WORKSPACE go get github.com/alex-ant/gomath/rational
29+
GOPATH=$GITHUB_WORKSPACE go get github.com/matishsiao/goInfo
2830
2931
# FIXME special package
3032
# github.com/go-playground/validator/v10
@@ -54,6 +56,7 @@ jobs:
5456
GOPATH=$GITHUB_WORKSPACE go get github.com/davecgh/go-spew/spew
5557
GOPATH=$GITHUB_WORKSPACE go get github.com/pmezard/go-difflib/difflib
5658
GOPATH=$GITHUB_WORKSPACE go get gopkg.in/yaml.v3
59+
rm -rf $GITHUB_WORKSPACE/src/github.com/BurntSushi/toml && git clone -b v0.3.1 --depth 1 https://github.com/BurntSushi/toml.git $GITHUB_WORKSPACE/src/github.com/BurntSushi/toml
5760
5861
GOPATH=$GITHUB_WORKSPACE make unittest
5962
working-directory: src/github.com/qiniu/go-sdk

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ test:
44
unittest:
55
go test -tags=unit -failfast -v -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'`
66

7+
integrationtest:
8+
go test -tags=integration -failfast -parallel 1 -v -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'`
9+
710
staticcheck:
811
staticcheck -go 1.10 `go list ./... | egrep -v 'examples|sms'`
912

auth/credentials.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
api "github.com/qiniu/go-sdk/v7"
1515
"github.com/qiniu/go-sdk/v7/conf"
1616
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
17+
"github.com/qiniu/go-sdk/v7/storagev2/defaults"
1718
)
1819

1920
const (
@@ -32,18 +33,39 @@ type Credentials struct {
3233
SecretKey []byte
3334
}
3435

35-
// 构建一个Credentials对象
36+
// New 构建一个Credentials对象
3637
func New(accessKey, secretKey string) *Credentials {
3738
return &Credentials{accessKey, []byte(secretKey)}
3839
}
3940

41+
// Default 构建默认的 Credentials 对象
42+
func Default() *Credentials {
43+
accessKey, secretKey, err := defaults.Credentials()
44+
if err == nil && accessKey != "" && secretKey != "" {
45+
return New(accessKey, secretKey)
46+
}
47+
return nil
48+
}
49+
4050
// Sign 对数据进行签名,一般用于私有空间下载用途
4151
func (ath *Credentials) Sign(data []byte) (token string) {
42-
h := hmac.New(sha1.New, ath.SecretKey)
52+
var (
53+
accessKey string
54+
secretKey []byte
55+
)
56+
if ath == nil {
57+
if cred := Default(); cred != nil {
58+
accessKey = cred.AccessKey
59+
secretKey = cred.SecretKey
60+
}
61+
} else {
62+
accessKey, secretKey = ath.AccessKey, ath.SecretKey
63+
}
64+
h := hmac.New(sha1.New, secretKey)
4365
h.Write(data)
4466

4567
sign := base64.URLEncoding.EncodeToString(h.Sum(nil))
46-
return fmt.Sprintf("%s:%s", ath.AccessKey, sign)
68+
return fmt.Sprintf("%s:%s", accessKey, sign)
4769
}
4870

4971
// SignToken 根据t的类型对请求进行签名,并把token加入req中
@@ -74,8 +96,16 @@ func (ath *Credentials) SignWithData(b []byte) (token string) {
7496

7597
// IsIAMKey 判断AccessKey是否为IAM的Key
7698
func (ath *Credentials) IsIAMKey() bool {
77-
return len(ath.AccessKey) == IAMKeyLen*4/3 &&
78-
strings.HasPrefix(ath.AccessKey, IAMKeyPrefix)
99+
var accessKey string
100+
if ath == nil {
101+
if cred := Default(); cred != nil {
102+
accessKey = cred.AccessKey
103+
}
104+
} else {
105+
accessKey = ath.AccessKey
106+
}
107+
return len(accessKey) == IAMKeyLen*4/3 &&
108+
strings.HasPrefix(accessKey, IAMKeyPrefix)
79109
}
80110

81111
func collectData(req *http.Request) (data []byte, err error) {

cdn/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type CdnManager struct {
2424

2525
// NewCdnManager 用来构建一个新的 CdnManager
2626
func NewCdnManager(mac *auth.Credentials) *CdnManager {
27+
if mac == nil {
28+
mac = auth.Default()
29+
}
2730
return &CdnManager{mac: mac}
2831
}
2932

client/client.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ import (
2121
"github.com/qiniu/go-sdk/v7/reqid"
2222
)
2323

24-
var UserAgent = getUserAgentWithAppName("default")
25-
var DefaultClient = Client{
26-
&http.Client{
27-
Transport: http.DefaultTransport,
28-
},
29-
}
24+
var (
25+
UserAgent = getUserAgentWithAppName("default")
26+
DefaultClient = Client{&http.Client{Transport: DefaultTransport}}
3027

31-
// 用来打印调试信息
32-
var DebugMode = false
33-
var DeepDebugInfo = false
28+
// 用来打印调试信息
29+
DebugMode = false
30+
DeepDebugInfo = false
31+
)
3432

3533
// --------------------------------------------------------------------
3634

@@ -51,8 +49,12 @@ func SetAppName(userApp string) error {
5149
}
5250

5351
func getUserAgentWithAppName(userApp string) string {
54-
return fmt.Sprintf("QiniuGo/%s (%s; %s; %s) %s",
55-
conf.Version, runtime.GOOS, runtime.GOARCH, userApp, runtime.Version())
52+
userAgentPrefix := "QiniuGo/"
53+
if testRuntime {
54+
userAgentPrefix = "QiniuGo_Debug/"
55+
}
56+
return fmt.Sprintf("%s%s (%s; %s; %s) %s",
57+
userAgentPrefix, conf.Version, runtime.GOOS, runtime.GOARCH, userApp, runtime.Version())
5658
}
5759

5860
// --------------------------------------------------------------------

client/client_1.12.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build !1.13
2+
// +build !1.13
3+
4+
package client
5+
6+
import (
7+
"net/http"
8+
"time"
9+
)
10+
11+
var DefaultTransport http.RoundTripper = &http.Transport{
12+
Proxy: http.ProxyFromEnvironment,
13+
DialContext: defaultDialFunc,
14+
MaxIdleConns: 100,
15+
IdleConnTimeout: 90 * time.Second,
16+
TLSHandshakeTimeout: 10 * time.Second,
17+
ExpectContinueTimeout: 1 * time.Second,
18+
}

client/client_1.13.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build 1.13
2+
// +build 1.13
3+
4+
package client
5+
6+
import (
7+
"net/http"
8+
"time"
9+
)
10+
11+
var DefaultTransport http.RoundTripper = &http.Transport{
12+
Proxy: http.ProxyFromEnvironment,
13+
DialContext: defaultDialFunc,
14+
ForceAttemptHTTP2: true,
15+
MaxIdleConns: 100,
16+
IdleConnTimeout: 90 * time.Second,
17+
TLSHandshakeTimeout: 10 * time.Second,
18+
ExpectContinueTimeout: 1 * time.Second,
19+
}

client/client_prod_env.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build !unit && !integration
2+
// +build !unit,!integration
3+
4+
package client
5+
6+
const testRuntime = false

client/client_test_env.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build unit || integration
2+
// +build unit integration
3+
4+
package client
5+
6+
const testRuntime = true

client/dialer.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net"
6+
"time"
7+
)
8+
9+
type (
10+
resolverContextKey struct{}
11+
dialTimeoutContextKey struct{}
12+
keepAliveIntervalContextKey struct{}
13+
resolverContextValue struct {
14+
domain string
15+
ips []net.IP
16+
}
17+
)
18+
19+
func defaultDialFunc(ctx context.Context, network string, address string) (net.Conn, error) {
20+
host, port, err := net.SplitHostPort(address)
21+
if err != nil {
22+
host = address
23+
}
24+
25+
dialTimeout, ok := ctx.Value(dialTimeoutContextKey{}).(time.Duration)
26+
if !ok {
27+
dialTimeout = 30 * time.Second
28+
}
29+
keepAliveInterval, ok := ctx.Value(keepAliveIntervalContextKey{}).(time.Duration)
30+
if !ok {
31+
keepAliveInterval = 15 * time.Second
32+
}
33+
if resolved, ok := ctx.Value(resolverContextKey{}).(resolverContextValue); ok && len(resolved.ips) > 0 && resolved.domain == host {
34+
dialer := net.Dialer{Timeout: dialTimeout / time.Duration(len(resolved.ips)), KeepAlive: keepAliveInterval}
35+
for _, ip := range resolved.ips {
36+
newAddr := ip.String()
37+
if port != "" {
38+
newAddr = net.JoinHostPort(newAddr, port)
39+
}
40+
if conn, err := dialer.DialContext(ctx, network, newAddr); err == nil {
41+
return conn, nil
42+
}
43+
}
44+
}
45+
return (&net.Dialer{Timeout: dialTimeout, KeepAlive: keepAliveInterval}).DialContext(ctx, network, address)
46+
}
47+
48+
func WithResolvedIPs(ctx context.Context, domain string, ips []net.IP) context.Context {
49+
return context.WithValue(ctx, resolverContextKey{}, resolverContextValue{domain: domain, ips: ips})
50+
}
51+
52+
func WithDialTimeout(ctx context.Context, timeout time.Duration) context.Context {
53+
return context.WithValue(ctx, dialTimeoutContextKey{}, timeout)
54+
}
55+
56+
func WithKeepAliveInterval(ctx context.Context, interval time.Duration) context.Context {
57+
return context.WithValue(ctx, keepAliveIntervalContextKey{}, interval)
58+
}

0 commit comments

Comments
 (0)