aaron-ai commented on code in PR #530:
URL: https://github.com/apache/rocketmq-clients/pull/530#discussion_r1213877119


##########
python/client/message/message_id_codec.py:
##########
@@ -0,0 +1,78 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+import os
+import threading
+import time
+import uuid
+from datetime import datetime, timezone
+
+
+class MessageIdCodec:
+    MESSAGE_ID_LENGTH_FOR_V1_OR_LATER = 34
+    MESSAGE_ID_VERSION_V0 = "00"
+    MESSAGE_ID_VERSION_V1 = "01"
+
+    _instance = None
+    _lock = threading.Lock()
+
+    def __new__(cls, *args, **kwargs):
+        if not cls._instance:
+            with cls._lock:
+                if not cls._instance:
+                    cls._instance = super(MessageIdCodec, cls).__new__(cls)
+        return cls._instance
+
+    def __init__(self):
+        self.processFixedStringV1 = self._get_process_fixed_string()
+        self.secondsSinceCustomEpoch = self._get_seconds_since_custom_epoch()
+        self.secondsStartTimestamp = int(time.time())
+        self.seconds = self._delta_seconds()
+        self.sequence = 0
+
+    def _get_process_fixed_string(self):
+        mac = uuid.getnode()
+        mac = format(mac, '012x')
+        mac_bytes = bytes.fromhex(mac[-12:])
+        pid = os.getpid()
+        pid_bytes = pid.to_bytes(2, 'big')

Review Comment:
   > I didn't encounter this issue on my machine (macos). It seems to be due to 
the process id being too large, causing an overflow that can't fit into 2 
bytes. This can be resolved by allocating more bytes. However, the process id 
in the message id is constrained to two bytes. If we do not consider the issue 
of duplication, we can also address this issue by making the following 
modifications:
   > 
   > Pid=os. getpid ()% 65536 Pid_ Bytes=pid. to_ Bytes (2, 'big')
   
   Nice! actually we pick the lower 2 bytes rather than the whole process id, 
so it makes sense.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to