This repository has been archived by the owner on May 7, 2023. It is now read-only.
forked from hyperledger/fabric-sdk-go
/
service.go
169 lines (139 loc) · 4.46 KB
/
service.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package dynamicdiscovery
import (
"context"
"sync"
"time"
discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
coptions "github.com/hyperledger/fabric-sdk-go/pkg/common/options"
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
fabdiscovery "github.com/hyperledger/fabric-sdk-go/pkg/fab/discovery"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref"
"github.com/pkg/errors"
)
// DiscoveryClient is the client to the discovery service
type DiscoveryClient interface {
Send(ctx context.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
}
const (
// AccessDenied indicates that the user does not have permission to perform the operation
AccessDenied = "access denied"
)
// DiscoveryError is an error originating at the Discovery service
type DiscoveryError error
// clientProvider is overridden by unit tests
var clientProvider = func(ctx contextAPI.Client) (DiscoveryClient, error) {
return fabdiscovery.New(ctx)
}
// service implements a dynamic Discovery Service that queries
// Fabric's Discovery service for information about the peers that
// are currently joined to the given channel.
type service struct {
responseTimeout time.Duration
lock sync.RWMutex
ctx contextAPI.Client
discClient DiscoveryClient
peersRef *lazyref.Reference
ErrHandler fab.ErrorHandler
}
type queryPeers func() ([]fab.Peer, error)
func newService(config fab.EndpointConfig, query queryPeers, opts ...coptions.Opt) *service {
opt := options{}
coptions.Apply(&opt, opts)
if opt.refreshInterval == 0 {
opt.refreshInterval = config.Timeout(fab.DiscoveryServiceRefresh)
}
if opt.responseTimeout == 0 {
opt.responseTimeout = config.Timeout(fab.DiscoveryResponse)
}
logger.Debugf("Cache refresh interval: %s", opt.refreshInterval)
logger.Debugf("Deliver service response timeout: %s", opt.responseTimeout)
return &service{
responseTimeout: opt.responseTimeout,
ErrHandler: opt.errHandler,
peersRef: lazyref.New(
func() (interface{}, error) {
return query()
},
lazyref.WithRefreshInterval(lazyref.InitOnFirstAccess, opt.refreshInterval),
),
}
}
// initialize initializes the service with client context
func (s *service) initialize(ctx contextAPI.Client) error {
s.lock.Lock()
defer s.lock.Unlock()
if s.ctx != nil {
// Already initialized
logger.Debugf("Already initialized with context: %#v", s.ctx)
return nil
}
discoveryClient, err := clientProvider(ctx)
if err != nil {
return errors.Wrap(err, "error creating discover client")
}
logger.Debugf("Initializing with context: %#v", ctx)
s.ctx = ctx
s.discClient = discoveryClient
return nil
}
// Close stops the lazyref background refresh
func (s *service) Close() {
logger.Debug("Closing peers ref...")
s.peersRef.Close()
}
// GetPeers returns the available peers
func (s *service) GetPeers() ([]fab.Peer, error) {
if s.peersRef.IsClosed() {
return nil, errors.Errorf("Discovery client has been closed")
}
refValue, err := s.peersRef.Get()
if err != nil {
return nil, err
}
peers, ok := refValue.([]fab.Peer)
if !ok {
return nil, errors.New("get peersRef didn't return Peer type")
}
return peers, nil
}
func (s *service) context() contextAPI.Client {
s.lock.RLock()
defer s.lock.RUnlock()
return s.ctx
}
func (s *service) discoveryClient() DiscoveryClient {
s.lock.RLock()
defer s.lock.RUnlock()
return s.discClient
}
func asPeers(ctx contextAPI.Client, endpoints []*discclient.Peer) []fab.Peer {
var peers []fab.Peer
for _, endpoint := range endpoints {
peer, ok := asPeer(ctx, endpoint)
if !ok {
continue
}
peers = append(peers, peer)
}
return peers
}
func asPeer(ctx contextAPI.Client, endpoint *discclient.Peer) (fab.Peer, bool) {
url := endpoint.AliveMessage.GetAliveMsg().Membership.Endpoint
logger.Debugf("Adding endpoint [%s]", url)
peerConfig, found := ctx.EndpointConfig().PeerConfig(url)
if !found {
logger.Debugf("Peer config not found for url [%s]", url)
return nil, false
}
peer, err := ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: *peerConfig, MSPID: endpoint.MSPID})
if err != nil {
logger.Warnf("Unable to create peer config for [%s]: %s", url, err)
return nil, false
}
return peer, true
}