@@ -25,7 +25,13 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
25
25
if options == nil {
26
26
options = NewEnvironmentOptions ()
27
27
}
28
- client := newClient ("go-stream-locator" , nil , options .TCPParameters , options .SaslConfiguration )
28
+
29
+ if options .RPCTimeout <= 0 {
30
+ options .RPCTimeout = defaultSocketCallTimeout
31
+ }
32
+
33
+ client := newClient ("go-stream-locator" , nil ,
34
+ options .TCPParameters , options .SaslConfiguration , options .RPCTimeout )
29
35
defer func (client * Client ) {
30
36
err := client .Close ()
31
37
if err != nil {
@@ -82,7 +88,8 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
82
88
}
83
89
func (env * Environment ) newReconnectClient () (* Client , error ) {
84
90
broker := env .options .ConnectionParameters [0 ]
85
- client := newClient ("go-stream-locator" , broker , env .options .TCPParameters , env .options .SaslConfiguration )
91
+ client := newClient ("go-stream-locator" , broker , env .options .TCPParameters ,
92
+ env .options .SaslConfiguration , env .options .RPCTimeout )
86
93
87
94
err := client .connect ()
88
95
tentatives := 1
@@ -96,7 +103,7 @@ func (env *Environment) newReconnectClient() (*Client, error) {
96
103
rand .Seed (time .Now ().UnixNano ())
97
104
n := rand .Intn (len (env .options .ConnectionParameters ))
98
105
client = newClient ("stream-locator" , env .options .ConnectionParameters [n ], env .options .TCPParameters ,
99
- env .options .SaslConfiguration )
106
+ env .options .SaslConfiguration , env . options . RPCTimeout )
100
107
tentatives = tentatives + 1
101
108
err = client .connect ()
102
109
@@ -139,7 +146,7 @@ func (env *Environment) NewProducer(streamName string, producerOptions *Producer
139
146
return nil , err
140
147
}
141
148
142
- return env .producers .newProducer (client , streamName , producerOptions , env .options .AddressResolver )
149
+ return env .producers .newProducer (client , streamName , producerOptions , env .options .AddressResolver , env . options . RPCTimeout )
143
150
}
144
151
145
152
func (env * Environment ) StreamExists (streamName string ) (bool , error ) {
@@ -229,7 +236,7 @@ func (env *Environment) NewConsumer(streamName string,
229
236
return nil , err
230
237
}
231
238
232
- return env .consumers .NewSubscriber (client , streamName , messagesHandler , options , env .options .AddressResolver )
239
+ return env .consumers .NewSubscriber (client , streamName , messagesHandler , options , env .options .AddressResolver , env . options . RPCTimeout )
233
240
}
234
241
235
242
func (env * Environment ) NewSuperStreamProducer (superStream string , superStreamProducerOptions * SuperStreamProducerOptions ) (* SuperStreamProducer , error ) {
@@ -258,6 +265,7 @@ type EnvironmentOptions struct {
258
265
MaxProducersPerClient int
259
266
MaxConsumersPerClient int
260
267
AddressResolver * AddressResolver
268
+ RPCTimeout time.Duration
261
269
}
262
270
263
271
func NewEnvironmentOptions () * EnvironmentOptions {
@@ -267,6 +275,7 @@ func NewEnvironmentOptions() *EnvironmentOptions {
267
275
ConnectionParameters : []* Broker {},
268
276
TCPParameters : newTCPParameterDefault (),
269
277
SaslConfiguration : newSaslConfigurationDefault (),
278
+ RPCTimeout : defaultSocketCallTimeout ,
270
279
}
271
280
}
272
281
@@ -429,6 +438,11 @@ func (envOptions *EnvironmentOptions) SetNoDelay(noDelay bool) *EnvironmentOptio
429
438
return envOptions
430
439
}
431
440
441
+ func (envOptions * EnvironmentOptions ) SetRPCTimeout (timeout time.Duration ) * EnvironmentOptions {
442
+ envOptions .RPCTimeout = timeout
443
+ return envOptions
444
+ }
445
+
432
446
type environmentCoordinator struct {
433
447
mutex * sync.Mutex
434
448
mutexContext * sync.RWMutex
@@ -510,7 +524,7 @@ func (c *Client) maybeCleanConsumers(streamName string) {
510
524
}
511
525
512
526
func (cc * environmentCoordinator ) newProducer (leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , streamName string ,
513
- options * ProducerOptions ) (* Producer , error ) {
527
+ options * ProducerOptions , rpcTimeout time. Duration ) (* Producer , error ) {
514
528
cc .mutex .Lock ()
515
529
defer cc .mutex .Unlock ()
516
530
cc .mutexContext .Lock ()
@@ -528,7 +542,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
528
542
}
529
543
530
544
if clientResult == nil {
531
- clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration )
545
+ clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
532
546
}
533
547
534
548
err := clientResult .connect ()
@@ -545,7 +559,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
545
559
if err != nil {
546
560
return nil , err
547
561
}
548
- clientResult = cc .newClientForProducer (options .ClientProvidedName , leader , tcpParameters , saslConfiguration )
562
+ clientResult = cc .newClientForProducer (options .ClientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
549
563
err = clientResult .connect ()
550
564
if err != nil {
551
565
return nil , err
@@ -562,8 +576,8 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
562
576
return producer , nil
563
577
}
564
578
565
- func (cc * environmentCoordinator ) newClientForProducer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ) * Client {
566
- clientResult := newClient (connectionName , leader , tcpParameters , saslConfiguration )
579
+ func (cc * environmentCoordinator ) newClientForProducer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , rpcTimeOut time. Duration ) * Client {
580
+ clientResult := newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeOut )
567
581
chMeta := make (chan metaDataUpdateEvent , 1 )
568
582
clientResult .metadataListener = chMeta
569
583
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -584,7 +598,7 @@ func (cc *environmentCoordinator) newClientForProducer(connectionName string, le
584
598
585
599
func (cc * environmentCoordinator ) newConsumer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ,
586
600
streamName string , messagesHandler MessagesHandler ,
587
- options * ConsumerOptions ) (* Consumer , error ) {
601
+ options * ConsumerOptions , rpcTimeout time. Duration ) (* Consumer , error ) {
588
602
cc .mutex .Lock ()
589
603
defer cc .mutex .Unlock ()
590
604
cc .mutexContext .Lock ()
@@ -598,7 +612,7 @@ func (cc *environmentCoordinator) newConsumer(connectionName string, leader *Bro
598
612
}
599
613
600
614
if clientResult == nil {
601
- clientResult = newClient (connectionName , leader , tcpParameters , saslConfiguration )
615
+ clientResult = newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeout )
602
616
chMeta := make (chan metaDataUpdateEvent )
603
617
clientResult .metadataListener = chMeta
604
618
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -663,7 +677,7 @@ func newProducers(maxItemsForClient int) *producersEnvironment {
663
677
}
664
678
665
679
func (ps * producersEnvironment ) newProducer (clientLocator * Client , streamName string ,
666
- options * ProducerOptions , resolver * AddressResolver ) (* Producer , error ) {
680
+ options * ProducerOptions , resolver * AddressResolver , rpcTimeOut time. Duration ) (* Producer , error ) {
667
681
ps .mutex .Lock ()
668
682
defer ps .mutex .Unlock ()
669
683
leader , err := clientLocator .BrokerLeader (streamName )
@@ -683,7 +697,7 @@ func (ps *producersEnvironment) newProducer(clientLocator *Client, streamName st
683
697
leader .cloneFrom (clientLocator .broker , resolver )
684
698
685
699
producer , err := ps .producersCoordinator [coordinatorKey ].newProducer (leader , clientLocator .tcpParameters ,
686
- clientLocator .saslConfiguration , streamName , options )
700
+ clientLocator .saslConfiguration , streamName , options , rpcTimeOut )
687
701
if err != nil {
688
702
return nil , err
689
703
}
@@ -728,7 +742,7 @@ func newConsumerEnvironment(maxItemsForClient int) *consumersEnvironment {
728
742
729
743
func (ps * consumersEnvironment ) NewSubscriber (clientLocator * Client , streamName string ,
730
744
messagesHandler MessagesHandler ,
731
- consumerOptions * ConsumerOptions , resolver * AddressResolver ) (* Consumer , error ) {
745
+ consumerOptions * ConsumerOptions , resolver * AddressResolver , rpcTimeout time. Duration ) (* Consumer , error ) {
732
746
ps .mutex .Lock ()
733
747
defer ps .mutex .Unlock ()
734
748
consumerBroker , err := clientLocator .BrokerForConsumer (streamName )
@@ -753,7 +767,7 @@ func (ps *consumersEnvironment) NewSubscriber(clientLocator *Client, streamName
753
767
consumer , err := ps .consumersCoordinator [coordinatorKey ].
754
768
newConsumer (clientProvidedName , consumerBroker , clientLocator .tcpParameters ,
755
769
clientLocator .saslConfiguration ,
756
- streamName , messagesHandler , consumerOptions )
770
+ streamName , messagesHandler , consumerOptions , rpcTimeout )
757
771
if err != nil {
758
772
return nil , err
759
773
}
0 commit comments