-
Notifications
You must be signed in to change notification settings - Fork 60
/
cache.go
65 lines (55 loc) · 1.54 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package idemix
import (
"time"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/msp/idemix"
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"go.uber.org/zap/zapcore"
)
func NewIdentityCache(backed idemix.IdentityCacheBackendFunc, size int) *idemix.IdentityCache {
return idemix.NewIdentityCache(backed, size, &driver2.IdentityOptions{EIDExtension: true})
}
type WalletIdentityCacheBackendFunc func() (view.Identity, error)
type WalletIdentityCache struct {
backed WalletIdentityCacheBackendFunc
ch chan view.Identity
timeout time.Duration
}
func NewWalletIdentityCache(backed WalletIdentityCacheBackendFunc, size int) *WalletIdentityCache {
ci := &WalletIdentityCache{
backed: backed,
ch: make(chan view.Identity, size),
timeout: time.Millisecond * 100,
}
if size > 0 {
go ci.run()
}
return ci
}
func (c *WalletIdentityCache) Identity() (view.Identity, error) {
select {
case entry := <-c.ch:
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("fetch identity from producer channel done [%s][%d]", entry)
}
return entry, nil
default:
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("fetch identity from producer channel timeout")
}
return c.backed()
}
}
func (c *WalletIdentityCache) run() {
for {
id, err := c.backed()
if err != nil {
continue
}
c.ch <- id
}
}