@@ -18,11 +18,10 @@ use axum::{
18
18
body:: Bytes ,
19
19
http:: { Uri , uri:: Scheme } ,
20
20
} ;
21
- use chrono:: Utc ;
22
21
use futures:: { Stream , TryStreamExt } ;
23
22
use reqwest:: { Client , Response , StatusCode } ;
24
23
use sarlacc:: Intern ;
25
- use tokio:: sync:: RwLock ;
24
+ use tokio:: { sync:: RwLock , time :: Instant } ;
26
25
use tracing:: { error, info, instrument} ;
27
26
28
27
use crate :: { discord:: Snowflake , webring:: CheckLevel } ;
@@ -32,8 +31,8 @@ use crate::{discord::Snowflake, webring::CheckLevel};
32
31
#[ allow( clippy:: unreadable_literal) ]
33
32
const WEBRING_CHANNEL : Snowflake = Snowflake :: new ( 1319140464812753009 ) ;
34
33
35
- /// The time in milliseconds for which the server is considered online after a successful ping.
36
- const ONLINE_CHECK_TTL_MS : i64 = 1000 ;
34
+ /// The time for which the server is considered online after a successful ping.
35
+ const ONLINE_CHECK_TTL : Duration = Duration :: from_secs ( 1 ) ;
37
36
38
37
/// The timeout to retry requesting a site after failure
39
38
const RETRY_TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
@@ -59,28 +58,32 @@ static CLIENT: LazyLock<Client> = LazyLock::new(|| {
59
58
} ) ;
60
59
61
60
/// (Last pinged, Was ping successful) — Used to check if the server is online
62
- static PING_INFO : RwLock < ( i64 , bool ) > = RwLock :: const_new ( ( i64 :: MIN , false ) ) ;
61
+ static PING_INFO : RwLock < ( Option < Instant > , bool ) > = RwLock :: const_new ( ( None , false ) ) ;
63
62
64
63
/// If a request succeeds, then call this function to mark the server as definitely online.
65
64
async fn mark_server_as_online ( ) {
66
- let at = Utc :: now ( ) . timestamp_millis ( ) ;
65
+ let at = Instant :: now ( ) ;
67
66
let mut ping_info = PING_INFO . write ( ) . await ;
68
- let now = Utc :: now ( ) . timestamp_millis ( ) ;
67
+ let now = Instant :: now ( ) ;
69
68
70
- if at + ONLINE_CHECK_TTL_MS < now || ping_info. 0 > at {
69
+ if at + ONLINE_CHECK_TTL < now || ping_info. 0 . is_some_and ( |ping| ping > at) {
71
70
return ;
72
71
}
73
72
74
- * ping_info = ( at , true ) ;
73
+ * ping_info = ( Some ( at ) , true ) ;
75
74
}
76
75
77
- /// Check if the server is online by either getting a cached value (cached for `ONLINE_CHECK_TTL_MS`), or by requesting our repository.
76
+ /// Check if the server is online by either getting a cached value (cached for
77
+ /// [`ONLINE_CHECK_TTL`]), or by making a `HEAD` request to the repository URL.
78
78
async fn is_online ( ) -> bool {
79
79
{
80
80
// Has it been checked within the TTL?
81
81
let ping_info = PING_INFO . read ( ) . await ;
82
- let now = Utc :: now ( ) . timestamp_millis ( ) ;
83
- if now < ping_info. 0 + ONLINE_CHECK_TTL_MS {
82
+ let now = Instant :: now ( ) ;
83
+ if ping_info
84
+ . 0
85
+ . is_some_and ( |ping| now < ping + ONLINE_CHECK_TTL )
86
+ {
84
87
return ping_info. 1 ;
85
88
}
86
89
}
@@ -89,22 +92,25 @@ async fn is_online() -> bool {
89
92
let mut ping_info = PING_INFO . write ( ) . await ;
90
93
91
94
// What if another thread did the ping while we were waiting for the write lock? If so, return it.
92
- let now = Utc :: now ( ) . timestamp_millis ( ) ;
93
- if now < ping_info. 0 + ONLINE_CHECK_TTL_MS {
95
+ let now = Instant :: now ( ) ;
96
+ if ping_info
97
+ . 0
98
+ . is_some_and ( |ping| now < ping + ONLINE_CHECK_TTL )
99
+ {
94
100
return ping_info. 1 ;
95
101
}
96
102
97
103
// Head-request our repository to make sure we're online.
98
- let result = CLIENT
104
+ let ping_successful = CLIENT
99
105
. head ( env ! ( "CARGO_PKG_REPOSITORY" ) )
100
106
. send ( )
101
107
. await
102
- . and_then ( Response :: error_for_status) ;
103
- let ping_successful = result . is_ok ( ) ;
108
+ . and_then ( Response :: error_for_status)
109
+ . is_ok ( ) ;
104
110
105
111
// Write the info
106
- let now = Utc :: now ( ) . timestamp_millis ( ) ;
107
- * ping_info = ( now, ping_successful) ;
112
+ let now = Instant :: now ( ) ;
113
+ * ping_info = ( Some ( now) , ping_successful) ;
108
114
109
115
ping_successful
110
116
}
0 commit comments