123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package ec2
- import (
- "time"
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/aws/awsutil"
- "github.com/aws/aws-sdk-go/aws/client"
- "github.com/aws/aws-sdk-go/aws/endpoints"
- "github.com/aws/aws-sdk-go/aws/request"
- "github.com/aws/aws-sdk-go/internal/sdkrand"
- )
- type retryer struct {
- client.DefaultRetryer
- }
- func (d retryer) RetryRules(r *request.Request) time.Duration {
- switch r.Operation.Name {
- case opModifyNetworkInterfaceAttribute:
- fallthrough
- case opAssignPrivateIpAddresses:
- return customRetryRule(r)
- default:
- return d.DefaultRetryer.RetryRules(r)
- }
- }
- func customRetryRule(r *request.Request) time.Duration {
- retryTimes := []time.Duration{
- time.Second,
- 3 * time.Second,
- 5 * time.Second,
- }
- count := r.RetryCount
- if count >= len(retryTimes) {
- count = len(retryTimes) - 1
- }
- minTime := int(retryTimes[count])
- return time.Duration(sdkrand.SeededRand.Intn(minTime) + minTime)
- }
- func setCustomRetryer(c *client.Client) {
- maxRetries := aws.IntValue(c.Config.MaxRetries)
- if c.Config.MaxRetries == nil || maxRetries == aws.UseServiceDefaultRetries {
- maxRetries = 3
- }
- c.Retryer = retryer{
- DefaultRetryer: client.DefaultRetryer{
- NumMaxRetries: maxRetries,
- },
- }
- }
- func init() {
- initClient = func(c *client.Client) {
- if c.Config.Retryer == nil {
-
-
- setCustomRetryer(c)
- }
- }
- initRequest = func(r *request.Request) {
- if r.Operation.Name == opCopySnapshot {
- r.Handlers.Build.PushFront(fillPresignedURL)
- }
- }
- }
- func fillPresignedURL(r *request.Request) {
- if !r.ParamsFilled() {
- return
- }
- origParams := r.Params.(*CopySnapshotInput)
-
- if origParams.PresignedUrl != nil || origParams.DestinationRegion != nil {
- return
- }
- origParams.DestinationRegion = r.Config.Region
- newParams := awsutil.CopyOf(r.Params).(*CopySnapshotInput)
-
-
- cfg := r.Config.Copy(aws.NewConfig().
- WithEndpoint("").
- WithRegion(aws.StringValue(origParams.SourceRegion)))
- clientInfo := r.ClientInfo
- resolved, err := r.Config.EndpointResolver.EndpointFor(
- clientInfo.ServiceName, aws.StringValue(cfg.Region),
- func(opt *endpoints.Options) {
- opt.DisableSSL = aws.BoolValue(cfg.DisableSSL)
- opt.UseDualStack = aws.BoolValue(cfg.UseDualStack)
- },
- )
- if err != nil {
- r.Error = err
- return
- }
- clientInfo.Endpoint = resolved.URL
- clientInfo.SigningRegion = resolved.SigningRegion
-
- req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data)
- url, err := req.Presign(5 * time.Minute)
- if err != nil {
- r.Error = err
- return
- }
-
- origParams.PresignedUrl = &url
- }
|