forked from hyperledger/fabric-sdk-go
/
contextcache.go
226 lines (194 loc) · 6.78 KB
/
contextcache.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chpvdr
import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/dynamicdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/fabricselection"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/channel/membership"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
"github.com/pkg/errors"
)
type cache interface {
Get(lazycache.Key, ...interface{}) (interface{}, error)
Close()
}
type contextCache struct {
ctx fab.ClientContext
eventServiceCache cache
discoveryServiceCache cache
selectionServiceCache cache
chCfgCache cache
membershipCache cache
}
var cfgCacheProvider = func(opts ...options.Opt) cache {
return chconfig.NewRefCache(opts...)
}
func newContextCache(ctx fab.ClientContext, opts []options.Opt) *contextCache {
eventIdleTime := ctx.EndpointConfig().Timeout(fab.EventServiceIdle)
chConfigRefresh := ctx.EndpointConfig().Timeout(fab.ChannelConfigRefresh)
membershipRefresh := ctx.EndpointConfig().Timeout(fab.ChannelMembershipRefresh)
c := &contextCache{
ctx: ctx,
}
c.chCfgCache = cfgCacheProvider(append(opts, chconfig.WithRefreshInterval(chConfigRefresh))...)
c.membershipCache = membership.NewRefCache(membershipRefresh)
c.discoveryServiceCache = lazycache.New(
"Discovery_Service_Cache",
func(key lazycache.Key) (interface{}, error) {
ck := key.(*cacheKey)
return c.createDiscoveryService(ck.channelConfig, opts...)
},
)
c.selectionServiceCache = lazycache.New(
"Selection_Service_Cache",
func(key lazycache.Key) (interface{}, error) {
ck := key.(*cacheKey)
return c.createSelectionService(ck.channelConfig, opts...)
},
)
c.eventServiceCache = lazycache.New(
"Event_Service_Cache",
func(key lazycache.Key) (interface{}, error) {
ck := key.(*eventCacheKey)
return NewEventClientRef(
eventIdleTime,
func() (fab.EventClient, error) {
return c.createEventClient(ck.channelConfig, ck.opts...)
},
), nil
},
)
return c
}
func (c *contextCache) Close() {
logger.Debug("Closing event service cache...")
c.eventServiceCache.Close()
logger.Debug("Closing membership cache...")
c.membershipCache.Close()
logger.Debug("Closing channel configuration cache...")
c.chCfgCache.Close()
logger.Debug("Closing selection service cache...")
c.selectionServiceCache.Close()
logger.Debug("Closing discovery service cache...")
c.discoveryServiceCache.Close()
}
func (c *contextCache) createEventClient(chConfig fab.ChannelCfg, opts ...options.Opt) (fab.EventClient, error) {
discovery, err := c.GetDiscoveryService(chConfig.ID())
if err != nil {
return nil, errors.WithMessage(err, "could not get discovery service")
}
logger.Debugf("Using deliver events for channel [%s]", chConfig.ID())
return deliverclient.New(c.ctx, chConfig, discovery, opts...)
}
func (c *contextCache) createDiscoveryService(chConfig fab.ChannelCfg, opts ...options.Opt) (fab.DiscoveryService, error) {
if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
logger.Debugf("Using Dynamic Discovery based on V1_2 capability.")
membership, err := c.GetMembership(chConfig.ID())
if err != nil {
return nil, errors.WithMessage(err, "failed to create discovery service")
}
return dynamicdiscovery.NewChannelService(c.ctx, membership, chConfig.ID(), opts...)
}
return staticdiscovery.NewService(c.ctx.EndpointConfig(), c.ctx.InfraProvider(), chConfig.ID())
}
func (c *contextCache) GetDiscoveryService(channelID string) (fab.DiscoveryService, error) {
chnlCfg, err := c.GetChannelConfig(channelID)
if err != nil {
return nil, err
}
key := newCacheKey(chnlCfg)
discoveryService, err := c.discoveryServiceCache.Get(key)
if err != nil {
return nil, err
}
return discoveryService.(fab.DiscoveryService), nil
}
func (c *contextCache) createSelectionService(chConfig fab.ChannelCfg, opts ...options.Opt) (fab.SelectionService, error) {
discovery, err := c.GetDiscoveryService(chConfig.ID())
if err != nil {
return nil, err
}
if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
logger.Debugf("Using Fabric Selection based on V1_2 capability.")
return fabricselection.New(c.ctx, chConfig.ID(), discovery, opts...)
}
return dynamicselection.NewService(c.ctx, chConfig.ID(), discovery)
}
func (c *contextCache) GetSelectionService(channelID string) (fab.SelectionService, error) {
chnlCfg, err := c.GetChannelConfig(channelID)
if err != nil {
return nil, err
}
key := newCacheKey(chnlCfg)
selectionService, err := c.selectionServiceCache.Get(key)
if err != nil {
return nil, err
}
return selectionService.(fab.SelectionService), nil
}
// GetEventService returns the EventService.
func (c *contextCache) GetEventService(channelID string, opts ...options.Opt) (fab.EventService, error) {
chnlCfg, err := c.GetChannelConfig(channelID)
if err != nil {
return nil, err
}
key, err := newEventCacheKey(chnlCfg, opts...)
if err != nil {
return nil, err
}
eventService, err := c.eventServiceCache.Get(key)
if err != nil {
return nil, err
}
return eventService.(fab.EventService), nil
}
func (c *contextCache) GetChannelConfig(channelID string) (fab.ChannelCfg, error) {
if channelID == "" {
// System channel
return chconfig.NewChannelCfg(""), nil
}
chCfgRef, err := c.loadChannelCfgRef(channelID)
if err != nil {
return nil, err
}
chCfg, err := chCfgRef.Get()
if err != nil {
return nil, errors.WithMessage(err, "could not get chConfig cache reference")
}
return chCfg.(fab.ChannelCfg), nil
}
func (c *contextCache) loadChannelCfgRef(channelID string) (*chconfig.Ref, error) {
key, err := chconfig.NewCacheKey(c.ctx, func(string) (fab.ChannelConfig, error) { return chconfig.New(channelID) }, channelID)
if err != nil {
return nil, err
}
cfg, err := c.chCfgCache.Get(key)
if err != nil {
return nil, err
}
return cfg.(*chconfig.Ref), nil
}
func (c *contextCache) GetMembership(channelID string) (fab.ChannelMembership, error) {
chCfgRef, err := c.loadChannelCfgRef(channelID)
if err != nil {
return nil, err
}
key, err := membership.NewCacheKey(membership.Context{Providers: c.ctx, EndpointConfig: c.ctx.EndpointConfig()},
chCfgRef.Reference, channelID)
if err != nil {
return nil, err
}
ref, err := c.membershipCache.Get(key)
if err != nil {
return nil, err
}
return ref.(*membership.Ref), nil
}