Skip to content

Commit

Permalink
do not call face availability for every forwarded user (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-cronus authored Jul 5, 2024
1 parent b9792c5 commit d2960f6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb
github.com/ice-blockchain/wintr v1.143.0
github.com/ice-blockchain/wintr v1.144.0
github.com/imroc/req/v3 v3.43.7
github.com/ip2location/ip2location-go/v9 v9.7.0
github.com/jackc/pgx/v5 v5.6.0
Expand All @@ -22,7 +22,7 @@ require (
github.com/testcontainers/testcontainers-go v0.31.0
github.com/zeebo/xxh3 v1.0.2
golang.org/x/mod v0.19.0
golang.org/x/net v0.26.0
golang.org/x/net v0.27.0
)

require (
Expand Down Expand Up @@ -155,7 +155,7 @@ require (
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb h1:8TnFP3mc7O+tc44kv2e0/TpZKnEVUaKH+UstwfBwRkk=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb/go.mod h1:ZsQU7i3mxhgBBu43Oev7WPFbIjP4TniN/b1UPNGbrq8=
github.com/ice-blockchain/wintr v1.143.0 h1:KID/M01KN1jl7mlDF+VnIBScR7uT277mJtebYtemSF0=
github.com/ice-blockchain/wintr v1.143.0/go.mod h1:EBMC4stY8PL8ptIuoLwbh92LblNlOPNz/Ze+PlmsyyI=
github.com/ice-blockchain/wintr v1.144.0 h1:YQE0olkPdSI6AOlw7r/j5jGI6uLciZQrvXFIkN4C4l4=
github.com/ice-blockchain/wintr v1.144.0/go.mod h1:3HAl5nodsetqQN30q3gUvsxgfq2B7F86Os/II7/5GPQ=
github.com/imroc/req/v3 v3.43.7 h1:dOcNb9n0X83N5/5/AOkiU+cLhzx8QFXjv5MhikazzQA=
github.com/imroc/req/v3 v3.43.7/go.mod h1:SQIz5iYop16MJxbo8ib+4LnostGCok8NQf8ToyQc2xA=
github.com/ip2location/ip2location-go/v9 v9.7.0 h1:ipwl67HOWcrw+6GOChkEXcreRQR37NabqBd2ayYa4Q0=
Expand Down Expand Up @@ -430,8 +430,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
Expand All @@ -458,8 +458,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
Expand Down
1 change: 1 addition & 0 deletions kyc/face/internal/threedivi/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
users internal.UserRepository
cfg *Config
loadBalancedUsersCount atomic.Uint64
activeUsersCount atomic.Uint64
}
Config struct {
ThreeDiVi struct {
Expand Down
42 changes: 34 additions & 8 deletions kyc/face/internal/threedivi/threedivi.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,31 @@ func New3Divi(ctx context.Context, usersRepository internal.UserRepository, cfg
cfg: cfg,
}
go tdv.clearUsers(ctx)
reqCtx, reqCancel := context.WithTimeout(ctx, requestDeadline)
log.Error(errors.Wrapf(tdv.updateAvailability(reqCtx), "failed to update face availability on startup"))
reqCancel()
go tdv.startAvailabilitySyncer(ctx)

return tdv
}

func (t *threeDivi) Available(ctx context.Context, userWasPreviouslyForwardedToFaceKYC bool) error {
func (t *threeDivi) startAvailabilitySyncer(ctx context.Context) {
ticker := stdlibtime.NewTicker(100 * stdlibtime.Millisecond) //nolint:gomnd // .
defer ticker.Stop()
for {
select {
case <-ticker.C:
reqCtx, cancel := context.WithTimeout(ctx, requestDeadline)
log.Error(errors.Wrap(t.updateAvailability(reqCtx), "failed to updateAvailability"))
cancel()
case <-ctx.Done():
return
}
}
}

//nolint:funlen // .
func (t *threeDivi) updateAvailability(ctx context.Context) error {
if t.cfg.ThreeDiVi.AvailabilityURL == "" {
return nil
}
Expand Down Expand Up @@ -77,17 +97,23 @@ func (t *threeDivi) Available(ctx context.Context, userWasPreviouslyForwardedToF
} else if data, err2 := resp.ToBytes(); err2 != nil {
return errors.Wrapf(err2, "failed to read body of availability of face auth")
} else { //nolint:revive // .
return t.isAvailable(data, userWasPreviouslyForwardedToFaceKYC)
activeUsers, cErr := t.activeUsers(data)
if cErr != nil {
return errors.Wrapf(cErr, "failed to parse metrics of availability of face auth")
}
t.activeUsersCount.Store(uint64(activeUsers))
}

return nil
}

func (t *threeDivi) Available(_ context.Context, userWasPreviouslyForwardedToFaceKYC bool) error {
return t.isAvailable(userWasPreviouslyForwardedToFaceKYC)
}

//nolint:revive // .
func (t *threeDivi) isAvailable(data []byte, userWasPreviouslyForwardedToFaceKYC bool) error {
activeUsers, cErr := t.activeUsers(data)
if cErr != nil {
return errors.Wrapf(cErr, "failed to parse metrics of availability of face auth")
}
if int64(t.cfg.ThreeDiVi.ConcurrentUsers)-(int64(activeUsers)+int64(t.loadBalancedUsersCount.Load())) >= 1 {
func (t *threeDivi) isAvailable(userWasPreviouslyForwardedToFaceKYC bool) error {
if int64(t.cfg.ThreeDiVi.ConcurrentUsers)-(int64(t.activeUsersCount.Load())+int64(t.loadBalancedUsersCount.Load())) >= 1 {
if !userWasPreviouslyForwardedToFaceKYC {
t.loadBalancedUsersCount.Add(1)
}
Expand Down

0 comments on commit d2960f6

Please sign in to comment.