Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate event service if WithNoCache() opt used #225

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions pkg/client/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Client struct {
fromBlock uint64
seekType seek.Type
eventConsumerTimeout *time.Duration
noCacheInit bool
}

// New returns a Client instance. Client receives events such as block, filtered block,
Expand All @@ -58,24 +59,7 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client
return nil, errors.New("channel service not initialized")
}

var es fab.EventService
if eventClient.permitBlockEvents {
var opts []options.Opt
opts = append(opts, client.WithBlockEvents())
if eventClient.seekType != "" {
opts = append(opts, deliverclient.WithSeekType(eventClient.seekType))
if eventClient.seekType == seek.FromBlock {
opts = append(opts, deliverclient.WithBlockNum(eventClient.fromBlock))
}
}
if eventClient.eventConsumerTimeout != nil {
opts = append(opts, dispatcher.WithEventConsumerTimeout(*eventClient.eventConsumerTimeout))
}
es, err = channelContext.ChannelService().EventService(opts...)
} else {
es, err = channelContext.ChannelService().EventService()
}

es, err := eventService(channelContext, &eventClient)
if err != nil {
return nil, errors.WithMessage(err, "event service creation failed")
}
Expand Down Expand Up @@ -130,3 +114,31 @@ func (c *Client) RegisterTxStatusEvent(txID string) (fab.Registration, <-chan *f
func (c *Client) Unregister(reg fab.Registration) {
c.eventService.Unregister(reg)
}

func blockEventOpts(eventClient *Client) (opts []options.Opt) {
opts = append(opts, client.WithBlockEvents())
if eventClient.seekType != "" {
opts = append(opts, deliverclient.WithSeekType(eventClient.seekType))
if eventClient.seekType == seek.FromBlock {
opts = append(opts, deliverclient.WithBlockNum(eventClient.fromBlock))
}
}
if eventClient.eventConsumerTimeout != nil {
opts = append(opts, dispatcher.WithEventConsumerTimeout(*eventClient.eventConsumerTimeout))
}
return
}

func eventService(channelContext context.Channel, eventClient *Client) (es fab.EventService, err error) {
if eventClient.permitBlockEvents {
opts := blockEventOpts(eventClient)
if eventClient.noCacheInit {
es, err = channelContext.ChannelService().EventServiceNoCache(opts...)
} else {
es, err = channelContext.ChannelService().EventService(opts...)
}
} else {
es, err = channelContext.ChannelService().EventService()
}
return
}
8 changes: 8 additions & 0 deletions pkg/client/event/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
// ClientOption describes a functional parameter for the New constructor
type ClientOption func(*Client) error

// WithNoCache indicates that event service must be initialized without cache.
func WithNoCache() ClientOption {
return func(c *Client) error {
c.noCacheInit = true
return nil
}
}

// WithBlockEvents indicates that block events are to be received.
// Note that the caller must have sufficient privileges for this option.
func WithBlockEvents() ClientOption {
Expand Down
17 changes: 17 additions & 0 deletions pkg/client/resmgmt/mockchannelservice.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/common/providers/fab/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type ChannelService interface {
Config() (ChannelConfig, error)
EventService(opts ...options.Opt) (EventService, error)
EventServiceNoCache(opts ...options.Opt) (EventService, error)
Membership() (ChannelMembership, error)
ChannelConfig() (ChannelCfg, error)
Transactor(reqCtx reqContext.Context) (Transactor, error)
Expand Down
5 changes: 5 additions & 0 deletions pkg/fab/mocks/mockchprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (cs *MockChannelService) EventService(opts ...options.Opt) (fab.EventServic
return NewMockEventService(), nil
}

// EventServiceNoCache returns a mock event service
func (cs *MockChannelService) EventServiceNoCache(opts ...options.Opt) (fab.EventService, error) {
return NewMockEventService(), nil
}

// SetTransactor changes the return value of Transactor
func (cs *MockChannelService) SetTransactor(t fab.Transactor) {
cs.transactor = t
Expand Down
6 changes: 6 additions & 0 deletions pkg/fabsdk/provider/chpvdr/chprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func (cs *ChannelService) EventService(opts ...options.Opt) (fab.EventService, e
return cs.ctxtCache.GetEventService(cs.channelID, opts...)
}

// EventServiceNoCache creates EventService and returns it (without using cache).
func (cs *ChannelService) EventServiceNoCache(opts ...options.Opt) (fab.EventService, error) {
cs.ctxtCache.Drain(DrainEventSevice)
return cs.ctxtCache.GetEventService(cs.channelID, opts...)
}

// Membership returns and caches a channel member identifier
// A membership reference is returned that refreshes with the configured interval
func (cs *ChannelService) Membership() (fab.ChannelMembership, error) {
Expand Down
33 changes: 33 additions & 0 deletions pkg/fabsdk/provider/chpvdr/contextcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ import (
"github.com/pkg/errors"
)

type drainType int

const (
// DrainEventSevice used to drain event service cache
DrainEventSevice drainType = iota + 1
// DrainMembershipCache used to drain membership service cache
DrainMembershipCache
// DrainChCfgCache used to drain channel config cache
DrainChCfgCache
// DrainSelectionServiceCache used to drain selection service cache
DrainSelectionServiceCache
// DrainDiscoveryServiceCache used to drain discovery service cache
DrainDiscoveryServiceCache
)

type cache interface {
Get(lazycache.Key, ...interface{}) (interface{}, error)
Close()
DeleteAll()
}

type contextCache struct {
Expand Down Expand Up @@ -82,6 +98,22 @@ func newContextCache(ctx fab.ClientContext, opts []options.Opt) *contextCache {
return c
}

// Drain deletes all entries from the specified cache.
func (c *contextCache) Drain(whatToDrain drainType) {
switch whatToDrain {
case DrainEventSevice:
c.eventServiceCache.DeleteAll()
case DrainMembershipCache:
c.membershipCache.DeleteAll()
case DrainChCfgCache:
c.chCfgCache.DeleteAll()
case DrainSelectionServiceCache:
c.selectionServiceCache.DeleteAll()
case DrainDiscoveryServiceCache:
c.discoveryServiceCache.DeleteAll()
}
}

func (c *contextCache) Close() {
logger.Debug("Closing event service cache...")
c.eventServiceCache.Close()
Expand Down Expand Up @@ -170,6 +202,7 @@ func (c *contextCache) GetEventService(channelID string, opts ...options.Opt) (f
if err != nil {
return nil, err
}

eventService, err := c.eventServiceCache.Get(key)
if err != nil {
return nil, err
Expand Down