Skip to content

Commit

Permalink
[FABG-824] Allow for provider-specific options
Browse files Browse the repository at this point in the history
Added a 'WithProviderOptions' option in the SDK to allow for
provider-specific options.

Change-Id: Ib75b948e3a0bd82ce971adf388fca33889647db9
Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn committed Mar 5, 2019
1 parent bda01c9 commit 5fd3e98
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 86 deletions.
12 changes: 6 additions & 6 deletions pkg/client/common/discovery/dynamicdiscovery/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func WithRefreshInterval(value time.Duration) coptions.Opt {
return func(p coptions.Params) {
logger.Debug("Checking refreshIntervalSetter")
if setter, ok := p.(refreshIntervalSetter); ok {
setter.SetRefreshInterval(value)
setter.SetDiscoveryRefreshInterval(value)
}
}
}
Expand All @@ -33,25 +33,25 @@ func WithResponseTimeout(value time.Duration) coptions.Opt {
return func(p coptions.Params) {
logger.Debug("Checking responseTimeoutSetter")
if setter, ok := p.(responseTimeoutSetter); ok {
setter.SetResponseTimeout(value)
setter.SetDiscoveryResponseTimeout(value)
}
}
}

type refreshIntervalSetter interface {
SetRefreshInterval(value time.Duration)
SetDiscoveryRefreshInterval(value time.Duration)
}

type responseTimeoutSetter interface {
SetResponseTimeout(value time.Duration)
SetDiscoveryResponseTimeout(value time.Duration)
}

func (o *options) SetRefreshInterval(value time.Duration) {
func (o *options) SetDiscoveryRefreshInterval(value time.Duration) {
logger.Debugf("RefreshInterval: %s", value)
o.refreshInterval = value
}

func (o *options) SetResponseTimeout(value time.Duration) {
func (o *options) SetDiscoveryResponseTimeout(value time.Duration) {
logger.Debugf("ResponseTimeout: %s", value)
o.responseTimeout = value
}
19 changes: 9 additions & 10 deletions pkg/client/common/selection/fabricselection/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"

coptions "github.com/hyperledger/fabric-sdk-go/pkg/common/options"
)

Expand All @@ -26,7 +25,7 @@ func WithRefreshInterval(value time.Duration) coptions.Opt {
return func(p coptions.Params) {
logger.Debug("Checking refreshIntervalSetter")
if setter, ok := p.(refreshIntervalSetter); ok {
setter.SetRefreshInterval(value)
setter.SetSelectionRefreshInterval(value)
}
}
}
Expand All @@ -36,7 +35,7 @@ func WithResponseTimeout(value time.Duration) coptions.Opt {
return func(p coptions.Params) {
logger.Debug("Checking responseTimeoutSetter")
if setter, ok := p.(responseTimeoutSetter); ok {
setter.SetResponseTimeout(value)
setter.SetSelectionResponseTimeout(value)
}
}
}
Expand All @@ -47,34 +46,34 @@ func WithRetryOpts(value retry.Opts) coptions.Opt {
return func(p coptions.Params) {
logger.Debug("Checking retryOptsSetter")
if setter, ok := p.(retryOptsSetter); ok {
setter.SetRetryOpts(value)
setter.SetSelectionRetryOpts(value)
}
}
}

type refreshIntervalSetter interface {
SetRefreshInterval(value time.Duration)
SetSelectionRefreshInterval(value time.Duration)
}

type responseTimeoutSetter interface {
SetResponseTimeout(value time.Duration)
SetSelectionResponseTimeout(value time.Duration)
}

type retryOptsSetter interface {
SetRetryOpts(value retry.Opts)
SetSelectionRetryOpts(value retry.Opts)
}

func (o *params) SetRefreshInterval(value time.Duration) {
func (o *params) SetSelectionRefreshInterval(value time.Duration) {
logger.Debugf("RefreshInterval: %s", value)
o.refreshInterval = value
}

func (o *params) SetResponseTimeout(value time.Duration) {
func (o *params) SetSelectionResponseTimeout(value time.Duration) {
logger.Debugf("ResponseTimeout: %s", value)
o.responseTimeout = value
}

func (o *params) SetRetryOpts(value retry.Opts) {
func (o *params) SetSelectionRetryOpts(value retry.Opts) {
logger.Debugf("RetryOpts: %#v", value)
o.retryOpts = value
}
8 changes: 3 additions & 5 deletions pkg/fab/chconfig/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ SPDX-License-Identifier: Apache-2.0
package chconfig

import (
"time"

"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/util/concurrent/lazycache"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -46,13 +44,13 @@ func NewCacheKey(ctx fab.ClientContext, pvdr Provider, channelID string) (CacheK

// NewRefCache a cache of channel config references that refreshed with the
// given interval
func NewRefCache(refresh time.Duration) *lazycache.Cache {
func NewRefCache(opts ...options.Opt) *lazycache.Cache {
initializer := func(key lazycache.Key) (interface{}, error) {
ck, ok := key.(CacheKey)
if !ok {
return nil, errors.New("unexpected cache key")
}
return NewRef(refresh, ck.Provider(), ck.ChannelID(), ck.Context()), nil
return NewRef(ck.Context(), ck.Provider(), ck.ChannelID(), opts...), nil
}

return lazycache.New("Channel_Cfg_Cache", initializer)
Expand Down
6 changes: 3 additions & 3 deletions pkg/fab/chconfig/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestChannelConfigCache(t *testing.T) {
user := mspmocks.NewMockSigningIdentity("user", "user")
clientCtx := mocks.NewMockContext(user)

cache := NewRefCache(time.Millisecond * 10)
cache := NewRefCache(WithRefreshInterval(time.Millisecond * 10))
assert.NotNil(t, cache)

key, err := NewCacheKey(clientCtx, mockProvider, "test")
Expand All @@ -42,7 +42,7 @@ func TestChannelConfigCacheBad(t *testing.T) {
user := mspmocks.NewMockSigningIdentity("user", "user")
clientCtx := mocks.NewMockContext(user)

cache := NewRefCache(time.Millisecond * 10)
cache := NewRefCache(WithRefreshInterval(time.Millisecond * 10))
assert.NotNil(t, cache)

r, err := cache.Get(&badKey{s: "test"})
Expand All @@ -54,7 +54,7 @@ func TestChannelConfigCacheBad(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, key)

cache = NewRefCache(time.Millisecond * 10)
cache = NewRefCache(WithRefreshInterval(time.Millisecond * 10))
assert.NotNil(t, cache)

r, err = cache.Get(key)
Expand Down
3 changes: 1 addition & 2 deletions pkg/fab/chconfig/chconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"regexp"

"github.com/golang/protobuf/proto"
"github.com/pkg/errors"

channelConfig "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/channelconfig"
imsp "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
Expand All @@ -27,6 +25,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
mb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)

var logger = logging.NewLogger("fabsdk/fab")
Expand Down
46 changes: 46 additions & 0 deletions pkg/fab/chconfig/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package chconfig

import (
"time"

coptions "github.com/hyperledger/fabric-sdk-go/pkg/common/options"
)

const (
defaultRefreshInterval = time.Second * 90
)

type params struct {
refreshInterval time.Duration
}

func newDefaultParams() *params {
return &params{
refreshInterval: defaultRefreshInterval,
}
}

// WithRefreshInterval sets the interval in which the
// channel config cache is refreshed
func WithRefreshInterval(value time.Duration) coptions.Opt {
return func(p coptions.Params) {
if setter, ok := p.(refreshIntervalSetter); ok {
setter.SetChConfigRefreshInterval(value)
}
}
}

type refreshIntervalSetter interface {
SetChConfigRefreshInterval(value time.Duration)
}

func (o *params) SetChConfigRefreshInterval(value time.Duration) {
logger.Debugf("RefreshInterval: %s", value)
o.refreshInterval = value
}
13 changes: 9 additions & 4 deletions pkg/fab/chconfig/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package chconfig

import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref"
Expand All @@ -23,8 +22,14 @@ type Ref struct {
channelID string
}

// ChannelConfigError is returned when the channel config could not be refreshed
type ChannelConfigError error

// NewRef returns a new channel config reference
func NewRef(refresh time.Duration, pvdr Provider, channel string, ctx fab.ClientContext) *Ref {
func NewRef(ctx fab.ClientContext, pvdr Provider, channel string, opts ...options.Opt) *Ref {
params := newDefaultParams()
options.Apply(params, opts)

cfgRef := &Ref{
pvdr: pvdr,
ctx: ctx,
Expand All @@ -33,7 +38,7 @@ func NewRef(refresh time.Duration, pvdr Provider, channel string, ctx fab.Client

cfgRef.Reference = lazyref.New(
cfgRef.initializer(),
lazyref.WithRefreshInterval(lazyref.InitImmediately, refresh),
lazyref.WithRefreshInterval(lazyref.InitImmediately, params.refreshInterval),
)

return cfgRef
Expand Down
3 changes: 2 additions & 1 deletion pkg/fabsdk/api/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package api

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
Expand Down Expand Up @@ -35,5 +36,5 @@ type MSPProviderFactory interface {
// ServiceProviderFactory allows overriding default service providers (such as peer discovery)
type ServiceProviderFactory interface {
CreateLocalDiscoveryProvider(config fab.EndpointConfig) (fab.LocalDiscoveryProvider, error)
CreateChannelProvider(config fab.EndpointConfig) (fab.ChannelProvider, error)
CreateChannelProvider(config fab.EndpointConfig, opts ...options.Opt) (fab.ChannelProvider, error)
}
23 changes: 16 additions & 7 deletions pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/core/operations"
contextApi "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/metrics"

"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
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/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/api"
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab"
sdkApi "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/metrics"
metricsCfg "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/metrics/cfg"
mspImpl "github.com/hyperledger/fabric-sdk-go/pkg/msp"
"github.com/pkg/errors"
Expand Down Expand Up @@ -57,6 +57,7 @@ type options struct {
endpointConfig fab.EndpointConfig
IdentityConfig msp.IdentityConfig
ConfigBackend []core.ConfigBackend
ProviderOpts []coptions.Opt // Provider options are passed along to the various providers
metricsConfig metricsCfg.MetricsConfig
}

Expand Down Expand Up @@ -202,6 +203,14 @@ func WithMetricsConfig(metricsConfigs ...interface{}) Option {
}
}

// WithProviderOpts adds options which are propagated to the various providers.
func WithProviderOpts(sopts ...coptions.Opt) Option {
return func(opts *options) error {
opts.ProviderOpts = append(opts.ProviderOpts, sopts...)
return nil
}
}

// providerInit interface allows for initializing providers
// TODO: minimize interface
type providerInit interface {
Expand Down Expand Up @@ -261,7 +270,7 @@ func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option)
return errors.WithMessage(err, "failed to create local discovery provider")
}

channelProvider, err := sdk.opts.Service.CreateChannelProvider(cfg.endpointConfig)
channelProvider, err := sdk.opts.Service.CreateChannelProvider(cfg.endpointConfig, sdk.opts.ProviderOpts...)
if err != nil {
return errors.WithMessage(err, "failed to create channel provider")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/fabsdk/factory/defsvc/svcfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ SPDX-License-Identifier: Apache-2.0
package defsvc

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"

discovery "github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
"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/fabsdk/provider/chpvdr"
)

Expand All @@ -29,6 +29,6 @@ func (f *ProviderFactory) CreateLocalDiscoveryProvider(config fab.EndpointConfig
}

// CreateChannelProvider returns a new default implementation of channel provider
func (f *ProviderFactory) CreateChannelProvider(config fab.EndpointConfig) (fab.ChannelProvider, error) {
return chpvdr.New(config)
func (f *ProviderFactory) CreateChannelProvider(config fab.EndpointConfig, opts ...options.Opt) (fab.ChannelProvider, error) {
return chpvdr.New(config, opts...)
}
Loading

0 comments on commit 5fd3e98

Please sign in to comment.