Skip to content

Commit

Permalink
Async connections to endpoints (#4050) (#4056)
Browse files Browse the repository at this point in the history
Add the AsyncConnect flag to the grpc connection code for gateway connections to endorsing peers and orderers

Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
(cherry picked from commit bf9e310)

Co-authored-by: Andrew Coleman <andrew_coleman@uk.ibm.com>
  • Loading branch information
mergify[bot] and andrew-coleman authored Feb 21, 2023
1 parent 4054745 commit 782f239
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/pkg/gateway/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (ef *endpointFactory) newConnection(address string, tlsRootCerts [][]byte)
Certificate: ef.clientCert,
Key: ef.clientKey,
},
DialTimeout: ef.timeout,
DialTimeout: ef.timeout,
AsyncConnect: true,
}
dialOpts, err := config.DialOptions()
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions internal/pkg/gateway/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ SPDX-License-Identifier: Apache-2.0
package gateway

import (
"context"
"testing"
"time"

peerprotos "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/crypto/tlsgen"
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/internal/pkg/comm"
"github.com/hyperledger/fabric/internal/pkg/gateway/mocks"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -53,3 +56,45 @@ func TestMutualTLS(t *testing.T) {
err = endorser.closeConnection()
require.NoError(t, err, "failed to close connection")
}

//go:generate counterfeiter -o mocks/endorserserver.go --fake-name EndorserServer . endorserServer
type endorserServer interface {
peerprotos.EndorserServer
}

var payload = []byte("test response")

func TestAsyncConnect(t *testing.T) {
server, err := comm.NewGRPCServer("127.0.0.1:0", comm.ServerConfig{})
require.NoError(t, err)

endorserServer := &mocks.EndorserServer{}
endorserServer.ProcessProposalReturns(&peerprotos.ProposalResponse{Payload: payload}, nil)
peerprotos.RegisterEndorserServer(server.Server(), endorserServer)

factory := &endpointFactory{
timeout: 10 * time.Second,
}

endorser, err := factory.newEndorser(common.PKIidType{}, server.Address(), "msp1", nil)
require.NoError(t, err, "failed to make async connection to server")

endorse := func() (*peerprotos.ProposalResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return endorser.client.ProcessProposal(ctx, &peerprotos.SignedProposal{})
}

_, err = endorse() // server not started, so should timeout
require.ErrorContains(t, err, "DeadlineExceeded")

go server.Start()
defer server.Stop()

pr, err := endorse()
require.NoError(t, err)
require.Equal(t, payload, pr.GetPayload())

err = endorser.closeConnection()
require.NoError(t, err, "failed to close connection")
}
117 changes: 117 additions & 0 deletions internal/pkg/gateway/mocks/endorserserver.go

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

0 comments on commit 782f239

Please sign in to comment.