1 Star 0 Fork 0

peter/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 3.65 KB
一键复制 编辑 原始数据 按行查看 历史
yacovm 提交于 2018-04-18 19:01 +08:00 . [FAB-9571] Add indirect Validate() MSP caching
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cache
import (
"fmt"
"sync"
"github.com/golang/groupcache/lru"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/msp"
pmsp "github.com/hyperledger/fabric/protos/msp"
)
const (
deserializeIdentityCacheSize = 100
validateIdentityCacheSize = 100
satisfiesPrincipalCacheSize = 100
)
var mspLogger = flogging.MustGetLogger("msp")
func New(o msp.MSP) (msp.MSP, error) {
mspLogger.Debugf("Creating Cache-MSP instance")
if o == nil {
return nil, fmt.Errorf("Invalid passed MSP. It must be different from nil.")
}
theMsp := &cachedMSP{MSP: o}
theMsp.deserializeIdentityCache = lru.New(deserializeIdentityCacheSize)
theMsp.satisfiesPrincipalCache = lru.New(satisfiesPrincipalCacheSize)
theMsp.validateIdentityCache = lru.New(validateIdentityCacheSize)
return theMsp, nil
}
type cachedMSP struct {
msp.MSP
// cache for DeserializeIdentity.
deserializeIdentityCache *lru.Cache
dicMutex sync.Mutex // synchronize access to cache
// cache for validateIdentity
validateIdentityCache *lru.Cache
vicMutex sync.Mutex // synchronize access to cache
// basically a map of principals=>identities=>stringified to booleans
// specifying whether this identity satisfies this principal
satisfiesPrincipalCache *lru.Cache
spcMutex sync.Mutex // synchronize access to cache
}
type cachedIdentity struct {
msp.Identity
cache *cachedMSP
}
func (id *cachedIdentity) SatisfiesPrincipal(principal *pmsp.MSPPrincipal) error {
return id.cache.SatisfiesPrincipal(id.Identity, principal)
}
func (id *cachedIdentity) Validate() error {
return id.cache.Validate(id.Identity)
}
func (c *cachedMSP) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
c.dicMutex.Lock()
id, ok := c.deserializeIdentityCache.Get(string(serializedIdentity))
c.dicMutex.Unlock()
if ok {
return &cachedIdentity{
cache: c,
Identity: id.(msp.Identity),
}, nil
}
id, err := c.MSP.DeserializeIdentity(serializedIdentity)
if err == nil {
c.dicMutex.Lock()
defer c.dicMutex.Unlock()
c.deserializeIdentityCache.Add(string(serializedIdentity), id)
return &cachedIdentity{
cache: c,
Identity: id.(msp.Identity),
}, nil
}
return nil, err
}
func (c *cachedMSP) Setup(config *pmsp.MSPConfig) error {
c.cleanCash()
return c.MSP.Setup(config)
}
func (c *cachedMSP) Validate(id msp.Identity) error {
identifier := id.GetIdentifier()
key := string(identifier.Mspid + ":" + identifier.Id)
c.vicMutex.Lock()
_, ok := c.validateIdentityCache.Get(key)
c.vicMutex.Unlock()
if ok {
// cache only stores if the identity is valid.
return nil
}
err := c.MSP.Validate(id)
if err == nil {
c.vicMutex.Lock()
defer c.vicMutex.Unlock()
c.validateIdentityCache.Add(key, true)
}
return err
}
func (c *cachedMSP) SatisfiesPrincipal(id msp.Identity, principal *pmsp.MSPPrincipal) error {
identifier := id.GetIdentifier()
identityKey := string(identifier.Mspid + ":" + identifier.Id)
principalKey := string(principal.PrincipalClassification) + string(principal.Principal)
key := identityKey + principalKey
c.spcMutex.Lock()
v, ok := c.satisfiesPrincipalCache.Get(key)
c.spcMutex.Unlock()
if ok {
if v == nil {
return nil
}
return v.(error)
}
err := c.MSP.SatisfiesPrincipal(id, principal)
c.spcMutex.Lock()
defer c.spcMutex.Unlock()
c.satisfiesPrincipalCache.Add(key, err)
return err
}
func (c *cachedMSP) cleanCash() error {
c.deserializeIdentityCache = lru.New(deserializeIdentityCacheSize)
c.satisfiesPrincipalCache = lru.New(satisfiesPrincipalCacheSize)
c.validateIdentityCache = lru.New(validateIdentityCacheSize)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/peter_code_git/fabric.git
git@gitee.com:peter_code_git/fabric.git
peter_code_git
fabric
fabric
v1.2.0

搜索帮助