@@ -554,45 +554,30 @@ func (cc *environmentCoordinator) newProducer(leader *Broker, tcpParameters *TCP
554554 }
555555
556556 if clientResult == nil {
557- clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
557+ clientResult = cc .newClientForConnection (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
558558 }
559559
560+ // try to reconnect in case the socket is closed
560561 err := clientResult .connect ()
561562 if err != nil {
562563 return nil , err
563564 }
564565
565- for clientResult .connectionProperties .host != leader .advHost ||
566- clientResult .connectionProperties .port != leader .advPort {
567- logs .LogDebug ("connectionProperties host %s doesn't match with the advertised_host %s, advertised_port %s .. retry" ,
568- clientResult .connectionProperties .host ,
569- leader .advHost , leader .advPort )
570- clientResult .Close ()
571- clientResult = cc .newClientForProducer (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
572- err = clientResult .connect ()
573- if err != nil {
574- return nil , err
575- }
576- time .Sleep (1 * time .Second )
566+ clientResult , err = cc .validateBrokerConnection (clientResult , leader ,
567+ func () * Client {
568+ return cc .newClientForConnection (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
569+ })
570+ if err != nil {
571+ return nil , err
577572 }
578573
579574 producer , err := clientResult .declarePublisher (streamName , options , cleanUp )
580-
581575 if err != nil {
582576 return nil , err
583577 }
584-
585578 return producer , nil
586579}
587580
588- func (cc * environmentCoordinator ) newClientForProducer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , rpcTimeOut time.Duration ) * Client {
589- clientResult := newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeOut )
590- cc .nextId ++
591-
592- cc .clientsPerContext .Store (cc .nextId , clientResult )
593- return clientResult
594- }
595-
596581func (cc * environmentCoordinator ) newConsumer (connectionName string , leader * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration ,
597582 streamName string , messagesHandler MessagesHandler ,
598583 options * ConsumerOptions , rpcTimeout time.Duration , cleanUp func ()) (* Consumer , error ) {
@@ -608,24 +593,60 @@ func (cc *environmentCoordinator) newConsumer(connectionName string, leader *Bro
608593 return true
609594 })
610595
596+ clientProvidedName := "go-stream-consumer"
597+ if options != nil && options .ClientProvidedName != "" {
598+ clientProvidedName = options .ClientProvidedName
599+ }
600+
611601 if clientResult == nil {
612- clientResult = newClient (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeout )
613- cc .nextId ++
614- cc .clientsPerContext .Store (cc .nextId , clientResult )
602+ clientResult = cc .newClientForConnection (clientProvidedName , leader , tcpParameters , saslConfiguration , rpcTimeout )
615603 }
604+
616605 // try to reconnect in case the socket is closed
617606 err := clientResult .connect ()
618607 if err != nil {
619608 return nil , err
620609 }
621610
611+ clientResult , err = cc .validateBrokerConnection (clientResult , leader ,
612+ func () * Client {
613+ return cc .newClientForConnection (connectionName , leader , tcpParameters , saslConfiguration , rpcTimeout )
614+ })
615+ if err != nil {
616+ return nil , err
617+ }
618+
622619 subscriber , err := clientResult .declareSubscriber (streamName , messagesHandler , options , cleanUp )
623620 if err != nil {
624621 return nil , err
625622 }
626623 return subscriber , nil
627624}
628625
626+ func (cc * environmentCoordinator ) validateBrokerConnection (client * Client , broker * Broker , newClientFunc func () * Client ) (* Client , error ) {
627+ for client .connectionProperties .host != broker .advHost ||
628+ client .connectionProperties .port != broker .advPort {
629+ logs .LogDebug ("connectionProperties host %s doesn't match with the advertised_host %s, advertised_port %s .. retry" ,
630+ client .connectionProperties .host ,
631+ broker .advHost , broker .advPort )
632+ client .Close ()
633+ client = newClientFunc ()
634+ err := client .connect ()
635+ if err != nil {
636+ return nil , err
637+ }
638+ time .Sleep (time .Duration (500 + rand .Intn (1000 )) * time .Millisecond )
639+ }
640+ return client , nil
641+ }
642+
643+ func (cc * environmentCoordinator ) newClientForConnection (connectionName string , broker * Broker , tcpParameters * TCPParameters , saslConfiguration * SaslConfiguration , rpcTimeout time.Duration ) * Client {
644+ clientResult := newClient (connectionName , broker , tcpParameters , saslConfiguration , rpcTimeout )
645+ cc .nextId ++
646+ cc .clientsPerContext .Store (cc .nextId , clientResult )
647+ return clientResult
648+ }
649+
629650func (cc * environmentCoordinator ) Close () error {
630651 cc .clientsPerContext .Range (func (_ , value any ) bool {
631652 value .(* Client ).coordinator .Close ()
@@ -664,7 +685,7 @@ func (ps *producersEnvironment) newProducer(clientLocator *Client, streamName st
664685 options * ProducerOptions , resolver * AddressResolver , rpcTimeOut time.Duration ) (* Producer , error ) {
665686 ps .mutex .Lock ()
666687 defer ps .mutex .Unlock ()
667- leader , err := clientLocator .BrokerLeader (streamName )
688+ leader , err := clientLocator .BrokerLeaderWithResolver (streamName , resolver )
668689 if err != nil {
669690 return nil , err
670691 }
0 commit comments