rocketmq-client-rust 1.0.0

Rust implementation of Apache rocketmq client
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Producer configuration validation module
//!
//! This module provides validation functions for ProducerConfig fields,
//! ensuring that configuration values are within acceptable bounds and
//! properly formatted.

use crate::ClientError;
use crate::ClientResult;

/// Validator for ProducerConfig fields
///
/// Provides validation methods for all producer configuration parameters,
/// ensuring they meet RocketMQ requirements.
pub struct ProducerConfigValidator;

impl ProducerConfigValidator {
    // =========================================================================
    // Validation Constants
    // =========================================================================

    /// Minimum send message timeout (100ms)
    pub const MIN_SEND_MSG_TIMEOUT: u32 = 100;

    /// Maximum send message timeout (5 minutes)
    pub const MAX_SEND_MSG_TIMEOUT: u32 = 300_000;

    /// Minimum maximum message size (1KB)
    pub const MIN_MAX_MESSAGE_SIZE: u32 = 1024;

    /// Maximum maximum message size (128MB)
    pub const MAX_MAX_MESSAGE_SIZE: u32 = 128 * 1024 * 1024;

    /// Minimum default topic queue numbers
    pub const MIN_DEFAULT_TOPIC_QUEUE_NUMS: u32 = 1;

    /// Maximum default topic queue numbers
    pub const MAX_DEFAULT_TOPIC_QUEUE_NUMS: u32 = 1024;

    /// Minimum retry times
    pub const MIN_RETRY_TIMES: u32 = 0;

    /// Maximum retry times
    pub const MAX_RETRY_TIMES: u32 = 16;

    /// Minimum compress message body threshold (1KB)
    pub const MIN_COMPRESS_MSG_BODY_OVER_HOWMUCH: u32 = 1024;

    /// Maximum compress message body threshold (64MB)
    pub const MAX_COMPRESS_MSG_BODY_OVER_HOWMUCH: u32 = 64 * 1024 * 1024;

    /// Minimum batch max delay (0ms)
    pub const MIN_BATCH_MAX_DELAY_MS: u32 = 0;

    /// Maximum batch max delay (60 seconds)
    pub const MAX_BATCH_MAX_DELAY_MS: u32 = 60_000;

    /// Minimum back pressure for async send num (10)
    pub const MIN_BACK_PRESSURE_FOR_ASYNC_SEND_NUM: u32 = 10;

    /// Maximum back pressure for async send num (100,000)
    pub const MAX_BACK_PRESSURE_FOR_ASYNC_SEND_NUM: u32 = 100_000;

    /// Minimum back pressure for async send size (1MB)
    pub const MIN_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE: u32 = 1024 * 1024;

    /// Maximum back pressure for async send size (1GB)
    pub const MAX_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE: u32 = 1024 * 1024 * 1024;

    // =========================================================================
    // Validation Methods
    // =========================================================================

    /// Validate send message timeout
    ///
    /// Ensures the timeout is between 100ms and 5 minutes.
    pub fn validate_send_msg_timeout(timeout: u32) -> ClientResult<()> {
        if !(Self::MIN_SEND_MSG_TIMEOUT..=Self::MAX_SEND_MSG_TIMEOUT).contains(&timeout) {
            return Err(ClientError::config_invalid(
                "send_msg_timeout",
                timeout.to_string(),
                format!(
                    "must be between {} and {} milliseconds",
                    Self::MIN_SEND_MSG_TIMEOUT,
                    Self::MAX_SEND_MSG_TIMEOUT
                ),
            ));
        }
        Ok(())
    }

    /// Validate send message max timeout per request
    ///
    /// None means no limit, otherwise validates the timeout.
    pub fn validate_send_msg_max_timeout_per_request(timeout: Option<u32>) -> ClientResult<()> {
        if let Some(t) = timeout {
            if !(Self::MIN_SEND_MSG_TIMEOUT..=Self::MAX_SEND_MSG_TIMEOUT).contains(&t) {
                return Err(ClientError::config_invalid(
                    "send_msg_max_timeout_per_request",
                    t.to_string(),
                    format!(
                        "must be between {} and {} milliseconds, or None for no limit",
                        Self::MIN_SEND_MSG_TIMEOUT,
                        Self::MAX_SEND_MSG_TIMEOUT
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Validate maximum message size
    ///
    /// Ensures the size is between 1KB and 128MB.
    pub fn validate_max_message_size(size: u32) -> ClientResult<()> {
        if !(Self::MIN_MAX_MESSAGE_SIZE..=Self::MAX_MAX_MESSAGE_SIZE).contains(&size) {
            return Err(ClientError::config_invalid(
                "max_message_size",
                size.to_string(),
                format!(
                    "must be between {} and {} bytes",
                    Self::MIN_MAX_MESSAGE_SIZE,
                    Self::MAX_MAX_MESSAGE_SIZE
                ),
            ));
        }
        Ok(())
    }

    /// Validate default topic queue numbers
    ///
    /// Ensures the queue count is between 1 and 1024.
    pub fn validate_default_topic_queue_nums(nums: u32) -> ClientResult<()> {
        if !(Self::MIN_DEFAULT_TOPIC_QUEUE_NUMS..=Self::MAX_DEFAULT_TOPIC_QUEUE_NUMS).contains(&nums) {
            return Err(ClientError::config_invalid(
                "default_topic_queue_nums",
                nums.to_string(),
                format!(
                    "must be between {} and {}",
                    Self::MIN_DEFAULT_TOPIC_QUEUE_NUMS,
                    Self::MAX_DEFAULT_TOPIC_QUEUE_NUMS
                ),
            ));
        }
        Ok(())
    }

    /// Validate retry times when send failed
    ///
    /// Ensures the retry count is between 0 and 16.
    pub fn validate_retry_times_when_send_failed(times: u32) -> ClientResult<()> {
        if times > Self::MAX_RETRY_TIMES {
            return Err(ClientError::config_invalid(
                "retry_times_when_send_failed",
                times.to_string(),
                format!("must not exceed {}", Self::MAX_RETRY_TIMES),
            ));
        }
        Ok(())
    }

    /// Validate retry times when send async failed
    ///
    /// Ensures the retry count is between 0 and 16.
    pub fn validate_retry_times_when_send_async_failed(times: u32) -> ClientResult<()> {
        if times > Self::MAX_RETRY_TIMES {
            return Err(ClientError::config_invalid(
                "retry_times_when_send_async_failed",
                times.to_string(),
                format!("must not exceed {}", Self::MAX_RETRY_TIMES),
            ));
        }
        Ok(())
    }

    /// Validate compress message body threshold
    ///
    /// Ensures the threshold is between 1KB and 64MB.
    pub fn validate_compress_msg_body_over_howmuch(threshold: u32) -> ClientResult<()> {
        if !(Self::MIN_COMPRESS_MSG_BODY_OVER_HOWMUCH..=Self::MAX_COMPRESS_MSG_BODY_OVER_HOWMUCH).contains(&threshold) {
            return Err(ClientError::config_invalid(
                "compress_msg_body_over_howmuch",
                threshold.to_string(),
                format!(
                    "must be between {} and {} bytes",
                    Self::MIN_COMPRESS_MSG_BODY_OVER_HOWMUCH,
                    Self::MAX_COMPRESS_MSG_BODY_OVER_HOWMUCH
                ),
            ));
        }
        Ok(())
    }

    /// Validate batch max delay
    ///
    /// None means no delay, otherwise validates the delay is within bounds.
    pub fn validate_batch_max_delay_ms(delay: Option<u32>) -> ClientResult<()> {
        if let Some(d) = delay {
            if d > Self::MAX_BATCH_MAX_DELAY_MS {
                return Err(ClientError::config_invalid(
                    "batch_max_delay_ms",
                    d.to_string(),
                    format!(
                        "must be at most {} milliseconds, or None for no delay",
                        Self::MAX_BATCH_MAX_DELAY_MS
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Validate back pressure for async send num
    ///
    /// Ensures the value is between 10 and 100,000.
    pub fn validate_back_pressure_for_async_send_num(num: u32) -> ClientResult<()> {
        if !(Self::MIN_BACK_PRESSURE_FOR_ASYNC_SEND_NUM..=Self::MAX_BACK_PRESSURE_FOR_ASYNC_SEND_NUM).contains(&num) {
            return Err(ClientError::config_invalid(
                "back_pressure_for_async_send_num",
                num.to_string(),
                format!(
                    "must be between {} and {}",
                    Self::MIN_BACK_PRESSURE_FOR_ASYNC_SEND_NUM,
                    Self::MAX_BACK_PRESSURE_FOR_ASYNC_SEND_NUM
                ),
            ));
        }
        Ok(())
    }

    /// Validate back pressure for async send size
    ///
    /// Ensures the size is between 1MB and 1GB.
    pub fn validate_back_pressure_for_async_send_size(size: u32) -> ClientResult<()> {
        if !(Self::MIN_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE..=Self::MAX_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE).contains(&size)
        {
            return Err(ClientError::config_invalid(
                "back_pressure_for_async_send_size",
                size.to_string(),
                format!(
                    "must be between {} and {} bytes",
                    Self::MIN_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE,
                    Self::MAX_BACK_PRESSURE_FOR_ASYNC_SEND_SIZE
                ),
            ));
        }
        Ok(())
    }

    /// Validate batch max bytes
    ///
    /// None means no limit, otherwise validates the size is within reasonable bounds.
    /// Max batch size should not exceed 32MB (matches Java: batchMaxBytes).
    pub fn validate_batch_max_bytes(size: Option<u64>) -> ClientResult<()> {
        const MIN_BATCH_MAX_BYTES: u64 = 1024; // 1KB
        const MAX_BATCH_MAX_BYTES: u64 = 32 * 1024 * 1024; // 32MB

        if let Some(s) = size {
            if !(MIN_BATCH_MAX_BYTES..=MAX_BATCH_MAX_BYTES).contains(&s) {
                return Err(ClientError::config_invalid(
                    "batch_max_bytes",
                    s.to_string(),
                    format!(
                        "must be between {} and {} bytes, or None for no limit",
                        MIN_BATCH_MAX_BYTES, MAX_BATCH_MAX_BYTES
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Validate total batch max bytes
    ///
    /// None means no limit, otherwise validates the size is within reasonable bounds.
    /// Max total batch size should not exceed 256MB
    pub fn validate_total_batch_max_bytes(size: Option<u64>) -> ClientResult<()> {
        const MIN_TOTAL_BATCH_MAX_BYTES: u64 = 1024; // 1KB
        const MAX_TOTAL_BATCH_MAX_BYTES: u64 = 256 * 1024 * 1024; // 256MB

        if let Some(s) = size {
            if !(MIN_TOTAL_BATCH_MAX_BYTES..=MAX_TOTAL_BATCH_MAX_BYTES).contains(&s) {
                return Err(ClientError::config_invalid(
                    "total_batch_max_bytes",
                    s.to_string(),
                    format!(
                        "must be between {} and {} bytes, or None for no limit",
                        MIN_TOTAL_BATCH_MAX_BYTES, MAX_TOTAL_BATCH_MAX_BYTES
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Validate producer group
    ///
    /// Ensures the producer group is not empty.
    pub fn validate_producer_group(group: &str) -> ClientResult<()> {
        if group.is_empty() {
            return Err(ClientError::config_missing("producer_group"));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_send_msg_timeout_valid() {
        assert!(ProducerConfigValidator::validate_send_msg_timeout(3000).is_ok());
        assert!(ProducerConfigValidator::validate_send_msg_timeout(100).is_ok());
        assert!(ProducerConfigValidator::validate_send_msg_timeout(300_000).is_ok());
    }

    #[test]
    fn test_validate_send_msg_timeout_invalid() {
        assert!(ProducerConfigValidator::validate_send_msg_timeout(10).is_err());
        assert!(ProducerConfigValidator::validate_send_msg_timeout(400_000).is_err());
    }

    #[test]
    fn test_validate_send_msg_max_timeout_per_request_none() {
        assert!(ProducerConfigValidator::validate_send_msg_max_timeout_per_request(None).is_ok());
    }

    #[test]
    fn test_validate_send_msg_max_timeout_per_request_valid() {
        assert!(ProducerConfigValidator::validate_send_msg_max_timeout_per_request(Some(3000)).is_ok());
    }

    #[test]
    fn test_validate_send_msg_max_timeout_per_request_invalid() {
        assert!(ProducerConfigValidator::validate_send_msg_max_timeout_per_request(Some(10)).is_err());
    }

    #[test]
    fn test_validate_max_message_size_valid() {
        assert!(ProducerConfigValidator::validate_max_message_size(4 * 1024 * 1024).is_ok());
        assert!(ProducerConfigValidator::validate_max_message_size(1024).is_ok());
        assert!(ProducerConfigValidator::validate_max_message_size(128 * 1024 * 1024).is_ok());
    }

    #[test]
    fn test_validate_max_message_size_invalid() {
        assert!(ProducerConfigValidator::validate_max_message_size(512).is_err());
        assert!(ProducerConfigValidator::validate_max_message_size(256 * 1024 * 1024).is_err());
    }

    #[test]
    fn test_validate_retry_times_when_send_failed_valid() {
        assert!(ProducerConfigValidator::validate_retry_times_when_send_failed(0).is_ok());
        assert!(ProducerConfigValidator::validate_retry_times_when_send_failed(2).is_ok());
        assert!(ProducerConfigValidator::validate_retry_times_when_send_failed(16).is_ok());
    }

    #[test]
    fn test_validate_retry_times_when_send_failed_invalid() {
        assert!(ProducerConfigValidator::validate_retry_times_when_send_failed(17).is_err());
    }

    #[test]
    fn test_validate_back_pressure_for_async_send_num_valid() {
        assert!(ProducerConfigValidator::validate_back_pressure_for_async_send_num(1024).is_ok());
        assert!(ProducerConfigValidator::validate_back_pressure_for_async_send_num(10).is_ok());
    }

    #[test]
    fn test_validate_back_pressure_for_async_send_num_invalid() {
        assert!(ProducerConfigValidator::validate_back_pressure_for_async_send_num(9).is_err());
        assert!(ProducerConfigValidator::validate_back_pressure_for_async_send_num(100_001).is_err());
    }

    #[test]
    fn test_validate_producer_group_empty() {
        assert!(ProducerConfigValidator::validate_producer_group("").is_err());
    }

    #[test]
    fn test_validate_producer_group_valid() {
        assert!(ProducerConfigValidator::validate_producer_group("test_group").is_ok());
    }

    #[test]
    fn test_validate_batch_max_delay_ms_none() {
        assert!(ProducerConfigValidator::validate_batch_max_delay_ms(None).is_ok());
    }

    #[test]
    fn test_validate_batch_max_delay_ms_valid() {
        assert!(ProducerConfigValidator::validate_batch_max_delay_ms(Some(1000)).is_ok());
    }

    #[test]
    fn test_validate_batch_max_delay_ms_invalid() {
        assert!(ProducerConfigValidator::validate_batch_max_delay_ms(Some(70_000)).is_err());
    }

    #[test]
    fn test_validate_batch_max_bytes_none() {
        assert!(ProducerConfigValidator::validate_batch_max_bytes(None).is_ok());
    }

    #[test]
    fn test_validate_batch_max_bytes_valid() {
        assert!(ProducerConfigValidator::validate_batch_max_bytes(Some(1024)).is_ok());
        assert!(ProducerConfigValidator::validate_batch_max_bytes(Some(4 * 1024 * 1024)).is_ok());
        assert!(ProducerConfigValidator::validate_batch_max_bytes(Some(32 * 1024 * 1024)).is_ok());
    }

    #[test]
    fn test_validate_batch_max_bytes_invalid() {
        assert!(ProducerConfigValidator::validate_batch_max_bytes(Some(512)).is_err());
        assert!(ProducerConfigValidator::validate_batch_max_bytes(Some(64 * 1024 * 1024)).is_err());
    }

    #[test]
    fn test_validate_total_batch_max_bytes_none() {
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(None).is_ok());
    }

    #[test]
    fn test_validate_total_batch_max_bytes_valid() {
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(Some(1024)).is_ok());
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(Some(64 * 1024 * 1024)).is_ok());
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(Some(256 * 1024 * 1024)).is_ok());
    }

    #[test]
    fn test_validate_total_batch_max_bytes_invalid() {
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(Some(512)).is_err());
        assert!(ProducerConfigValidator::validate_total_batch_max_bytes(Some(512 * 1024 * 1024)).is_err());
    }
}