From 3858d4c7dfef767b2c91263f1344d467bcad63f4 Mon Sep 17 00:00:00 2001 From: Cronus <105345303+ice-cronus@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:45:45 +0700 Subject: [PATCH] increase backoff (#227) --- go.mod | 4 +-- go.sum | 8 +++--- kyc/face/internal/threedivi/threedivi.go | 32 +++++++++++++++++++----- kyc/face/remote_kyc_config.go | 11 +++++++- kyc/linking/linking.go | 13 ++++++++-- kyc/quiz/remote_kyc_config.go | 11 +++++++- kyc/social/remote_kyc_config.go | 22 ++++++++++++++-- users/user_growth.go | 13 ++++++++-- 8 files changed, 94 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index bf9b25f1..68a76914 100644 --- a/go.mod +++ b/go.mod @@ -97,7 +97,7 @@ require ( github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.5-0.20231225225746-43d5d4cd4e0e // indirect - github.com/google/pprof v0.0.0-20241023014458-598669927662 // indirect + github.com/google/pprof v0.0.0-20241029010322-833c56d90c8e // indirect github.com/google/s2a-go v0.1.8 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect @@ -189,7 +189,7 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect google.golang.org/grpc v1.67.1 // indirect - google.golang.org/grpc/stats/opentelemetry v0.0.0-20241025232817-cb329375b14e // indirect + google.golang.org/grpc/stats/opentelemetry v0.0.0-20241028142157-ada6787961b3 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 7b3ba820..bef7464e 100644 --- a/go.sum +++ b/go.sum @@ -223,8 +223,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc= github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0= -github.com/google/pprof v0.0.0-20241023014458-598669927662 h1:SKMkD83p7FwUqKmBsPdLHF5dNyxq3jOWwu9w9UyH5vA= -github.com/google/pprof v0.0.0-20241023014458-598669927662/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= +github.com/google/pprof v0.0.0-20241029010322-833c56d90c8e h1:v7R0PZoC2p1KWQmv1+GqCXQe59Ab1TkDF8Y9Lg2W6m4= +github.com/google/pprof v0.0.0-20241029010322-833c56d90c8e/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= github.com/google/s2a-go v0.1.8 h1:zZDs9gcbt9ZPLV0ndSyQk6Kacx2g/X+SKYovpnz3SMM= github.com/google/s2a-go v0.1.8/go.mod h1:6iNWHTpQ+nfNRN5E00MSdfDwVesa8hhS32PhPO8deJA= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -579,8 +579,8 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= -google.golang.org/grpc/stats/opentelemetry v0.0.0-20241025232817-cb329375b14e h1:SoMI+r+Qsp379U9BlVzrHtqAqYP3NEv9vNhYqUaAWOg= -google.golang.org/grpc/stats/opentelemetry v0.0.0-20241025232817-cb329375b14e/go.mod h1:jzYlkSMbKypzuu6xoAEijsNVo9ZeDF1u/zCfFgsx7jg= +google.golang.org/grpc/stats/opentelemetry v0.0.0-20241028142157-ada6787961b3 h1:hUfOButuEtpc0UvYiaYRbNwxVYr0mQQOWq6X8beJ9Gc= +google.golang.org/grpc/stats/opentelemetry v0.0.0-20241028142157-ada6787961b3/go.mod h1:jzYlkSMbKypzuu6xoAEijsNVo9ZeDF1u/zCfFgsx7jg= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/kyc/face/internal/threedivi/threedivi.go b/kyc/face/internal/threedivi/threedivi.go index e4dbc708..4848711c 100644 --- a/kyc/face/internal/threedivi/threedivi.go +++ b/kyc/face/internal/threedivi/threedivi.go @@ -75,8 +75,17 @@ func (t *threeDivi) updateAvailability(ctx context.Context) error { } if resp, err := req. SetContext(ctx). - SetRetryCount(10). //nolint:gomnd // . - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). //nolint:gomnd // . + SetRetryCount(10). //nolint:gomnd // . + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to check availability of face auth, retrying... ")) @@ -199,8 +208,8 @@ func (t *threeDivi) Reset(ctx context.Context, user *users.User, fetchState bool var resp *req.Response if resp, err = req. SetContext(ctx). - SetRetryCount(10). //nolint:gomnd // . - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). //nolint:gomnd // . + SetRetryCount(10). //nolint:gomnd // . + SetRetryInterval(backoff). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to delete face auth state for user, retrying... ")) @@ -293,13 +302,24 @@ func stepIdx(step users.KYCStep) int { return int(step) - 1 } +func backoff(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } +} + func (t *threeDivi) searchIn3DiviForApplicant(ctx context.Context, userID users.UserID) (*applicant, error) { getApplicantURL := fmt.Sprintf("%v/publicapi/api/v2/private/Applicants/ByReferenceId/%v", t.cfg.ThreeDiVi.BAFHost, userID) log.Debug("GET ", getApplicantURL) if resp, err := req. SetContext(ctx). - SetRetryCount(10). //nolint:gomnd // . - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). //nolint:gomnd // . + SetRetryCount(10). //nolint:gomnd // . + SetRetryInterval(backoff). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to match applicantId for user, retrying... ")) diff --git a/kyc/face/remote_kyc_config.go b/kyc/face/remote_kyc_config.go index c7375162..73b3a7d1 100644 --- a/kyc/face/remote_kyc_config.go +++ b/kyc/face/remote_kyc_config.go @@ -45,7 +45,16 @@ func (c *client) syncKYCConfigJSON(ctx context.Context) error { if resp, err := req. SetContext(ctx). SetRetryCount(25). - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to fetch KYCConfigJSON, retrying...")) //nolint:revive // . diff --git a/kyc/linking/linking.go b/kyc/linking/linking.go index 9c2fa457..81cd1551 100644 --- a/kyc/linking/linking.go +++ b/kyc/linking/linking.go @@ -167,8 +167,17 @@ func (l *linker) fetchTokenData(ctx context.Context, tenant, token string) (*use } if resp, err = req. SetContext(ctx). - SetRetryCount(10). //nolint:gomnd // . - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). //nolint:gomnd // . + SetRetryCount(10). //nolint:gomnd // . + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to check accounts linking, retrying... ")) diff --git a/kyc/quiz/remote_kyc_config.go b/kyc/quiz/remote_kyc_config.go index 4e181319..cba8edaa 100644 --- a/kyc/quiz/remote_kyc_config.go +++ b/kyc/quiz/remote_kyc_config.go @@ -45,7 +45,16 @@ func (r *repositoryImpl) syncKYCConfigJSON(ctx context.Context) error { if resp, err := req. SetContext(ctx). SetRetryCount(25). - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to fetch KYCConfigJSON, retrying...")) //nolint:revive // . diff --git a/kyc/social/remote_kyc_config.go b/kyc/social/remote_kyc_config.go index 553a0dac..881a00d4 100644 --- a/kyc/social/remote_kyc_config.go +++ b/kyc/social/remote_kyc_config.go @@ -51,7 +51,16 @@ func (r *repository) syncKYCConfigJSON1(ctx context.Context) error { if resp, err := req. SetContext(ctx). SetRetryCount(25). - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to fetch KYCConfigJSON, retrying...")) //nolint:revive // . @@ -94,7 +103,16 @@ func (r *repository) syncKYCConfigJSON2(ctx context.Context) error { if resp, err := req. SetContext(ctx). SetRetryCount(25). - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to fetch KYCConfigJSON, retrying...")) //nolint:revive // . diff --git a/users/user_growth.go b/users/user_growth.go index 5a263632..534a2a5e 100644 --- a/users/user_growth.go +++ b/users/user_growth.go @@ -42,8 +42,17 @@ func (r *repository) GetUserGrowth(ctx context.Context, days uint64, tz *stdlibt func (r *repository) getAdoptionTotalActiveUsersValue(ctx context.Context) (totalActiveUsers uint64, err error) { resp, err := req. SetContext(ctx). - SetRetryCount(25). //nolint:gomnd,mnd // . - SetRetryBackoffInterval(10*stdlibtime.Millisecond, 1*stdlibtime.Second). //nolint:gomnd,mnd // . + SetRetryCount(25). //nolint:gomnd,mnd // . + SetRetryInterval(func(_ *req.Response, attempt int) stdlibtime.Duration { + switch { + case attempt <= 1: + return 100 * stdlibtime.Millisecond //nolint:gomnd // . + case attempt == 2: //nolint:gomnd // . + return 1 * stdlibtime.Second + default: + return 10 * stdlibtime.Second //nolint:gomnd // . + } + }). SetRetryHook(func(resp *req.Response, err error) { if err != nil { log.Error(errors.Wrap(err, "failed to fetch adoption, retrying...")) //nolint:revive // .