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