micro_web/date/mod.rs
1//! HTTP date header value management service.
2//!
3//! This module provides a service for efficiently managing and updating HTTP date header values
4//! in a concurrent environment. It updates the date string periodically to avoid repeated
5//! date string formatting operations in high-concurrency scenarios.
6
7use arc_swap::ArcSwap;
8use bytes::Bytes;
9use http::HeaderValue;
10use once_cell::sync::Lazy;
11use std::sync::Arc;
12use std::time::Duration;
13
14mod date_service_decorator;
15
16pub use date_service_decorator::DateServiceDecorator;
17
18/// A service that maintains and periodically updates the current HTTP date string.
19///
20/// This service runs a background task that updates the date string every 700ms,
21/// providing an efficient way to access formatted HTTP date strings without
22/// formatting them on every request.
23pub struct DateService {
24 current: Arc<ArcSwap<Bytes>>,
25 handle: tokio::task::JoinHandle<()>,
26}
27
28static DATE_SERVICE: Lazy<DateService> = Lazy::new(|| DateService::new_with_update_interval(Duration::from_millis(800)));
29
30impl DateService {
31 /// Returns a reference to the global singleton instance of `DateService`.
32 ///
33 /// This method provides access to a shared `DateService` instance that can be used
34 /// across the application to efficiently handle HTTP date headers.
35 ///
36 /// # Returns
37 /// A static reference to the global `DateService` instance.
38 pub fn get_global_instance() -> &'static DateService {
39 &DATE_SERVICE
40 }
41
42 /// Creates a new `DateService` instance.
43 ///
44 /// This method initializes the service with the current system time and starts
45 /// a background task that updates the date string every 700ms.
46 ///
47 /// # Returns
48 /// Returns a new `DateService` instance with the background update task running.
49 fn new_with_update_interval(update_interval: Duration) -> Self {
50 let mut buf = faf_http_date::get_date_buff_no_key();
51 faf_http_date::get_date_no_key(&mut buf);
52 let bytes = Bytes::from_owner(buf);
53
54 let current = Arc::new(ArcSwap::from_pointee(bytes));
55 let current_arc = Arc::clone(¤t);
56
57 let handle = tokio::spawn(async move {
58 loop {
59 tokio::time::sleep(update_interval).await;
60 let mut buf = faf_http_date::get_date_buff_no_key();
61 faf_http_date::get_date_no_key(&mut buf);
62 let bytes = Bytes::from_owner(buf);
63 current_arc.store(Arc::new(bytes));
64 }
65 });
66
67 DateService { current, handle }
68 }
69
70 /// Provides access to the current HTTP date string through a callback function.
71 ///
72 /// This method allows safe access to the current date string without exposing
73 /// the internal synchronization mechanisms.
74 pub(crate) fn with_http_date<F>(&self, mut f: F)
75 where
76 F: FnMut(HeaderValue),
77 {
78 let date = self.current.load().as_ref().clone();
79 // SAFE: date is created by faf_http_date, it's valid
80 let header_value = unsafe { HeaderValue::from_maybe_shared_unchecked(date) };
81 f(header_value)
82 }
83}
84
85/// Implements the `Drop` trait to ensure the background task is properly cleaned up
86/// when the `DateService` is dropped.
87impl Drop for DateService {
88 fn drop(&mut self) {
89 self.handle.abort();
90 }
91}