Skip to content

Commit

Permalink
feat: optimize GetObjectMetadatasInput validate in dfstore (#2686)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Aug 30, 2023
1 parent 79095f6 commit 9c9959f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
20 changes: 19 additions & 1 deletion client/dfstore/dfstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ import (
pkgobjectstorage "d7y.io/dragonfly/v2/pkg/objectstorage"
)

const (
// DefaultGetObjectMetadatasLimit is the default limit of get object metadatas.
DefaultGetObjectMetadatasLimit = 1000

// MaxGetObjectMetadatasLimit is the max limit of get object metadatas.
MaxGetObjectMetadatasLimit = 1000
)

// Dfstore is the interface used for object storage.
type Dfstore interface {
// CreateBucketRequestWithContext returns *http.Request of create bucket.
Expand Down Expand Up @@ -226,13 +234,20 @@ type GetObjectMetadatasInput struct {
Limit int64
}

// Convert converts GetObjectMetadatasInput fields.
func (i *GetObjectMetadatasInput) Convert() {
if i.Limit == 0 {
i.Limit = DefaultGetObjectMetadatasLimit
}
}

// Validate validates GetObjectMetadatasInput fields.
func (i *GetObjectMetadatasInput) Validate() error {
if i.BucketName == "" {
return errors.New("invalid BucketName")
}

if i.Limit < 0 || i.Limit > pkgobjectstorage.DefaultGetObjectMetadatasLimit {
if i.Limit <= 0 || i.Limit > MaxGetObjectMetadatasLimit {
return errors.New("invalid limit")
}

Expand All @@ -241,6 +256,9 @@ func (i *GetObjectMetadatasInput) Validate() error {

// GetObjectMetadatasRequestWithContext returns *http.Request of getting object metadatas.
func (dfs *dfstore) GetObjectMetadatasRequestWithContext(ctx context.Context, input *GetObjectMetadatasInput) (*http.Request, error) {
// Convert input fields.
input.Convert()

if err := input.Validate(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions client/dfstore/mocks/dfstore_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions pkg/objectstorage/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package objectstorage

import "time"

const (
// ServiceNameS3 is name of s3 storage.
ServiceNameS3 = "s3"
Expand Down Expand Up @@ -55,12 +57,38 @@ const (
MethodList Method = "List"
)

const (
// DefaultS3ForcePathStyle is the default force path style of s3.
DefaultS3ForcePathStyle = true
)

const (
// OBSStorageClassStandardIA is the standard ia storage class of obs.
OBSStorageClassStandardIA = "STANDARD_IA"
)

const (
// DefaultGetObjectMetadatasLimit is the default limit of get object metadatas.
DefaultGetObjectMetadatasLimit = 1000
// DefaultTLSHandshakeTimeout is the default timeout of tls handshake of http client.
DefaultTLSHandshakeTimeout = 30 * time.Second

// DefaultResponseHeaderTimeout is the default timeout of response header of http client.
DefaultResponseHeaderTimeout = 30 * time.Second

// DefaultIdleConnTimeout is the default timeout of idle connection of http client.
DefaultIdleConnTimeout = 5 * time.Minute

// DefaultMaxIdleConnsPerHost is the default max idle connections per host of http client.
DefaultMaxIdleConnsPerHost = 500

// DefaultReadBufferSize is the default read buffer size of http client.
DefaultReadBufferSize = 32 << 10

// DefaultWriteBufferSize is the default write buffer size of http client.
DefaultWriteBufferSize = 32 << 10

// DefaultDisableCompression is the default disable compression of http client.
DefaultDisableCompression = true

// DefaultTimeout is the default timeout of http client.
DefaultTimeout = time.Hour
)
21 changes: 9 additions & 12 deletions pkg/objectstorage/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package objectstorage

import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -169,20 +168,18 @@ func New(name, region, endpoint, accessKey, secretKey string, options ...Option)
endpoint: endpoint,
accessKey: accessKey,
secretKey: secretKey,
s3ForcePathStyle: true,
s3ForcePathStyle: DefaultS3ForcePathStyle,
httpClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: time.Second * 20,
ResponseHeaderTimeout: time.Second * 30,
IdleConnTimeout: time.Second * 300,
MaxIdleConnsPerHost: 500,
ReadBufferSize: 32 << 10,
WriteBufferSize: 32 << 10,
DisableCompression: true,
TLSClientConfig: &tls.Config{},
TLSHandshakeTimeout: DefaultTLSHandshakeTimeout,
ResponseHeaderTimeout: DefaultResponseHeaderTimeout,
IdleConnTimeout: DefaultIdleConnTimeout,
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
ReadBufferSize: DefaultReadBufferSize,
WriteBufferSize: DefaultWriteBufferSize,
DisableCompression: DefaultDisableCompression,
},
Timeout: time.Hour,
Timeout: DefaultTimeout,
},
}

Expand Down
4 changes: 0 additions & 4 deletions pkg/objectstorage/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ func (o *obs) GetObjectMetadata(ctx context.Context, bucketName, objectKey strin

// GetObjectMetadatas returns the metadatas of the objects.
func (o *obs) GetObjectMetadatas(ctx context.Context, bucketName, prefix, marker, delimiter string, limit int64) (*ObjectMetadatas, error) {
if limit == 0 {
limit = DefaultGetObjectMetadatasLimit
}

resp, err := o.client.ListObjects(&huaweiobs.ListObjectsInput{
ListObjsInput: huaweiobs.ListObjsInput{
Prefix: prefix,
Expand Down
4 changes: 0 additions & 4 deletions pkg/objectstorage/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ func (o *oss) GetObjectMetadatas(ctx context.Context, bucketName, prefix, marker
return nil, err
}

if limit == 0 {
limit = DefaultGetObjectMetadatasLimit
}

resp, err := bucket.ListObjects(aliyunoss.Prefix(prefix), aliyunoss.Marker(marker), aliyunoss.Delimiter(delimiter), aliyunoss.MaxKeys(int(limit)))
if err != nil {
return nil, err
Expand Down
5 changes: 1 addition & 4 deletions pkg/objectstorage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func (s *s3) GetObjectMetadata(ctx context.Context, bucketName, objectKey string

// GetObjectMetadatas returns the metadatas of the objects.
func (s *s3) GetObjectMetadatas(ctx context.Context, bucketName, prefix, marker, delimiter string, limit int64) (*ObjectMetadatas, error) {
if limit == 0 {
limit = DefaultGetObjectMetadatasLimit
}

resp, err := s.client.ListObjectsWithContext(ctx, &awss3.ListObjectsInput{
Bucket: aws.String(bucketName),
Prefix: aws.String(prefix),
Expand Down Expand Up @@ -156,6 +152,7 @@ func (s *s3) GetObjectMetadatas(ctx context.Context, bucketName, prefix, marker,
if err != nil {
return nil, fmt.Errorf("failed to decode commonPrefixes %s, error: %s", *commonPrefix.Prefix, err.Error())
}

commonPrefixes = append(commonPrefixes, prefix)
}

Expand Down

0 comments on commit 9c9959f

Please sign in to comment.