Skip to content

remove lock #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions xid/xid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import threading
import time
from typing import Union
import itertools

logger = logging.getLogger("xid")

Expand Down Expand Up @@ -35,19 +36,9 @@ def read_machine_id() -> bytes:
return os.urandom(3)


def generate_next_id():
id_ = rand_int()

while True:
new_id = id_ + 1
id_ += 1
yield new_id


machine_id = read_machine_id()
pid = os.getpid()
xid_generator = generate_next_id()
lock = threading.Lock()
xid_generator = itertools.count(rand_int())


class InvalidXID(Exception):
Expand Down Expand Up @@ -91,9 +82,7 @@ def _generate_new_xid(t: time.time):
id_[8] = _uint8(pid & 0xFF)

# Increment, 3 bytes, big endian
lock.acquire()
i = next(xid_generator)
lock.release()
id_[9] = _uint8(i >> 16 & 0xFF)
id_[10] = _uint8(i >> 8 & 0xFF)
id_[11] = _uint8(i & 0xFF)
Expand Down