-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from 88labs/feat/add-global-s3dialer
add global s3dialer
- Loading branch information
Showing
3 changed files
with
166 additions
and
4 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,63 @@ | ||
package global_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/88labs/go-utils/ulid" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/feature/s3/manager" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/88labs/go-utils/aws/awsconfig" | ||
"github.com/88labs/go-utils/aws/awss3" | ||
"github.com/88labs/go-utils/aws/awss3/options/global/s3dialer" | ||
"github.com/88labs/go-utils/aws/ctxawslocal" | ||
) | ||
|
||
const ( | ||
TestBucket = "test" | ||
TestRegion = awsconfig.RegionTokyo | ||
) | ||
|
||
func TestGlobalOptionWithHeadObject(t *testing.T) { | ||
ctx := ctxawslocal.WithContext( | ||
context.Background(), | ||
ctxawslocal.WithS3Endpoint("http://127.0.0.1:29000"), // use Minio | ||
ctxawslocal.WithAccessKey("DUMMYACCESSKEYEXAMPLE"), | ||
ctxawslocal.WithSecretAccessKey("DUMMYSECRETKEYEXAMPLE"), | ||
) | ||
s3Client, err := awss3.GetClient(ctx, TestRegion) | ||
assert.NoError(t, err) | ||
|
||
createFixture := func(fileSize int) awss3.Key { | ||
key := fmt.Sprintf("awstest/%s.txt", ulid.MustNew()) | ||
uploader := manager.NewUploader(s3Client) | ||
input := s3.PutObjectInput{ | ||
Body: bytes.NewReader(bytes.Repeat([]byte{1}, fileSize)), | ||
Bucket: aws.String(TestBucket), | ||
Key: aws.String(key), | ||
Expires: aws.Time(time.Now().Add(10 * time.Minute)), | ||
} | ||
if _, err := uploader.Upload(ctx, &input); err != nil { | ||
assert.NoError(t, err) | ||
} | ||
return awss3.Key(key) | ||
} | ||
|
||
t.Run("If the option is specified", func(t *testing.T) { | ||
key := createFixture(100) | ||
dialer := s3dialer.NewConfGlobalDialer() | ||
dialer.WithTimeout(time.Second) | ||
dialer.WithKeepAlive(2 * time.Second) | ||
dialer.WithDeadline(time.Now().Add(time.Second)) | ||
awss3.GlobalDialer = dialer | ||
res, err := awss3.HeadObject(ctx, TestRegion, TestBucket, key) | ||
assert.NoError(t, err) | ||
assert.Equal(t, aws.Int64(100), res.ContentLength) | ||
}) | ||
} |
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,50 @@ | ||
package s3dialer | ||
|
||
import "time" | ||
|
||
type ConfGlobalDialer struct { | ||
// Timeout is the maximum amount of time a dial will wait for | ||
// a connect to complete. If Deadline is also set, it may fail | ||
// earlier. | ||
// | ||
// The default is no timeout. | ||
// | ||
// When using TCP and dialing a host name with multiple IP | ||
// addresses, the timeout may be divided between them. | ||
// | ||
// With or without a timeout, the operating system may impose | ||
// its own earlier timeout. For instance, TCP timeouts are | ||
// often around 3 minutes. | ||
Timeout time.Duration | ||
|
||
// Deadline is the absolute point in time after which dials | ||
// will fail. If Timeout is set, it may fail earlier. | ||
// Zero means no deadline, or dependent on the operating system | ||
// as with the Timeout option. | ||
Deadline *time.Time | ||
|
||
// KeepAlive specifies the interval between keep-alive | ||
// probes for an active network connection. | ||
// If zero, keep-alive probes are sent with a default value | ||
// (currently 15 seconds), if supported by the protocol and operating | ||
// system. Network protocols or operating systems that do | ||
// not support keep-alives ignore this field. | ||
// If negative, keep-alive probes are disabled. | ||
KeepAlive time.Duration | ||
} | ||
|
||
func NewConfGlobalDialer() *ConfGlobalDialer { | ||
return &ConfGlobalDialer{} | ||
} | ||
|
||
func (c *ConfGlobalDialer) WithTimeout(timeout time.Duration) { | ||
c.Timeout = timeout | ||
} | ||
|
||
func (c *ConfGlobalDialer) WithDeadline(deadline time.Time) { | ||
c.Deadline = &deadline | ||
} | ||
|
||
func (c *ConfGlobalDialer) WithKeepAlive(keepAlive time.Duration) { | ||
c.KeepAlive = keepAlive | ||
} |