1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use ;
use LazyLock;
use crateError;
use crateResult;
/// Manages the existence of a single session within a system.
///
/// `SessionManager` is an internal utility that enforces a single active session
/// at any given time. It is primarily used internally by higher-level abstractions like [`Session`]
/// to coordinate access to WebSocket sessions or other shared resources.
///
/// ### Key Features
/// - **Thread-Safe**: Uses atomic operations to safely manage session state in multi-threaded environments.
/// - **Lifecycle Management**: Ensures proper acquisition and release of session state.
/// - **Internal Use Only**: This type is not exposed outside of the crate. Use [`Session`] or
/// other public abstractions for interacting with session-related functionality.
///
/// ### Warning
/// - Only one `SessionManager` instance should exist in the system. Use the
/// [`GLOBAL_SESSION_MANAGER`] for global access in multi-threaded contexts.
///
/// ### Internal Use
/// Developers working within this crate can use `SessionManager` to manage session lifecycle
/// explicitly. For most external users, session management is handled transparently by [`Session`].
///
/// # Examples
///
/// ## Internal Use: Acquiring and Releasing a Session
///
/// The following example demonstrates how crate-internal code can acquire and release a session:
///
/// ```ignore
/// use tradier::wssession::session_manager::SessionManager;
///
/// let manager = SessionManager::default();
/// assert!(manager.acquire_session().is_ok());
/// assert!(manager.acquire_session().is_err()); // Second acquisition fails
/// manager.release_session(); // Releases the session
/// assert!(manager.acquire_session().is_ok()); // Session can be reacquired
/// ```
pub
/// A globally accessible, lazily-initialized singleton instance of `SessionManager`.
///
/// The `GLOBAL_SESSION_MANAGER` provides a single, thread-safe instance of `SessionManager`
/// for use within the crate. It is not directly accessible outside the crate due to its
/// `pub(crate)` visibility.
///
/// Most session-related operations should use higher-level abstractions like [`Session`].
///
/// ### Key Features
/// - **Lazy Initialization**: The `SessionManager` is created only when first accessed.
/// - **Thread-Safe**: Ensures safe, concurrent access across threads.
///
/// ### Internal Usage
///
/// Direct access to `GLOBAL_SESSION_MANAGER` is intended only for crate-internal use. External
/// consumers of the library should use public abstractions like [`Session`].
pub static GLOBAL_SESSION_MANAGER: =
new;