@@ -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 {
@@ -96,7 +102,8 @@ func NewEnvironment(options *EnvironmentOptions) (*Environment, error) {
96
102
}
97
103
func (env * Environment ) newReconnectClient () (* Client , error ) {
98
104
broker := env .options .ConnectionParameters [0 ]
99
- client := newClient ("go-stream-locator" , broker , env .options .TCPParameters , env .options .SaslConfiguration )
105
+ client := newClient ("go-stream-locator" , broker , env .options .TCPParameters ,
106
+ env .options .SaslConfiguration , env .options .RPCTimeout )
100
107
101
108
err := client .connect ()
102
109
tentatives := 1
@@ -110,7 +117,7 @@ func (env *Environment) newReconnectClient() (*Client, error) {
110
117
rand .Seed (time .Now ().UnixNano ())
111
118
n := rand .Intn (len (env .options .ConnectionParameters ))
112
119
client = newClient ("stream-locator" , env .options .ConnectionParameters [n ], env .options .TCPParameters ,
113
- env .options .SaslConfiguration )
120
+ env .options .SaslConfiguration , env . options . RPCTimeout )
114
121
tentatives = tentatives + 1
115
122
err = client .connect ()
116
123
@@ -153,7 +160,7 @@ func (env *Environment) NewProducer(streamName string, producerOptions *Producer
153
160
return nil , err
154
161
}
155
162
156
- return env .producers .newProducer (client , streamName , producerOptions , env .options .AddressResolver )
163
+ return env .producers .newProducer (client , streamName , producerOptions , env .options .AddressResolver , env . options . RPCTimeout )
157
164
}
158
165
159
166
func (env * Environment ) StreamExists (streamName string ) (bool , error ) {
@@ -243,7 +250,7 @@ func (env *Environment) NewConsumer(streamName string,
243
250
return nil , err
244
251
}
245
252
246
- return env .consumers .NewSubscriber (client , streamName , messagesHandler , options , env .options .AddressResolver )
253
+ return env .consumers .NewSubscriber (client , streamName , messagesHandler , options , env .options .AddressResolver , env . options . RPCTimeout )
247
254
}
248
255
249
256
func (env * Environment ) NewSuperStreamProducer (superStream string , superStreamProducerOptions * SuperStreamProducerOptions ) (* SuperStreamProducer , error ) {
@@ -272,6 +279,7 @@ type EnvironmentOptions struct {
272
279
MaxProducersPerClient int
273
280
MaxConsumersPerClient int
274
281
AddressResolver * AddressResolver
282
+ RPCTimeout time.Duration
275
283
}
276
284
277
285
func NewEnvironmentOptions () * EnvironmentOptions {
@@ -281,6 +289,7 @@ func NewEnvironmentOptions() *EnvironmentOptions {
281
289
ConnectionParameters : []* Broker {},
282
290
TCPParameters : newTCPParameterDefault (),
283
291
SaslConfiguration : newSaslConfigurationDefault (),
292
+ RPCTimeout : defaultSocketCallTimeout ,
284
293
}
285
294
}
286
295
@@ -443,6 +452,11 @@ func (envOptions *EnvironmentOptions) SetNoDelay(noDelay bool) *EnvironmentOptio
443
452
return envOptions
444
453
}
445
454
455
+ func (envOptions * EnvironmentOptions ) SetRPCTimeout (timeout time.Duration ) * EnvironmentOptions {
456
+ envOptions .RPCTimeout = timeout
457
+ return envOptions
458
+ }
459
+
446
460
type environmentCoordinator struct {
447
461
mutex * sync.Mutex
448
462
mutexContext * sync.RWMutex
@@ -524,7 +538,7 @@ func (c *Client) maybeCleanConsumers(streamName string) {
524
538
}
525
539
526
540
func (cc * environmentCoordinator ) newProducer (leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , streamName string ,
527
- options * ProducerOptions ) (* Producer , error ) {
541
+ options * ProducerOptions , rpcTimeout time. Duration ) (* Producer , error ) {
528
542
cc .mutex .Lock ()
529
543
defer cc .mutex .Unlock ()
530
544
cc .mutexContext .Lock ()
@@ -542,7 +556,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
542
556
}
543
557
544
558
if clientResult == nil {
545
- clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration )
559
+ clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
546
560
}
547
561
548
562
err := clientResult .connect ()
@@ -559,7 +573,7 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
559
573
if err != nil {
560
574
return nil , err
561
575
}
562
- clientResult = cc .newClientForProducer (options .ClientProvidedName , leader , tcpParameters , saslConfiguration )
576
+ clientResult = cc .newClientForProducer (options .ClientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
563
577
err = clientResult .connect ()
564
578
if err != nil {
565
579
return nil , err
@@ -576,8 +590,8 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
576
590
return producer , nil
577
591
}
578
592
579
- func (cc * environmentCoordinator ) newClientForProducer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ) * Client {
580
- clientResult := newClient (connectionName , leader , tcpParameters , saslConfiguration )
593
+ func (cc * environmentCoordinator ) newClientForProducer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , rpcTimeOut time. Duration ) * Client {
594
+ clientResult := newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeOut )
581
595
chMeta := make (chan metaDataUpdateEvent , 1 )
582
596
clientResult .metadataListener = chMeta
583
597
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -598,7 +612,7 @@ func (cc *environmentCoordinator) newClientForProducer(connectionName string, le
598
612
599
613
func (cc * environmentCoordinator ) newConsumer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ,
600
614
streamName string , messagesHandler MessagesHandler ,
601
- options * ConsumerOptions ) (* Consumer , error ) {
615
+ options * ConsumerOptions , rpcTimeout time. Duration ) (* Consumer , error ) {
602
616
cc .mutex .Lock ()
603
617
defer cc .mutex .Unlock ()
604
618
cc .mutexContext .Lock ()
@@ -612,7 +626,7 @@ func (cc *environmentCoordinator) newConsumer(connectionName string, leader *Bro
612
626
}
613
627
614
628
if clientResult == nil {
615
- clientResult = newClient (connectionName , leader , tcpParameters , saslConfiguration )
629
+ clientResult = newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeout )
616
630
chMeta := make (chan metaDataUpdateEvent )
617
631
clientResult .metadataListener = chMeta
618
632
go func (ch <- chan metaDataUpdateEvent , cl * Client ) {
@@ -677,7 +691,7 @@ func newProducers(maxItemsForClient int) *producersEnvironment {
677
691
}
678
692
679
693
func (ps * producersEnvironment ) newProducer (clientLocator * Client , streamName string ,
680
- options * ProducerOptions , resolver * AddressResolver ) (* Producer , error ) {
694
+ options * ProducerOptions , resolver * AddressResolver , rpcTimeOut time. Duration ) (* Producer , error ) {
681
695
ps .mutex .Lock ()
682
696
defer ps .mutex .Unlock ()
683
697
leader , err := clientLocator .BrokerLeader (streamName )
@@ -697,7 +711,7 @@ func (ps *producersEnvironment) newProducer(clientLocator *Client, streamName st
697
711
leader .cloneFrom (clientLocator .broker , resolver )
698
712
699
713
producer , err := ps .producersCoordinator [coordinatorKey ].newProducer (leader , clientLocator .tcpParameters ,
700
- clientLocator .saslConfiguration , streamName , options )
714
+ clientLocator .saslConfiguration , streamName , options , rpcTimeOut )
701
715
if err != nil {
702
716
return nil , err
703
717
}
@@ -742,7 +756,7 @@ func newConsumerEnvironment(maxItemsForClient int) *consumersEnvironment {
742
756
743
757
func (ps * consumersEnvironment ) NewSubscriber (clientLocator * Client , streamName string ,
744
758
messagesHandler MessagesHandler ,
745
- consumerOptions * ConsumerOptions , resolver * AddressResolver ) (* Consumer , error ) {
759
+ consumerOptions * ConsumerOptions , resolver * AddressResolver , rpcTimeout time. Duration ) (* Consumer , error ) {
746
760
ps .mutex .Lock ()
747
761
defer ps .mutex .Unlock ()
748
762
consumerBroker , err := clientLocator .BrokerForConsumer (streamName )
@@ -767,7 +781,7 @@ func (ps *consumersEnvironment) NewSubscriber(clientLocator *Client, streamName
767
781
consumer , err := ps .consumersCoordinator [coordinatorKey ].
768
782
newConsumer (clientProvidedName , consumerBroker , clientLocator .tcpParameters ,
769
783
clientLocator .saslConfiguration ,
770
- streamName , messagesHandler , consumerOptions )
784
+ streamName , messagesHandler , consumerOptions , rpcTimeout )
771
785
if err != nil {
772
786
return nil , err
773
787
}
0 commit comments