Use postgresql style uuid generation (uuid_generate_v1mc) for Context uuids (#38089)

This avoids the syscall to getrandom() every time we generate a uuid.
pull/38098/head
J. Nick Koston 2020-07-22 18:53:01 -10:00 committed by GitHub
parent d7811a4adf
commit 6e2025a748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 1 deletions

View File

@ -12,6 +12,7 @@ from ipaddress import ip_address
import logging
import os
import pathlib
import random
import re
import threading
from time import monotonic
@ -458,7 +459,13 @@ class Context:
user_id: str = attr.ib(default=None)
parent_id: Optional[str] = attr.ib(default=None)
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
# The uuid1 uses a random multicast MAC address instead of the real MAC address
# of the machine without the overhead of calling the getrandom() system call.
#
# This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
id: str = attr.ib(
factory=lambda: uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex
)
def as_dict(self) -> dict:
"""Return a dictionary representation of the context."""