forked from TrueCloudLab/frostfs-node
[#1117] node: Introduce cache for frostfsid contract client
Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
parent
b60a51b862
commit
9cc51f86b7
4 changed files with 99 additions and 4 deletions
71
cmd/frostfs-node/frostfsid.go
Normal file
71
cmd/frostfs-node/frostfsid.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
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.Name,
|
||||
Name: subjExt.Name,
|
||||
KV: subjExt.KV,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue