@@ -27,7 +27,7 @@ pub use self::request::{
27
27
StateReceiver ,
28
28
StateResponser ,
29
29
} ;
30
- pub use self :: types:: { EventType , SessionId , SessionState , WatchedEvent } ;
30
+ pub use self :: types:: { EventType , SessionId , SessionInfo , SessionState , WatchedEvent } ;
31
31
pub use self :: watch:: { OneshotReceiver , PersistentReceiver , WatchReceiver } ;
32
32
use self :: watch:: { WatchManager , WatcherId } ;
33
33
use crate :: deadline:: Deadline ;
@@ -71,11 +71,9 @@ pub struct Session {
71
71
ping_timeout : Duration ,
72
72
session_expired_timeout : Duration ,
73
73
74
- pub session_id : SessionId ,
74
+ pub session : SessionInfo ,
75
75
session_state : SessionState ,
76
76
pub session_timeout : Duration ,
77
- pub session_password : Vec < u8 > ,
78
- session_readonly : bool ,
79
77
80
78
pub authes : Vec < MarshalledRequest > ,
81
79
state_sender : tokio:: sync:: watch:: Sender < SessionState > ,
@@ -86,16 +84,15 @@ pub struct Session {
86
84
87
85
impl Session {
88
86
pub fn new (
89
- session : Option < ( SessionId , Vec < u8 > ) > ,
87
+ session : Option < SessionInfo > ,
90
88
authes : & [ AuthPacket ] ,
91
89
readonly : bool ,
92
90
detached : bool ,
93
91
tls_config : ClientConfig ,
94
92
session_timeout : Duration ,
95
93
connection_timeout : Duration ,
96
94
) -> ( Session , tokio:: sync:: watch:: Receiver < SessionState > ) {
97
- let ( session_id, session_password) =
98
- session. unwrap_or_else ( || ( SessionId ( 0 ) , Vec :: with_capacity ( PASSWORD_LEN ) ) ) ;
95
+ let session = session. unwrap_or_else ( || SessionInfo :: new ( SessionId ( 0 ) , Vec :: with_capacity ( PASSWORD_LEN ) ) ) ;
99
96
let ( state_sender, state_receiver) = tokio:: sync:: watch:: channel ( SessionState :: Disconnected ) ;
100
97
let now = Instant :: now ( ) ;
101
98
let ( watch_manager, unwatch_receiver) = WatchManager :: new ( ) ;
@@ -114,11 +111,9 @@ impl Session {
114
111
session_expired_timeout : Duration :: ZERO ,
115
112
connector : Connector :: new ( tls_config) ,
116
113
117
- session_id ,
114
+ session ,
118
115
session_timeout,
119
116
session_state : SessionState :: Disconnected ,
120
- session_password,
121
- session_readonly : session_id. 0 == 0 ,
122
117
123
118
authes : authes. iter ( ) . map ( |auth| MarshalledRequest :: new ( OpCode :: Auth , auth) ) . collect ( ) ,
124
119
state_sender,
@@ -132,7 +127,7 @@ impl Session {
132
127
133
128
fn is_readonly_allowed ( & self ) -> bool {
134
129
// Session downgrade is not allowed as partitioned session will expired finally by quorum.
135
- self . readonly && self . session_readonly
130
+ self . readonly && self . session . readonly
136
131
}
137
132
138
133
async fn close_requester < T : RequestOperation > ( mut requester : mpsc:: UnboundedReceiver < T > , err : & Error ) {
@@ -215,7 +210,7 @@ impl Session {
215
210
) {
216
211
if let Err ( err) = self . serve_session ( endpoints, & mut conn, buf, depot, requester, unwatch_requester) . await {
217
212
self . resolve_serve_error ( & err) ;
218
- log:: info!( "ZooKeeper session {} state {} error {}" , self . session_id , self . session_state, err) ;
213
+ log:: info!( "ZooKeeper session {} state {} error {}" , self . session . id , self . session_state, err) ;
219
214
depot. error ( & err) ;
220
215
} else {
221
216
self . change_state ( SessionState :: Disconnected ) ;
@@ -301,7 +296,7 @@ impl Session {
301
296
depot. pop_ping ( ) ?;
302
297
if let Some ( last_ping) = self . last_ping . take ( ) {
303
298
let elapsed = Instant :: now ( ) - last_ping;
304
- log:: debug!( "ZooKeeper session {} got ping response after {}ms" , self . session_id , elapsed. as_millis( ) ) ;
299
+ log:: debug!( "ZooKeeper session {} got ping response after {}ms" , self . session . id , elapsed. as_millis( ) ) ;
305
300
}
306
301
return Ok ( ( ) ) ;
307
302
}
@@ -349,7 +344,7 @@ impl Session {
349
344
}
350
345
351
346
fn complete_connect ( & mut self ) {
352
- let state = if self . session_readonly { SessionState :: ConnectedReadOnly } else { SessionState :: SyncConnected } ;
347
+ let state = if self . session . readonly { SessionState :: ConnectedReadOnly } else { SessionState :: SyncConnected } ;
353
348
self . change_state ( state) ;
354
349
}
355
350
@@ -361,11 +356,11 @@ impl Session {
361
356
} else if !self . is_readonly_allowed ( ) && response. readonly {
362
357
return Err ( Error :: ConnectionLoss ) ;
363
358
}
364
- self . session_id = SessionId ( response. session_id ) ;
359
+ self . session . id = SessionId ( response. session_id ) ;
360
+ self . session . password . clear ( ) ;
361
+ self . session . password . extend_from_slice ( response. password ) ;
362
+ self . session . readonly = response. readonly ;
365
363
self . reset_timeout ( Duration :: from_millis ( response. session_timeout as u64 ) ) ;
366
- self . session_password . clear ( ) ;
367
- self . session_password . extend_from_slice ( response. password ) ;
368
- self . session_readonly = response. readonly ;
369
364
self . complete_connect ( ) ;
370
365
Ok ( ( ) )
371
366
}
@@ -450,7 +445,7 @@ impl Session {
450
445
unwatch_requester : & mut mpsc:: UnboundedReceiver < ( WatcherId , StateResponser ) > ,
451
446
) -> Result < ( ) , Error > {
452
447
let mut seek_for_writable =
453
- if self . session_readonly { Some ( self . connector . clone ( ) . seek_for_writable ( endpoints) ) } else { None } ;
448
+ if self . session . readonly { Some ( self . connector . clone ( ) . seek_for_writable ( endpoints) ) } else { None } ;
454
449
let mut tick = time:: interval ( self . tick_timeout ) ;
455
450
tick. set_missed_tick_behavior ( time:: MissedTickBehavior :: Skip ) ;
456
451
let mut channel_closed = false ;
@@ -519,8 +514,8 @@ impl Session {
519
514
protocol_version : 0 ,
520
515
last_zxid_seen : 0 ,
521
516
timeout : self . session_timeout . as_millis ( ) as i32 ,
522
- session_id : if self . session_readonly { 0 } else { self . session_id . 0 } ,
523
- password : self . session_password . as_slice ( ) ,
517
+ session_id : if self . session . readonly { 0 } else { self . session . id . 0 } ,
518
+ password : self . session . password . as_slice ( ) ,
524
519
readonly : self . is_readonly_allowed ( ) ,
525
520
} ;
526
521
log:: trace!( "Sending connect request: {request:?}" ) ;
@@ -570,7 +565,7 @@ impl Session {
570
565
Err ( err)
571
566
} ,
572
567
_ => {
573
- log:: info!( "ZooKeeper succeeds to establish session({}) to {}" , self . session_id , endpoint) ;
568
+ log:: info!( "ZooKeeper succeeds to establish session({}) to {}" , self . session . id , endpoint) ;
574
569
Ok ( conn)
575
570
} ,
576
571
}
@@ -582,7 +577,7 @@ impl Session {
582
577
buf : & mut Vec < u8 > ,
583
578
depot : & mut Depot ,
584
579
) -> Result < Connection , Error > {
585
- let session_timeout = if self . session_id . 0 == 0 { self . session_timeout } else { self . session_expired_timeout } ;
580
+ let session_timeout = if self . session . id . 0 == 0 { self . session_timeout } else { self . session_expired_timeout } ;
586
581
let mut deadline = Deadline :: until ( self . last_recv + session_timeout) ;
587
582
let mut last_error = match self . start_once ( endpoints, & mut deadline, buf, depot) . await {
588
583
Err ( err) => err,
0 commit comments