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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::{
    types::Callback, LongPollingServiceContext, SessionAddedArgs, SessionRemovedArgs, SubscribeArgs,
};
use ahash::AHashMap;
use core::future::Future;
use std::sync::Arc;
use tokio::sync::RwLock;

const DEFAULT_TIMEOUT_MS: u64 = 20_000;
const DEFAULT_INTERVAL_MS: u64 = 0;
const DEFAULT_MAX_INTERVAL_MS: u64 = 60_000;
const DEFAULT_CHANNEL_CAPACITY: usize = 500;
const DEFAULT_STORAGE_CAPACITY: usize = 10_000;

/// A builder to construct `LongPoolingServiceContext`.
#[derive(Debug)]
pub struct LongPollingServiceContextBuilder {
    subscriptions_storage_capacity: usize,
    client_ids_storage_capacity: usize,
    consts: LongPollingServiceContextConsts,
    session_added: Callback<SessionAddedArgs>,
    subscribe_added: Callback<SubscribeArgs>,
    session_removed: Callback<SessionRemovedArgs>,
}

impl Default for LongPollingServiceContextBuilder {
    #[inline(always)]
    fn default() -> Self {
        Self {
            subscriptions_storage_capacity: DEFAULT_STORAGE_CAPACITY,
            client_ids_storage_capacity: DEFAULT_STORAGE_CAPACITY,
            consts: Default::default(),
            session_added: Callback::Empty,
            subscribe_added: Callback::Empty,
            session_removed: Callback::Empty,
        }
    }
}

#[derive(Debug)]
pub(crate) struct LongPollingServiceContextConsts {
    pub(crate) timeout_ms: u64,
    pub(crate) interval_ms: u64,
    pub(crate) max_interval_ms: u64,
    pub(crate) client_channel_capacity: usize,
    pub(crate) subscription_channel_capacity: usize,
}

impl Default for LongPollingServiceContextConsts {
    #[inline(always)]
    fn default() -> Self {
        Self {
            timeout_ms: DEFAULT_TIMEOUT_MS,
            interval_ms: DEFAULT_INTERVAL_MS,
            max_interval_ms: DEFAULT_MAX_INTERVAL_MS,
            client_channel_capacity: DEFAULT_CHANNEL_CAPACITY,
            subscription_channel_capacity: DEFAULT_CHANNEL_CAPACITY,
        }
    }
}

impl LongPollingServiceContextBuilder {
    /// Construct a new `LongPoolingServiceContextBuilder`.
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Return a `LongPoolingServiceContextBuilder`.
    ///
    /// # Example
    /// ```rust
    /// use axum_cometd::LongPollingServiceContextBuilder;
    ///
    /// let context = LongPollingServiceContextBuilder::new().build();
    /// ```
    #[inline(always)]
    pub fn build(self) -> Arc<LongPollingServiceContext> {
        let Self {
            subscriptions_storage_capacity,
            client_ids_storage_capacity,
            consts,
            session_added,
            subscribe_added,
            session_removed,
        } = self;

        Arc::new(LongPollingServiceContext {
            session_added,
            subscribe_added,
            session_removed,
            wildnames_cache: Default::default(),
            channel_name_validator: Default::default(),
            consts,
            channels_data: RwLock::new(AHashMap::with_capacity(subscriptions_storage_capacity)),
            client_id_senders: Arc::new(RwLock::new(AHashMap::with_capacity(
                client_ids_storage_capacity,
            ))),
        })
    }

    /// Set message wait timeout in milliseconds.
    #[inline(always)]
    #[must_use]
    pub const fn timeout_ms(mut self, timeout_ms: u64) -> Self {
        self.consts.timeout_ms = timeout_ms;
        self
    }

    /// Set timeout in milliseconds, that the client must wait between two connects.
    #[inline(always)]
    #[must_use]
    pub fn interval_ms(self, _interval_ms: u64) -> Self {
        unimplemented!()
        /*Self {
            consts: LongPoolingServiceContextConsts {
                interval_ms,
                ..self.consts
            },
            ..self
        }*/
    }

    /// Set timeout in milliseconds, which server wait between erase clientId.
    #[inline(always)]
    #[must_use]
    pub const fn max_interval_ms(mut self, max_interval_ms: u64) -> Self {
        self.consts.max_interval_ms = max_interval_ms;
        self
    }

    /// Set capacity of internal client channels.
    #[inline(always)]
    #[must_use]
    pub const fn client_channel_capacity(mut self, capacity: usize) -> Self {
        self.consts.client_channel_capacity = capacity;
        self
    }

    /// Set capacity of internal client channels storage.
    #[inline(always)]
    #[must_use]
    pub const fn client_storage_capacity(mut self, capacity: usize) -> Self {
        self.client_ids_storage_capacity = capacity;
        self
    }

    /// Set capacity of internal subscription channels.
    #[inline(always)]
    #[must_use]
    pub const fn subscription_channel_capacity(mut self, capacity: usize) -> Self {
        self.consts.subscription_channel_capacity = capacity;
        self
    }

    /// Set capacity of internal subscription channels storage.
    #[inline(always)]
    #[must_use]
    pub const fn subscription_storage_capacity(mut self, capacity: usize) -> Self {
        self.subscriptions_storage_capacity = capacity;
        self
    }

    /// Set sync callback on new session creation.
    #[inline(always)]
    #[must_use]
    pub fn session_added<F>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SessionAddedArgs) + Send + Sync + 'static,
    {
        Self {
            session_added: Callback::new_sync(callback),
            ..self
        }
    }

    /// Set async callback on new session creation.
    #[inline(always)]
    #[must_use]
    pub fn async_session_added<F, Fut>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SessionAddedArgs) -> Fut + Sync + Send + 'static,
        Fut: Future<Output = ()> + Sync + Send + 'static,
    {
        Self {
            session_added: Callback::new_async(callback),
            ..self
        }
    }

    /// Set sync callback on new subscribe creation.
    #[inline(always)]
    #[must_use]
    pub fn subscribe_added<F>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SubscribeArgs) + Send + Sync + 'static,
    {
        Self {
            subscribe_added: Callback::new_sync(callback),
            ..self
        }
    }

    /// Set async callback on new subscribe creation.
    #[inline(always)]
    #[must_use]
    pub fn async_subscribe_added<F, Fut>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SubscribeArgs) -> Fut + Sync + Send + 'static,
        Fut: Future<Output = ()> + Sync + Send + 'static,
    {
        Self {
            subscribe_added: Callback::new_async(callback),
            ..self
        }
    }

    /// Set sync callback on new session creation.
    #[inline(always)]
    #[must_use]
    pub fn session_removed<F>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SessionRemovedArgs) + Send + Sync + 'static,
    {
        Self {
            session_removed: Callback::new_sync(callback),
            ..self
        }
    }

    /// Set async callback on new session creation.
    #[inline(always)]
    #[must_use]
    pub fn async_session_removed<F, Fut>(self, callback: F) -> Self
    where
        F: Fn(&Arc<LongPollingServiceContext>, SessionRemovedArgs) -> Fut + Sync + Send + 'static,
        Fut: Future<Output = ()> + Sync + Send + 'static,
    {
        Self {
            session_removed: Callback::new_async(callback),
            ..self
        }
    }
}