Skip to content

Commit

Permalink
fix-add-maxconnsperhost
Browse files Browse the repository at this point in the history
  • Loading branch information
铭毅 authored and kkuai committed May 13, 2022
1 parent b63f1a2 commit 287e287
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,20 @@ func Timeout(connectTimeoutSec, readWriteTimeout int64) ClientOption {
}
}

// MaxConns sets the HTTP max connections for a client.
//
// maxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts. Default is 100.
// maxIdleConnsPerHost controls the maximum idle (keep-alive) connections to keep per-host. Default is 100.
// maxConnsPerHost limits the total number of connections per host. Default is no limit.
//
func MaxConns(maxIdleConns, maxIdleConnsPerHost, maxConnsPerHost int) ClientOption {
return func(client *Client) {
client.Config.HTTPMaxConns.MaxIdleConns = maxIdleConns
client.Config.HTTPMaxConns.MaxIdleConnsPerHost = maxIdleConnsPerHost
client.Config.HTTPMaxConns.MaxConnsPerHost = maxConnsPerHost
}
}

// SecurityToken sets the temporary user's SecurityToken.
//
// token STS token
Expand Down
28 changes: 28 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,34 @@ func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
c.Assert(err, IsNil)
}

func (s *OssClientSuite) TestMaxConnsPutObjectSuccess(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucketName := bucketNamePrefix + RandLowStr(5)
err = client.CreateBucket(bucketName)
c.Assert(err, IsNil)

bucket, err := client.Bucket(bucketName)
c.Assert(err, IsNil)

c.Assert(bucket.GetConfig().HTTPMaxConns.MaxIdleConns, Equals, 100)
c.Assert(bucket.GetConfig().HTTPMaxConns.MaxIdleConnsPerHost, Equals, 100)
c.Assert(bucket.GetConfig().HTTPMaxConns.MaxConnsPerHost, Equals, 0)

// put object
objectName := objectNamePrefix + RandLowStr(5)
err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
c.Assert(err, IsNil)

bucket.DeleteObject(objectName)
err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
c.Assert(err, IsNil)
bucket.DeleteObject(objectName)

client.DeleteBucket(bucketName)
}

func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)
Expand Down
1 change: 1 addition & 0 deletions oss/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type HTTPTimeout struct {
type HTTPMaxConns struct {
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
}

// CredentialInf is interface for get AccessKeyID,AccessKeySecret,SecurityToken
Expand Down
1 change: 1 addition & 0 deletions oss/transport_1_7.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newTransport(conn *Conn, config *Config) *http.Transport {
},
MaxIdleConns: httpMaxConns.MaxIdleConns,
MaxIdleConnsPerHost: httpMaxConns.MaxIdleConnsPerHost,
MaxConnsPerHost: httpMaxConns.MaxConnsPerHost,
IdleConnTimeout: httpTimeOut.IdleConnTimeout,
ResponseHeaderTimeout: httpTimeOut.HeaderTimeout,
}
Expand Down

0 comments on commit 287e287

Please sign in to comment.