Airat Arifullin
2697d4d1fe
All checks were successful
DCO action / DCO (pull_request) Successful in 3m16s
Vulncheck / Vulncheck (pull_request) Successful in 3m2s
Build / Build Components (1.21) (pull_request) Successful in 5m35s
Tests and linters / Staticcheck (pull_request) Successful in 5m8s
Tests and linters / gopls check (pull_request) Successful in 5m22s
Tests and linters / Lint (pull_request) Successful in 6m31s
Pre-commit hooks / Pre-commit (pull_request) Successful in 9m6s
Tests and linters / Tests (1.21) (pull_request) Successful in 9m26s
Tests and linters / Tests with -race (pull_request) Successful in 9m31s
Tests and linters / Tests (1.22) (pull_request) Successful in 9m58s
Build / Build Components (1.22) (pull_request) Successful in 2m55s
* Fix `subjectFromSubjectExtended` converter. Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
|
frostfsidcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/frostfsid"
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
)
|
|
|
|
type morphFrostfsIDCache struct {
|
|
subjProvider frostfsidcore.SubjectProvider
|
|
|
|
subjCache *expirable.LRU[util.Uint160, *client.Subject]
|
|
|
|
subjExtCache *expirable.LRU[util.Uint160, *client.SubjectExtended]
|
|
}
|
|
|
|
func newMorphFrostfsIDCache(subjProvider frostfsidcore.SubjectProvider, size int, ttl time.Duration) frostfsidcore.SubjectProvider {
|
|
return &morphFrostfsIDCache{
|
|
subjProvider: subjProvider,
|
|
|
|
subjCache: expirable.NewLRU(size, func(util.Uint160, *client.Subject) {}, ttl),
|
|
|
|
subjExtCache: expirable.NewLRU(size, func(util.Uint160, *client.SubjectExtended) {}, ttl),
|
|
}
|
|
}
|
|
|
|
func (m *morphFrostfsIDCache) GetSubject(addr util.Uint160) (*client.Subject, error) {
|
|
result, found := m.subjCache.Get(addr)
|
|
if found {
|
|
return result, nil
|
|
}
|
|
|
|
result, err := m.subjProvider.GetSubject(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.subjCache.Add(addr, result)
|
|
return result, nil
|
|
}
|
|
|
|
func (m *morphFrostfsIDCache) GetSubjectExtended(addr util.Uint160) (*client.SubjectExtended, error) {
|
|
subjExt, found := m.subjExtCache.Get(addr)
|
|
if found {
|
|
return subjExt, nil
|
|
}
|
|
|
|
var err error
|
|
subjExt, err = m.subjProvider.GetSubjectExtended(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.subjExtCache.Add(addr, subjExt)
|
|
m.subjCache.Add(addr, subjectFromSubjectExtended(subjExt))
|
|
|
|
return subjExt, nil
|
|
}
|
|
|
|
func subjectFromSubjectExtended(subjExt *client.SubjectExtended) *client.Subject {
|
|
return &client.Subject{
|
|
PrimaryKey: subjExt.PrimaryKey,
|
|
AdditionalKeys: subjExt.AdditionalKeys,
|
|
Namespace: subjExt.Namespace,
|
|
Name: subjExt.Name,
|
|
KV: subjExt.KV,
|
|
}
|
|
}
|