Skip to main content

rocketmq_model/utils/
correlation_id_util.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use uuid::Uuid;
16
17pub struct CorrelationIdUtil;
18
19impl CorrelationIdUtil {
20    /// Creates a new correlation ID using UUID v4 in standard format.
21    ///
22    /// Uses hyphenated format (36 chars) to maintain compatibility with Java's UUID.toString().
23    /// Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
24    ///
25    /// # Returns
26    /// A 36-character UUID string (e.g., "550e8400-e29b-41d4-a716-446655440000")
27    #[inline]
28    pub fn create_correlation_id() -> String {
29        Uuid::new_v4().to_string()
30    }
31
32    /// Creates a new correlation ID using UUID v4 in simple format (without hyphens).
33    ///
34    /// This format is more compact and has ~11% less memory overhead compared to standard format.
35    /// Format: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 hex characters)
36    ///
37    /// # Returns
38    /// A 32-character UUID string (e.g., "550e8400e29b41d4a716446655440000")
39    #[inline]
40    pub fn create_correlation_id_simple() -> String {
41        Uuid::new_v4().simple().to_string()
42    }
43}