Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 3da99de

Browse files
committed
[FAB-5016] Configurable connection timeouts
Change-Id: I637abb8ebdfd2c09e9a6b3e1a63795d780d9f87e Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
1 parent a24a856 commit 3da99de

File tree

14 files changed

+150
-26
lines changed

14 files changed

+150
-26
lines changed

api/apiconfig/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package apiconfig
88

99
import (
1010
"crypto/x509"
11+
"time"
1112

1213
bccspFactory "github.com/hyperledger/fabric/bccsp/factory"
1314
)
@@ -18,6 +19,7 @@ type Config interface {
1819
CAServerCertFiles(org string) ([]string, error)
1920
CAClientKeyFile(org string) (string, error)
2021
CAClientCertFile(org string) (string, error)
22+
TimeoutOrDefault(ConnectionType) time.Duration
2123
MspID(org string) (string, error)
2224
OrderersConfig() ([]OrdererConfig, error)
2325
RandomOrdererConfig() (*OrdererConfig, error)
@@ -37,3 +39,17 @@ type Config interface {
3739
CryptoConfigPath() string
3840
CSPConfig() *bccspFactory.FactoryOpts
3941
}
42+
43+
// ConnectionType enumerates the different types of outgoing connections
44+
type ConnectionType int
45+
46+
const (
47+
// Endorser connection
48+
Endorser ConnectionType = iota
49+
// EventHub connection
50+
EventHub
51+
// EventReg connection
52+
EventReg
53+
// Orderer connection
54+
Orderer
55+
)

api/apiconfig/mocks/mockconfig.gen.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ var format = logging.MustStringFormatter(
3030
`%{color}%{time:15:04:05.000} [%{module}] %{level:.4s} : %{color:reset} %{message}`,
3131
)
3232

33-
const cmdRoot = "fabric_sdk"
33+
const (
34+
cmdRoot = "fabric_sdk"
35+
defaultTimeout = time.Second * 5
36+
)
3437

3538
// Config represents the configuration for the client
3639
type Config struct {
@@ -133,6 +136,26 @@ func (c *Config) CAClientCertFile(org string) (string, error) {
133136
"$GOPATH", os.Getenv("GOPATH"), -1), nil
134137
}
135138

139+
// TimeoutOrDefault reads connection timeouts for the given connection type
140+
func (c *Config) TimeoutOrDefault(conn apiconfig.ConnectionType) time.Duration {
141+
var timeout time.Duration
142+
switch conn {
143+
case apiconfig.Endorser:
144+
timeout = myViper.GetDuration("connection.timeout.peer.endorser")
145+
case apiconfig.EventHub:
146+
timeout = myViper.GetDuration("connection.timeout.peer.eventhub")
147+
case apiconfig.EventReg:
148+
timeout = myViper.GetDuration("connection.timeout.peer.eventreg")
149+
case apiconfig.Orderer:
150+
timeout = myViper.GetDuration("connection.timeout.orderer")
151+
}
152+
if timeout == 0 {
153+
timeout = defaultTimeout
154+
}
155+
156+
return timeout
157+
}
158+
136159
// MspID returns the MSP ID for the requested organization
137160
func (c *Config) MspID(org string) (string, error) {
138161
config, err := c.NetworkConfig()

pkg/config/config_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414
"testing"
15+
"time"
1516

1617
api "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1718
"github.com/spf13/viper"
@@ -208,6 +209,36 @@ func TestTLSACAConfig(t *testing.T) {
208209
}
209210
}
210211

212+
func TestTimeouts(t *testing.T) {
213+
myViper.Set("connection.timeout.peer.endorser", "2s")
214+
myViper.Set("connection.timeout.peer.eventhub", "2m")
215+
myViper.Set("connection.timeout.peer.eventreg", "2h")
216+
myViper.Set("connection.timeout.orderer", "2ms")
217+
218+
t1 := configImpl.TimeoutOrDefault(api.Endorser)
219+
if t1 != time.Second*2 {
220+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
221+
}
222+
t2 := configImpl.TimeoutOrDefault(api.EventHub)
223+
if t2 != time.Minute*2 {
224+
t.Fatalf("Timeout not read correctly. Got: %s", t2)
225+
}
226+
t3 := configImpl.TimeoutOrDefault(api.EventReg)
227+
if t3 != time.Hour*2 {
228+
t.Fatalf("Timeout not read correctly. Got: %s", t3)
229+
}
230+
t4 := configImpl.TimeoutOrDefault(api.Orderer)
231+
if t4 != time.Millisecond*2 {
232+
t.Fatalf("Timeout not read correctly. Got: %s", t4)
233+
}
234+
// Test default
235+
myViper.Set("connection.timeout.orderer", "")
236+
t5 := configImpl.TimeoutOrDefault(api.Orderer)
237+
if t5 != time.Second*5 {
238+
t.Fatalf("Timeout not read correctly. Got: %s", t5)
239+
}
240+
}
241+
211242
func TestOrdererConfig(t *testing.T) {
212243
oConfig, err := configImpl.RandomOrdererConfig()
213244

pkg/fabric-ca-client/mocks/mockconfig.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package mocks
88

99
import (
1010
"crypto/x509"
11+
"time"
1112

1213
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1314

@@ -51,6 +52,11 @@ func (c *MockConfig) FabricClientViper() *viper.Viper {
5152
return nil
5253
}
5354

55+
//TimeoutOrDefault not implemented
56+
func (c *MockConfig) TimeoutOrDefault(apiconfig.ConnectionType) time.Duration {
57+
return 0
58+
}
59+
5460
// PeersConfig Retrieves the fabric peers from the config file provided
5561
func (c *MockConfig) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
5662
return nil, nil

pkg/fabric-client/events/consumer/consumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewEventsClient(client fab.FabricClient, peerAddress string, certificate st
5858
//newEventsClientConnectionWithAddress Returns a new grpc.ClientConn to the configured local PEER.
5959
func newEventsClientConnectionWithAddress(peerAddress string, certificate string, serverhostoverride string, config apiconfig.Config) (*grpc.ClientConn, error) {
6060
var opts []grpc.DialOption
61-
opts = append(opts, grpc.WithTimeout(time.Second*3))
61+
opts = append(opts, grpc.WithTimeout(config.TimeoutOrDefault(apiconfig.EventHub)))
6262
if config.IsTLSEnabled() {
6363
tlsCaCertPool, err := config.TLSCACertPool(certificate)
6464
if err != nil {

pkg/fabric-client/events/eventhub.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/golang/protobuf/proto"
19+
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1920
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
2021
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
2122
consumer "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/events/consumer"
@@ -245,7 +246,9 @@ func (eventHub *EventHub) Connect() error {
245246
}
246247

247248
if eventHub.grpcClient == nil {
248-
eventsClient, _ := eventHub.eventsClientFactory.newEventsClient(eventHub.client, eventHub.peerAddr, eventHub.peerTLSCertificate, eventHub.peerTLSServerHostOverride, 5, eventHub)
249+
eventsClient, _ := eventHub.eventsClientFactory.newEventsClient(eventHub.client,
250+
eventHub.peerAddr, eventHub.peerTLSCertificate, eventHub.peerTLSServerHostOverride,
251+
eventHub.client.Config().TimeoutOrDefault(apiconfig.EventReg), eventHub)
249252
eventHub.grpcClient = eventsClient
250253
}
251254

pkg/fabric-client/mocks/mockconfig.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package mocks
88

99
import (
1010
"crypto/x509"
11+
"time"
1112

1213
config "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1314

@@ -53,6 +54,11 @@ func (c *MockConfig) CAClientCertFile(org string) (string, error) {
5354
return "", nil
5455
}
5556

57+
//TimeoutOrDefault not implemented
58+
func (c *MockConfig) TimeoutOrDefault(config.ConnectionType) time.Duration {
59+
return 0
60+
}
61+
5662
// FabricClientViper returns the internal viper instance used by the
5763
// SDK to read configuration options
5864
func (c *MockConfig) FabricClientViper() *viper.Viper {

pkg/fabric-client/orderer/orderer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ package orderer
88

99
import (
1010
"fmt"
11-
"time"
1211

13-
config "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
12+
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1413
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1514
"google.golang.org/grpc/credentials"
1615

@@ -30,9 +29,9 @@ type Orderer struct {
3029
}
3130

3231
// NewOrderer Returns a Orderer instance
33-
func NewOrderer(url string, certificate string, serverHostOverride string, config config.Config) (*Orderer, error) {
32+
func NewOrderer(url string, certificate string, serverHostOverride string, config apiconfig.Config) (*Orderer, error) {
3433
var opts []grpc.DialOption
35-
opts = append(opts, grpc.WithTimeout(time.Second*3))
34+
opts = append(opts, grpc.WithTimeout(config.TimeoutOrDefault(apiconfig.Orderer)))
3635
if config.IsTLSEnabled() {
3736
tlsCaCertPool, err := config.TLSCACertPool(certificate)
3837
if err != nil {

pkg/fabric-client/peer/peer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package peer
88

99
import (
1010
"encoding/pem"
11-
"time"
1211

1312
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1413
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
@@ -19,7 +18,6 @@ import (
1918
var logger = logging.MustGetLogger("fabric_sdk_go")
2019

2120
const (
22-
connTimeout = time.Second * 10 // TODO: should be configurable
2321
connBlocking = true
2422
)
2523

@@ -40,7 +38,7 @@ type Peer struct {
4038
// serverNameOverride is passed to NewClientTLSFromCert in grpc/credentials.
4139
func NewPeerTLSFromCert(url string, certificate string, serverHostOverride string, config apiconfig.Config) (*Peer, error) {
4240
// TODO: config is declaring TLS but cert & serverHostOverride is being passed-in...
43-
conn, err := newPeerEndorser(url, certificate, serverHostOverride, connTimeout, connBlocking, config)
41+
conn, err := newPeerEndorser(url, certificate, serverHostOverride, connBlocking, config)
4442
if err != nil {
4543
return nil, err
4644
}
@@ -51,7 +49,7 @@ func NewPeerTLSFromCert(url string, certificate string, serverHostOverride strin
5149
// NewPeer constructs a Peer given its endpoint configuration settings.
5250
// url is the URL with format of "host:port".
5351
func NewPeer(url string, config apiconfig.Config) (*Peer, error) {
54-
conn, err := newPeerEndorser(url, "", "", connTimeout, connBlocking, config)
52+
conn, err := newPeerEndorser(url, "", "", connBlocking, config)
5553
if err != nil {
5654
return nil, err
5755
}

0 commit comments

Comments
 (0)