@@ -22,8 +22,10 @@ use crate::{
22
22
validation:: ExactlyOne ,
23
23
} ;
24
24
use async_trait:: async_trait;
25
- use std:: ops:: { ControlFlow , Deref , Range } ;
26
- use std:: sync:: Arc ;
25
+ use std:: {
26
+ ops:: { ControlFlow , Deref , Range } ,
27
+ sync:: Arc ,
28
+ } ;
27
29
use time:: OffsetDateTime ;
28
30
use tokio:: sync:: Mutex ;
29
31
use tracing:: { error, info} ;
@@ -95,14 +97,35 @@ impl std::fmt::Debug for PartitionClient {
95
97
}
96
98
97
99
impl PartitionClient {
98
- pub ( super ) fn new ( topic : String , partition : i32 , brokers : Arc < BrokerConnector > ) -> Self {
99
- Self {
100
+ pub ( super ) async fn new (
101
+ topic : String ,
102
+ partition : i32 ,
103
+ brokers : Arc < BrokerConnector > ,
104
+ ) -> Result < Self > {
105
+ let p = Self {
100
106
topic,
101
107
partition,
102
- brokers,
108
+ brokers : Arc :: clone ( & brokers ) ,
103
109
backoff_config : Default :: default ( ) ,
104
110
current_broker : Mutex :: new ( None ) ,
105
- }
111
+ } ;
112
+
113
+ // Force discover and establish a cached connection to the leader
114
+ let scope = & p;
115
+ maybe_retry (
116
+ & Default :: default ( ) ,
117
+ & * brokers,
118
+ "leader_detection" ,
119
+ || async move {
120
+ scope
121
+ . get_leader ( MetadataLookupMode :: CachedArbitrary )
122
+ . await ?;
123
+ Ok ( ( ) )
124
+ } ,
125
+ )
126
+ . await ?;
127
+
128
+ Ok ( p)
106
129
}
107
130
108
131
/// Topic
@@ -115,17 +138,6 @@ impl PartitionClient {
115
138
self . partition
116
139
}
117
140
118
- /// Re-run the leader discovery process for this client, and establish a
119
- /// connection to the leader.
120
- pub ( crate ) async fn refresh_leader ( & self ) -> Result < ( ) > {
121
- // Remove the current broker connection, if any.
122
- * self . current_broker . lock ( ) . await = None ;
123
- // And acquire a new one, forcing discovery and establishing a cached
124
- // connection to the leader.
125
- self . get ( ) . await ?;
126
- Ok ( ( ) )
127
- }
128
-
129
141
/// Produce a batch of records to the partition
130
142
pub async fn produce (
131
143
& self ,
0 commit comments