@@ -29,9 +29,7 @@ pub struct FriendsSocket {
29
29
#[ derive( Deserialize , Serialize ) ]
30
30
pub struct UserFriend {
31
31
pub id : String ,
32
- // TODO: Remove this optional and serde alias on release
33
- pub friend_id : Option < String > ,
34
- #[ serde( alias = "pending" ) ]
32
+ pub friend_id : String ,
35
33
pub accepted : bool ,
36
34
pub created : DateTime < Utc > ,
37
35
}
@@ -72,6 +70,7 @@ impl FriendsSocket {
72
70
}
73
71
}
74
72
73
+ #[ tracing:: instrument( skip_all) ]
75
74
pub async fn connect (
76
75
& self ,
77
76
exec : impl sqlx:: Executor < ' _ , Database = sqlx:: Sqlite > + Copy ,
@@ -144,8 +143,20 @@ impl FriendsSocket {
144
143
)
145
144
. ok ( )
146
145
}
147
- Message :: Ping ( _)
148
- | Message :: Pong ( _)
146
+ Message :: Ping ( bytes) => {
147
+ if let Some ( write) = write_handle
148
+ . write ( )
149
+ . await
150
+ . as_mut ( )
151
+ {
152
+ let _ = write
153
+ . send ( Message :: Pong ( bytes) )
154
+ . await ;
155
+ }
156
+
157
+ continue ;
158
+ }
159
+ Message :: Pong ( _)
149
160
| Message :: Frame ( _) => continue ,
150
161
Message :: Close ( _) => break ,
151
162
} ;
@@ -175,8 +186,7 @@ impl FriendsSocket {
175
186
}
176
187
}
177
188
Err ( e) => {
178
- println ! ( "WebSocket error: {:?}" , e) ;
179
- break ;
189
+ tracing:: error!( "Error handling message from websocket server: {:?}" , e) ;
180
190
}
181
191
}
182
192
}
@@ -198,11 +208,13 @@ impl FriendsSocket {
198
208
Ok ( ( ) )
199
209
}
200
210
201
- pub async fn reconnect_task ( ) -> crate :: Result < ( ) > {
211
+ #[ tracing:: instrument( skip_all) ]
212
+ pub async fn socket_loop ( ) -> crate :: Result < ( ) > {
202
213
let state = crate :: State :: get ( ) . await ?;
203
214
204
215
tokio:: task:: spawn ( async move {
205
216
let mut last_connection = Utc :: now ( ) ;
217
+ let mut last_ping = Utc :: now ( ) ;
206
218
207
219
loop {
208
220
let connected = {
@@ -215,6 +227,7 @@ impl FriendsSocket {
215
227
> chrono:: Duration :: seconds ( 30 )
216
228
{
217
229
last_connection = Utc :: now ( ) ;
230
+ last_ping = Utc :: now ( ) ;
218
231
let _ = state
219
232
. friends_socket
220
233
. connect (
@@ -223,6 +236,15 @@ impl FriendsSocket {
223
236
& state. process_manager ,
224
237
)
225
238
. await ;
239
+ } else if connected
240
+ && Utc :: now ( ) . signed_duration_since ( last_ping)
241
+ > chrono:: Duration :: seconds ( 10 )
242
+ {
243
+ last_ping = Utc :: now ( ) ;
244
+ let mut write = state. friends_socket . write . write ( ) . await ;
245
+ if let Some ( write) = write. as_mut ( ) {
246
+ let _ = write. send ( Message :: Ping ( Vec :: new ( ) ) ) . await ;
247
+ }
226
248
}
227
249
228
250
tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
@@ -232,6 +254,7 @@ impl FriendsSocket {
232
254
Ok ( ( ) )
233
255
}
234
256
257
+ #[ tracing:: instrument( skip( self ) ) ]
235
258
pub async fn disconnect ( & self ) -> crate :: Result < ( ) > {
236
259
let mut write_lock = self . write . write ( ) . await ;
237
260
if let Some ( ref mut write_half) = * write_lock {
@@ -241,6 +264,7 @@ impl FriendsSocket {
241
264
Ok ( ( ) )
242
265
}
243
266
267
+ #[ tracing:: instrument( skip( self ) ) ]
244
268
pub async fn update_status (
245
269
& self ,
246
270
profile_name : Option < String > ,
@@ -257,6 +281,7 @@ impl FriendsSocket {
257
281
Ok ( ( ) )
258
282
}
259
283
284
+ #[ tracing:: instrument( skip_all) ]
260
285
pub async fn friends (
261
286
exec : impl sqlx:: Executor < ' _ , Database = sqlx:: Sqlite > + Copy ,
262
287
semaphore : & FetchSemaphore ,
@@ -272,13 +297,15 @@ impl FriendsSocket {
272
297
. await
273
298
}
274
299
300
+ #[ tracing:: instrument( skip( self ) ) ]
275
301
pub fn friend_statuses ( & self ) -> Vec < UserStatus > {
276
302
self . user_statuses
277
303
. iter ( )
278
304
. map ( |x| x. value ( ) . clone ( ) )
279
305
. collect ( )
280
306
}
281
307
308
+ #[ tracing:: instrument( skip( exec, semaphore) ) ]
282
309
pub async fn add_friend (
283
310
user_id : & str ,
284
311
exec : impl sqlx:: Executor < ' _ , Database = sqlx:: Sqlite > + Copy ,
@@ -299,6 +326,7 @@ impl FriendsSocket {
299
326
Ok ( ( ) )
300
327
}
301
328
329
+ #[ tracing:: instrument( skip( exec, semaphore) ) ]
302
330
pub async fn remove_friend (
303
331
user_id : & str ,
304
332
exec : impl sqlx:: Executor < ' _ , Database = sqlx:: Sqlite > + Copy ,
0 commit comments