Skip to content

Commit

Permalink
[FAB-8949] Add Close() to discovery, selection services
Browse files Browse the repository at this point in the history
Change-Id: I92af6c8d09140b736f03c6528ffc39ead6878e8d
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Mar 18, 2018
1 parent 47d1ef1 commit 6ab7137
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/client/common/selection/dynamicselection/dynamicselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dynamicselection

import (
"fmt"
"sync"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
Expand Down Expand Up @@ -42,6 +43,8 @@ type SelectionProvider struct {
lbp pgresolver.LoadBalancePolicy
providers api.Providers
cacheTimeout time.Duration
refs []*selectionService
refLock sync.RWMutex
}

// Opt applies a selection provider option
Expand Down Expand Up @@ -114,8 +117,26 @@ func (p *SelectionProvider) CreateSelectionService(channelID string) (fab.Select
if err != nil {
return nil, errors.WithMessage(err, "Failed to create cc policy provider")
}
svc, err := newSelectionService(channelID, p.lbp, ccPolicyProvider, p.cacheTimeout)
if err != nil {
return nil, err
}

p.refLock.Lock()
p.refs = append(p.refs, svc)
p.refLock.Unlock()

return svc, nil
}

// Close the selection services created by this provider
func (p *SelectionProvider) Close() {
p.refLock.Lock()
defer p.refLock.Unlock()

return newSelectionService(channelID, p.lbp, ccPolicyProvider, p.cacheTimeout)
for _, ref := range p.refs {
ref.Close()
}
}

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

func (s *selectionService) Close() {
s.pgResolvers.Close()
}

func (s *selectionService) getPeerGroupResolver(chaincodeIDs []string) (pgresolver.PeerGroupResolver, error) {
value, err := s.pgResolvers.Get(newResolverKey(s.channelID, chaincodeIDs...))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type options struct {
// Option configures the SDK.
type Option func(opts *options) error

type closeable interface {
Close()
}

// New initializes the SDK based on the set of options provided.
// configProvider provides the application configuration.
func New(cp core.ConfigProvider, opts ...Option) (*FabricSDK, error) {
Expand Down Expand Up @@ -233,6 +237,12 @@ func initSDK(sdk *FabricSDK, config core.Config, opts []Option) error {

// Close frees up caches and connections being maintained by the SDK
func (sdk *FabricSDK) Close() {
if pvdr, ok := sdk.provider.DiscoveryProvider().(closeable); ok {
pvdr.Close()
}
if pvdr, ok := sdk.provider.SelectionProvider().(closeable); ok {
pvdr.Close()
}
sdk.provider.InfraProvider().Close()
}

Expand Down

0 comments on commit 6ab7137

Please sign in to comment.