Skip to content

Commit

Permalink
[FAB-7968] Remove channel client async option
Browse files Browse the repository at this point in the history
Change-Id: I10c46419c449455df1c7741e6d24709d12058ea6
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Jan 30, 2018
1 parent b0efb7e commit e985ca4
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 317 deletions.
8 changes: 0 additions & 8 deletions api/apitxn/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ func WithTimeout(timeout time.Duration) Option {
}
}

//WithNotifier encapsulates Response to Option
func WithNotifier(notifier chan Response) Option {
return func(opts *Opts) error {
opts.Notifier = notifier
return nil
}
}

//WithProposalProcessor encapsulates ProposalProcessors to Option
func WithProposalProcessor(proposalProcessors ...ProposalProcessor) Option {
return func(opts *Opts) error {
Expand Down
5 changes: 2 additions & 3 deletions api/apitxn/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Response struct {

// Opts allows the user to specify more advanced options
type Opts struct {
Notifier chan Response // async
ProposalProcessors []ProposalProcessor // targets
Timeout time.Duration
}
Expand Down Expand Up @@ -71,10 +70,10 @@ type CCEvent struct {
type ChannelClient interface {

// Query chaincode with request and optional options provided
Query(request Request, opts ...Option) ([]byte, error)
Query(request Request, opts ...Option) Response

// Execute execute transaction with request and optional options provided
Execute(request Request, opts ...Option) ([]byte, TransactionID, error)
Execute(request Request, opts ...Option) Response

// RegisterChaincodeEvent registers chain code event
// @param {chan bool} channel which receives event details when the event is complete
Expand Down
41 changes: 16 additions & 25 deletions pkg/fabric-txn/chclient/chclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apitxn/txnhandler"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
txnHandlerImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/txnhandler"
"github.com/hyperledger/fabric-sdk-go/pkg/status"
)

const (
Expand Down Expand Up @@ -57,19 +58,13 @@ func New(c Context) (*ChannelClient, error) {
}

// Query chaincode using request and optional options provided
func (cc *ChannelClient) Query(request apitxn.Request, options ...apitxn.Option) ([]byte, error) {

response := cc.InvokeHandler(txnHandlerImpl.NewQueryHandler(), request, cc.addDefaultTimeout(apiconfig.Query, options...)...)

return response.Payload, response.Error
func (cc *ChannelClient) Query(request apitxn.Request, options ...apitxn.Option) apitxn.Response {
return cc.InvokeHandler(txnHandlerImpl.NewQueryHandler(), request, cc.addDefaultTimeout(apiconfig.Query, options...)...)
}

// Execute prepares and executes transaction using request and optional options provided
func (cc *ChannelClient) Execute(request apitxn.Request, options ...apitxn.Option) ([]byte, apitxn.TransactionID, error) {

response := cc.InvokeHandler(txnHandlerImpl.NewExecuteHandler(), request, cc.addDefaultTimeout(apiconfig.Execute, options...)...)

return response.Payload, response.TransactionID, response.Error
func (cc *ChannelClient) Execute(request apitxn.Request, options ...apitxn.Option) apitxn.Response {
return cc.InvokeHandler(txnHandlerImpl.NewExecuteHandler(), request, cc.addDefaultTimeout(apiconfig.Execute, options...)...)
}

//InvokeHandler invokes handler using request and options provided
Expand All @@ -87,19 +82,19 @@ func (cc *ChannelClient) InvokeHandler(handler txnhandler.Handler, request apitx
return apitxn.Response{Error: err}
}

//Perform action through handler
go handler.Handle(requestContext, clientContext)

//notifier in options will handle response if provided
if txnOpts.Notifier != nil {
return apitxn.Response{}
}
complete := make(chan bool)

go func() {
//Perform action through handler
handler.Handle(requestContext, clientContext)
complete <- true
}()
select {
case response := <-requestContext.Opts.Notifier:
return response
case <-time.After(requestContext.Opts.Timeout):
return apitxn.Response{Error: errors.New("handler timed out while performing operation")}
case <-complete:
return requestContext.Response
case <-time.After(txnOpts.Timeout):
return apitxn.Response{Error: status.New(status.ClientStatus, status.Timeout.ToInt32(),
"Operation timed out", nil)}
}
}

Expand Down Expand Up @@ -127,10 +122,6 @@ func (cc *ChannelClient) prepareHandlerContexts(request apitxn.Request, options
requestContext.Opts.Timeout = defaultHandlerTimeout
}

if requestContext.Opts.Notifier == nil {
requestContext.Opts.Notifier = make(chan apitxn.Response)
}

return requestContext, clientContext, nil

}
Expand Down
Loading

0 comments on commit e985ca4

Please sign in to comment.