Commit 08a4cd4 1 parent 1fd03c0 commit 08a4cd4 Copy full SHA for 08a4cd4
File tree 1 file changed +9
-12
lines changed
1 file changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ pub struct Session {
33
33
impl Session {
34
34
async fn json_api ( & mut self , global : & Global ) -> Result < & mut ClientConnection , SessionError > {
35
35
if self . json_api . is_none ( ) {
36
+ // Generate a session ID
37
+ if self . id . is_nil ( ) {
38
+ self . id = uuid:: Uuid :: new_v4 ( ) ;
39
+ }
40
+
36
41
// Can't use SocketAddr, see https://github.com/seanmonstar/warp/issues/830
37
42
self . json_api = Some ( ClientConnection :: new (
38
43
global
@@ -157,18 +162,10 @@ impl<T: Reply> WithSession<T> {
157
162
pub async fn new ( reply : T , instance : SessionInstance ) -> Self {
158
163
let id = instance. session . read ( ) . await . id ;
159
164
160
- let set_cookie = if id. is_nil ( ) {
161
- let mut session = instance. session . write ( ) . await ;
162
-
163
- if session. id == id {
164
- // Still the same ID after locking for write, so we can generate a new one
165
- let id = uuid:: Uuid :: new_v4 ( ) ;
166
- session. id = id;
167
- instance
168
- . sessions
169
- . write ( )
170
- . await
171
- . insert ( id, instance. session . clone ( ) ) ;
165
+ let set_cookie = if !instance. sessions . read ( ) . await . contains_key ( & id) {
166
+ let mut sessions = instance. sessions . write ( ) . await ;
167
+
168
+ if sessions. insert ( id, instance. session . clone ( ) ) . is_none ( ) {
172
169
Some ( id. to_string ( ) )
173
170
} else {
174
171
// Not the same ID, another request set the cookie first
You can’t perform that action at this time.
0 commit comments