Skip to content

Commit

Permalink
[FAB-7100] Keepalive options not set for shim
Browse files Browse the repository at this point in the history
The gRPC keepalive refactoring left out
the configuration of the keepalive
options for the shim to use when
connecting to the chaincode server.

This change rectifies that as well as
simplifying the code by removing the
specific chaincode client connection
functions.

Please note that the keepalive settings
for chaincode are statically configured.

Change-Id: I80aa26a4e69b7d85c5d4b32a53bbe37ed70f17df
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Nov 24, 2017
1 parent 784deb5 commit 0fd8426
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 51 deletions.
11 changes: 9 additions & 2 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io/ioutil"
"os"
"strings"
"time"
"unicode/utf8"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -248,10 +249,16 @@ func getPeerAddress() string {

func newPeerClientConnection() (*grpc.ClientConn, error) {
var peerAddress = getPeerAddress()
// set the keepalive options to match static settings for chaincode server
kaOpts := &comm.KeepaliveOptions{
ClientInterval: time.Duration(1) * time.Minute,
ClientTimeout: time.Duration(20) * time.Second,
}
if comm.TLSEnabled() {
return comm.NewChaincodeClientConnectionWithAddress(peerAddress, true, true, comm.InitTLSForShim(key, cert))
return comm.NewClientConnectionWithAddress(peerAddress, true, true,
comm.InitTLSForShim(key, cert), kaOpts)
}
return comm.NewChaincodeClientConnectionWithAddress(peerAddress, true, false, nil)
return comm.NewClientConnectionWithAddress(peerAddress, true, false, nil, kaOpts)
}

func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode) error {
Expand Down
17 changes: 3 additions & 14 deletions core/comm/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,13 @@ func GetPeerTestingAddress(port string) string {
// NewClientConnectionWithAddress Returns a new grpc.ClientConn to the given address
func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled bool,
creds credentials.TransportCredentials, ka *KeepaliveOptions) (*grpc.ClientConn, error) {
return newClientConnectionWithAddressWithKa(peerAddress, block, tslEnabled, creds, ka)
}

// NewChaincodeClientConnectionWithAddress Returns a new chaincode type grpc.ClientConn to the given address
func NewChaincodeClientConnectionWithAddress(peerAddress string, block bool, tslEnabled bool, creds credentials.TransportCredentials) (*grpc.ClientConn, error) {
ka := &KeepaliveOptions{}
return newClientConnectionWithAddressWithKa(peerAddress, block, tslEnabled, creds, ka)
}

// newClientConnectionWithAddressWithKa Returns a new grpc.ClientConn to the given address using specied keepalive options
func newClientConnectionWithAddressWithKa(peerAddress string, block bool, tslEnabled bool, creds credentials.TransportCredentials, ka *KeepaliveOptions) (*grpc.ClientConn, error) {
var opts []grpc.DialOption

//preserve old behavior for non chaincode. We probably
//want to change this in future to have peer client
//send keepalives too
if ka != nil {
opts = ClientKeepaliveOptions(ka)
} else {
// set to the default options
opts = ClientKeepaliveOptions(DefaultKeepaliveOptions())
}

if tslEnabled {
Expand Down
35 changes: 0 additions & 35 deletions core/comm/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,6 @@ func TestConnection_Correct(t *testing.T) {
tmpConn.Close()
}

func TestChaincodeConnection_Correct(t *testing.T) {
testutil.SetupTestConfig()
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
peerAddress := GetPeerTestingAddress("7052")
var tmpConn *grpc.ClientConn
var err error
if TLSEnabled() {
tmpConn, err = NewChaincodeClientConnectionWithAddress(peerAddress, true, true, InitTLSForPeer())
}
tmpConn, err = NewChaincodeClientConnectionWithAddress(peerAddress, true, false, nil)
if err != nil {
t.Fatalf("error connection to server at host:port = %s\n", peerAddress)
}

tmpConn.Close()
}

func TestConnection_WrongAddress(t *testing.T) {
testutil.SetupTestConfig()
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
Expand All @@ -102,24 +85,6 @@ func TestConnection_WrongAddress(t *testing.T) {
}
}

func TestChaincodeConnection_WrongAddress(t *testing.T) {
testutil.SetupTestConfig()
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
//some random port
peerAddress := GetPeerTestingAddress("10287")
var tmpConn *grpc.ClientConn
var err error
if TLSEnabled() {
tmpConn, err = NewChaincodeClientConnectionWithAddress(peerAddress, true, true, InitTLSForPeer())
}
tmpConn, err = NewChaincodeClientConnectionWithAddress(peerAddress, true, false, nil)
if err == nil {
fmt.Printf("error connection to server - at host:port = %s\n", peerAddress)
t.Error("error connection to server - connection should fail")
tmpConn.Close()
}
}

// utility function to load up our test root certificates from testdata/certs
func loadRootCAs() [][]byte {

Expand Down

0 comments on commit 0fd8426

Please sign in to comment.