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

Commit 20ff232

Browse files
committed
[FAB-6385] Go SDK Configurable timeouts
Change-Id: I9558396750f95e81dc0d00226f59214df5861cb4 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 2578687 commit 20ff232

File tree

13 files changed

+124
-73
lines changed

13 files changed

+124
-73
lines changed

api/apiconfig/configprovider.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Config interface {
2020
CAServerCertFiles(org string) ([]string, error)
2121
CAClientKeyFile(org string) (string, error)
2222
CAClientCertFile(org string) (string, error)
23-
TimeoutOrDefault(ConnectionType) time.Duration
23+
TimeoutOrDefault(TimeoutType) time.Duration
2424
MspID(org string) (string, error)
2525
OrderersConfig() ([]OrdererConfig, error)
2626
RandomOrdererConfig() (*OrdererConfig, error)
@@ -47,16 +47,22 @@ type Config interface {
4747
CSPConfig() *bccspFactory.FactoryOpts
4848
}
4949

50-
// ConnectionType enumerates the different types of outgoing connections
51-
type ConnectionType int
50+
// TimeoutType enumerates the different types of outgoing connections
51+
type TimeoutType int
5252

5353
const (
54-
// Endorser connection
55-
Endorser ConnectionType = iota
56-
// EventHub connection
54+
// Endorser connection timeout
55+
Endorser TimeoutType = iota
56+
// EventHub connection timeout
5757
EventHub
58-
// EventReg connection
58+
// EventReg connection timeout
5959
EventReg
60-
// Orderer connection
61-
Orderer
60+
// Query timeout
61+
Query
62+
// ExecuteTx timeout
63+
ExecuteTx
64+
// OrdererConnection Orderer connection timeout
65+
OrdererConnection
66+
// OrdererSendDeliver Orderer SendDeliver timeout
67+
OrdererResponse
6268
)

api/apiconfig/mocks/mockconfig.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,23 @@ func (c *Config) CAClientCertFile(org string) (string, error) {
211211
}
212212

213213
// TimeoutOrDefault reads connection timeouts for the given connection type
214-
func (c *Config) TimeoutOrDefault(conn apiconfig.ConnectionType) time.Duration {
214+
func (c *Config) TimeoutOrDefault(conn apiconfig.TimeoutType) time.Duration {
215215
var timeout time.Duration
216216
switch conn {
217217
case apiconfig.Endorser:
218-
timeout = myViper.GetDuration("client.connection.timeout.peer.endorser")
218+
timeout = myViper.GetDuration("client.endorserConnectionTimeout")
219219
case apiconfig.EventHub:
220-
timeout = myViper.GetDuration("client.connection.timeout.peer.eventhub")
220+
timeout = myViper.GetDuration("client.eventServiceConnectionTimeout")
221221
case apiconfig.EventReg:
222-
timeout = myViper.GetDuration("client.connection.timeout.peer.eventreg")
223-
case apiconfig.Orderer:
224-
timeout = myViper.GetDuration("client.connection.timeout.orderer")
222+
timeout = myViper.GetDuration("client.eventRegistrationResponseTimeout")
223+
case apiconfig.OrdererConnection:
224+
timeout = myViper.GetDuration("client.ordererConnectionTimeout")
225+
case apiconfig.Query:
226+
timeout = myViper.GetDuration("client.queryResponseTimeout")
227+
case apiconfig.ExecuteTx:
228+
timeout = myViper.GetDuration("client.executeTxResponseTimeout")
229+
case apiconfig.OrdererResponse:
230+
timeout = myViper.GetDuration("client.ordererResponseTimeout")
225231
}
226232
if timeout == 0 {
227233
timeout = defaultTimeout

pkg/config/config.yaml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ client:
4444
logging:
4545
level: info
4646

47-
# set connection timeouts for the peer and orderer for the client
48-
connection:
49-
timeout:
50-
peer:
51-
endorser: 3s
52-
eventHub: 3s
53-
eventReg: 3s
54-
orderer: 3s
47+
5548

5649
# Needed to load users crypto keys and certs.
5750
cryptoconfig:
@@ -94,6 +87,22 @@ client:
9487
#library: "/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/softhsm/libsofthsm2.so ,/usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/powerpc64le-linux-gnu/softhsm/libsofthsm2.so, /usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so"
9588
library: "add BCCSP library here"
9689

90+
#Timeout configs for various connection and response types
91+
endorserConnectionTimeout: 3s
92+
93+
eventServiceConnectionTimeout: 3s
94+
95+
ordererConnectionTimeout: 3s
96+
97+
ordererResponseTimeout: 5s
98+
99+
eventRegistrationResponseTimeout: 3s
100+
101+
queryResponseTimeout: 20s
102+
103+
executeTxResponseTimeout: 30s
104+
105+
97106
#
98107
# [Optional]. But most apps would have this section so that channel objects can be constructed
99108
# based on the content below. If an app is creating channels, then it likely will not need this
@@ -233,7 +242,7 @@ certificateAuthorities:
233242
# Fabric-CA supports dynamic user enrollment via REST APIs. A "root" user, a.k.a registrar, is
234243
# needed to enroll and invoke new users.
235244
# registrar:
236-
# enrollId: usualy-it-is_admin
245+
# enrollId: usually-it-is_admin
237246
# enrollSecret: adminpasswd
238247
# [Optional] The optional name of the CA.
239248
# caName: ca-org1

pkg/config/config_test.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,33 +300,50 @@ func TestTLSACAConfig(t *testing.T) {
300300
}
301301

302302
func TestTimeouts(t *testing.T) {
303-
myViper.Set("client.connection.timeout.peer.endorser", "2s")
304-
myViper.Set("client.connection.timeout.peer.eventhub", "2m")
305-
myViper.Set("client.connection.timeout.peer.eventreg", "2h")
306-
myViper.Set("client.connection.timeout.orderer", "2ms")
303+
myViper.Set("client.endorserConnectionTimeout", "2s")
304+
myViper.Set("client.eventServiceConnectionTimeout", "2m")
305+
myViper.Set("client.eventRegistrationResponseTimeout", "2h")
306+
myViper.Set("client.ordererConnectionTimeout", "2ms")
307+
myViper.Set("client.queryResponseTimeout", "7h")
308+
myViper.Set("client.executeTxResponseTimeout", "8h")
309+
myViper.Set("client.ordererResponseTimeout", "6s")
307310

308311
t1 := configImpl.TimeoutOrDefault(api.Endorser)
309312
if t1 != time.Second*2 {
310313
t.Fatalf("Timeout not read correctly. Got: %s", t1)
311314
}
312-
t2 := configImpl.TimeoutOrDefault(api.EventHub)
313-
if t2 != time.Minute*2 {
314-
t.Fatalf("Timeout not read correctly. Got: %s", t2)
315+
t1 = configImpl.TimeoutOrDefault(api.EventHub)
316+
if t1 != time.Minute*2 {
317+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
318+
}
319+
t1 = configImpl.TimeoutOrDefault(api.EventReg)
320+
if t1 != time.Hour*2 {
321+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
322+
}
323+
t1 = configImpl.TimeoutOrDefault(api.Query)
324+
if t1 != time.Hour*7 {
325+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
326+
}
327+
t1 = configImpl.TimeoutOrDefault(api.ExecuteTx)
328+
if t1 != time.Hour*8 {
329+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
315330
}
316-
t3 := configImpl.TimeoutOrDefault(api.EventReg)
317-
if t3 != time.Hour*2 {
318-
t.Fatalf("Timeout not read correctly. Got: %s", t3)
331+
t1 = configImpl.TimeoutOrDefault(api.OrdererConnection)
332+
if t1 != time.Millisecond*2 {
333+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
319334
}
320-
t4 := configImpl.TimeoutOrDefault(api.Orderer)
321-
if t4 != time.Millisecond*2 {
322-
t.Fatalf("Timeout not read correctly. Got: %s", t4)
335+
t1 = configImpl.TimeoutOrDefault(api.OrdererResponse)
336+
if t1 != time.Second*6 {
337+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
323338
}
339+
324340
// Test default
325-
myViper.Set("client.connection.timeout.orderer", "")
326-
t5 := configImpl.TimeoutOrDefault(api.Orderer)
327-
if t5 != time.Second*5 {
328-
t.Fatalf("Timeout not read correctly. Got: %s", t5)
341+
myViper.Set("client.ordererConnectionTimeout", "")
342+
t1 = configImpl.TimeoutOrDefault(api.OrdererConnection)
343+
if t1 != time.Second*5 {
344+
t.Fatalf("Timeout not read correctly. Got: %s", t1)
329345
}
346+
330347
}
331348

332349
func TestOrdererConfig(t *testing.T) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *MockConfig) FabricClientViper() *viper.Viper {
5858
}
5959

6060
//TimeoutOrDefault not implemented
61-
func (c *MockConfig) TimeoutOrDefault(apiconfig.ConnectionType) time.Duration {
61+
func (c *MockConfig) TimeoutOrDefault(apiconfig.TimeoutType) time.Duration {
6262
return 0
6363
}
6464

pkg/fabric-client/channel/channel.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package channel
99
import (
1010
"fmt"
1111

12+
config "github.com/hyperledger/fabric-sdk-go/api/apiconfig"
1213
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1314
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1415
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
@@ -35,6 +36,7 @@ type ClientContext interface {
3536
UserContext() fab.User
3637
SigningManager() fab.SigningManager
3738
NewTxnID() (apitxn.TransactionID, error)
39+
Config() config.Config
3840
}
3941

4042
// NewChannel represents a channel in a Fabric network.

pkg/fabric-client/channel/txnsender.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1818
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1919

20+
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
2021
protos_utils "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/utils"
2122
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/internal/txnproc"
2223
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
@@ -390,7 +391,7 @@ func (c *Channel) SendEnvelope(envelope *fab.SignedEnvelope) (*common.Block, err
390391
}
391392
mutex.Unlock()
392393

393-
case <-time.After(time.Second * 5):
394+
case <-time.After(c.ClientContext().Config().TimeoutOrDefault(apiconfig.OrdererResponse)):
394395
mutex.Lock()
395396
if errorResponse == nil {
396397
errorResponse = fmt.Errorf("Timeout waiting for response from orderer")

pkg/fabric-client/mocks/mockconfig.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func (c *MockConfig) CAClientCertFile(org string) (string, error) {
6060
}
6161

6262
//TimeoutOrDefault not implemented
63-
func (c *MockConfig) TimeoutOrDefault(config.ConnectionType) time.Duration {
63+
func (c *MockConfig) TimeoutOrDefault(arg config.TimeoutType) time.Duration {
64+
65+
if arg == config.Query || arg == config.ExecuteTx {
66+
return time.Second * 10
67+
}
6468
return 0
6569
}
6670

pkg/fabric-client/orderer/orderer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Orderer struct {
3131
// NewOrderer Returns a Orderer instance
3232
func NewOrderer(url string, certificate string, serverHostOverride string, config apiconfig.Config) (*Orderer, error) {
3333
var opts []grpc.DialOption
34-
opts = append(opts, grpc.WithTimeout(config.TimeoutOrDefault(apiconfig.Orderer)))
34+
opts = append(opts, grpc.WithTimeout(config.TimeoutOrDefault(apiconfig.OrdererConnection)))
3535
if config.IsTLSEnabled() {
3636
tlsCaCertPool, err := config.TLSCACertPool(certificate)
3737
if err != nil {

0 commit comments

Comments
 (0)