@@ -41,7 +41,6 @@ use sha2::Digest;
41
41
use sha2:: Sha256 ;
42
42
use tokio:: time:: Instant ;
43
43
44
- use crate :: servers:: http:: v1:: session:: consts:: REFRESH_TOKEN_TTL ;
45
44
use crate :: servers:: http:: v1:: session:: consts:: TOMBSTONE_TTL ;
46
45
use crate :: servers:: http:: v1:: session:: consts:: TTL_GRACE_PERIOD_META ;
47
46
use crate :: servers:: http:: v1:: session:: consts:: TTL_GRACE_PERIOD_QUERY ;
@@ -89,7 +88,7 @@ pub struct ClientSessionManager {
89
88
/// refresh:
90
89
/// - auth (with min interval)
91
90
session_state : Mutex < BTreeMap < String , SessionState > > ,
92
- pub session_token_ttl : Duration ,
91
+ pub max_idle_time : Duration ,
93
92
pub min_refresh_interval : Duration ,
94
93
}
95
94
@@ -102,12 +101,20 @@ impl ClientSessionManager {
102
101
format ! ( "{user_name}/{client_session_id}" )
103
102
}
104
103
104
+ fn refresh_token_ttl ( & self ) -> Duration {
105
+ self . max_idle_time
106
+ }
107
+
108
+ fn session_token_ttl ( & self ) -> Duration {
109
+ self . max_idle_time / 4
110
+ }
111
+
105
112
#[ async_backtrace:: framed]
106
113
pub async fn init ( cfg : & InnerConfig ) -> Result < ( ) > {
107
114
let mgr = Arc :: new ( Self {
108
115
session_tokens : RwLock :: new ( LruCache :: with_items_capacity ( 1024 ) ) ,
109
116
refresh_tokens : RwLock :: new ( LruCache :: with_items_capacity ( 1024 ) ) ,
110
- session_token_ttl : Duration :: from_secs ( cfg. query . http_session_timeout_secs ) ,
117
+ max_idle_time : Duration :: from_secs ( cfg. query . http_session_timeout_secs ) ,
111
118
min_refresh_interval : Duration :: from_secs (
112
119
( cfg. query . http_session_timeout_secs / 10 ) . min ( 300 ) ,
113
120
) ,
@@ -127,7 +134,9 @@ impl ClientSessionManager {
127
134
{
128
135
let guard = self . session_state . lock ( ) ;
129
136
for ( key, session_state) in & * guard {
130
- if ( now - session_state. last_access ) > self . session_token_ttl {
137
+ if ( now - session_state. last_access )
138
+ > self . max_idle_time + self . min_refresh_interval
139
+ {
131
140
expired. push ( ( key. clone ( ) , session_state. temp_tbl_mgr . clone ( ) ) ) ;
132
141
} else {
133
142
remained. push ( key. clone ( ) ) ;
@@ -140,18 +149,23 @@ impl ClientSessionManager {
140
149
guard. remove ( id) ;
141
150
}
142
151
}
152
+ let num_expired = expired. len ( ) ;
143
153
for ( key, mgr) in expired {
144
154
drop_all_temp_tables_with_logging ( & key, mgr, "idle" ) . await ;
145
155
}
156
+ let elapsed = now. duration_since ( Instant :: now ( ) ) ;
146
157
147
- if !( remained. is_empty ( ) ) {
158
+ if !( remained. is_empty ( ) && num_expired == 0 ) {
148
159
info ! (
149
- "[TEMP TABLE] sessions after cleanup, {} remained: {:?}" ,
160
+ "[TEMP TABLE] cleanup {num_expired} sessions in {} secs, {} remained: {:?}" ,
161
+ elapsed. as_secs( ) ,
150
162
remained. len( ) ,
151
163
remained
152
164
) ;
153
165
}
154
- tokio:: time:: sleep ( self . session_token_ttl / 4 ) . await ;
166
+ if elapsed < self . max_idle_time / 4 {
167
+ tokio:: time:: sleep ( self . max_idle_time / 4 - elapsed) . await ;
168
+ }
155
169
}
156
170
}
157
171
@@ -166,7 +180,7 @@ impl ClientSessionManager {
166
180
. upsert_client_session_id (
167
181
client_session_id,
168
182
& user_name,
169
- REFRESH_TOKEN_TTL + TTL_GRACE_PERIOD_META + self . min_refresh_interval ,
183
+ self . max_idle_time + self . min_refresh_interval + TTL_GRACE_PERIOD_META ,
170
184
)
171
185
. await ?;
172
186
Ok ( ( ) )
@@ -196,7 +210,7 @@ impl ClientSessionManager {
196
210
& tenant_name,
197
211
& user,
198
212
& auth_role,
199
- REFRESH_TOKEN_TTL + TTL_GRACE_PERIOD_META ,
213
+ self . refresh_token_ttl ( ) + TTL_GRACE_PERIOD_META ,
200
214
) ;
201
215
let refresh_token = claim. encode ( TokenType :: Refresh ) ;
202
216
let refresh_token_hash = hash_token ( refresh_token. as_bytes ( ) ) ;
@@ -208,7 +222,7 @@ impl ClientSessionManager {
208
222
. upsert_token (
209
223
& refresh_token_hash,
210
224
token_info. clone ( ) ,
211
- REFRESH_TOKEN_TTL + TTL_GRACE_PERIOD_META ,
225
+ self . refresh_token_ttl ( ) + TTL_GRACE_PERIOD_META ,
212
226
false ,
213
227
)
214
228
. await ?;
@@ -222,7 +236,7 @@ impl ClientSessionManager {
222
236
. insert ( refresh_token_hash. clone ( ) , None ) ;
223
237
224
238
// session token
225
- claim. expire_at_in_secs = ( now + self . session_token_ttl )
239
+ claim. expire_at_in_secs = ( now + self . session_token_ttl ( ) )
226
240
. duration_since ( SystemTime :: UNIX_EPOCH )
227
241
. unwrap ( )
228
242
. as_secs ( ) ;
@@ -238,7 +252,7 @@ impl ClientSessionManager {
238
252
. upsert_token (
239
253
& session_token_hash,
240
254
token_info. clone ( ) ,
241
- REFRESH_TOKEN_TTL + TTL_GRACE_PERIOD_META ,
255
+ self . session_token_ttl ( ) + TTL_GRACE_PERIOD_META ,
242
256
false ,
243
257
)
244
258
. await ?;
@@ -256,7 +270,7 @@ impl ClientSessionManager {
256
270
. upsert_client_session_id (
257
271
& client_session_id,
258
272
& claim. user ,
259
- REFRESH_TOKEN_TTL + TTL_GRACE_PERIOD_META ,
273
+ self . max_idle_time + self . min_refresh_interval + TTL_GRACE_PERIOD_META ,
260
274
)
261
275
. await ?;
262
276
0 commit comments