Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 6ab7137

Browse files
committed
[FAB-8949] Add Close() to discovery, selection services
Change-Id: I92af6c8d09140b736f03c6528ffc39ead6878e8d Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
1 parent 47d1ef1 commit 6ab7137

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pkg/client/common/selection/dynamicselection/dynamicselection.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package dynamicselection
88

99
import (
1010
"fmt"
11+
"sync"
1112
"time"
1213

1314
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
@@ -42,6 +43,8 @@ type SelectionProvider struct {
4243
lbp pgresolver.LoadBalancePolicy
4344
providers api.Providers
4445
cacheTimeout time.Duration
46+
refs []*selectionService
47+
refLock sync.RWMutex
4548
}
4649

4750
// Opt applies a selection provider option
@@ -114,8 +117,26 @@ func (p *SelectionProvider) CreateSelectionService(channelID string) (fab.Select
114117
if err != nil {
115118
return nil, errors.WithMessage(err, "Failed to create cc policy provider")
116119
}
120+
svc, err := newSelectionService(channelID, p.lbp, ccPolicyProvider, p.cacheTimeout)
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
p.refLock.Lock()
126+
p.refs = append(p.refs, svc)
127+
p.refLock.Unlock()
128+
129+
return svc, nil
130+
}
131+
132+
// Close the selection services created by this provider
133+
func (p *SelectionProvider) Close() {
134+
p.refLock.Lock()
135+
defer p.refLock.Unlock()
117136

118-
return newSelectionService(channelID, p.lbp, ccPolicyProvider, p.cacheTimeout)
137+
for _, ref := range p.refs {
138+
ref.Close()
139+
}
119140
}
120141

121142
func newSelectionService(channelID string, lbp pgresolver.LoadBalancePolicy, ccPolicyProvider CCPolicyProvider, cacheTimeout time.Duration) (*selectionService, error) {
@@ -159,6 +180,10 @@ func (s *selectionService) GetEndorsersForChaincode(chaincodeIDs []string, opts
159180
return resolver.Resolve(params.PeerFilter).Peers(), nil
160181
}
161182

183+
func (s *selectionService) Close() {
184+
s.pgResolvers.Close()
185+
}
186+
162187
func (s *selectionService) getPeerGroupResolver(chaincodeIDs []string) (pgresolver.PeerGroupResolver, error) {
163188
value, err := s.pgResolvers.Get(newResolverKey(s.channelID, chaincodeIDs...))
164189
if err != nil {

pkg/fabsdk/fabsdk.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type options struct {
3939
// Option configures the SDK.
4040
type Option func(opts *options) error
4141

42+
type closeable interface {
43+
Close()
44+
}
45+
4246
// New initializes the SDK based on the set of options provided.
4347
// configProvider provides the application configuration.
4448
func New(cp core.ConfigProvider, opts ...Option) (*FabricSDK, error) {
@@ -233,6 +237,12 @@ func initSDK(sdk *FabricSDK, config core.Config, opts []Option) error {
233237

234238
// Close frees up caches and connections being maintained by the SDK
235239
func (sdk *FabricSDK) Close() {
240+
if pvdr, ok := sdk.provider.DiscoveryProvider().(closeable); ok {
241+
pvdr.Close()
242+
}
243+
if pvdr, ok := sdk.provider.SelectionProvider().(closeable); ok {
244+
pvdr.Close()
245+
}
236246
sdk.provider.InfraProvider().Close()
237247
}
238248

0 commit comments

Comments
 (0)