-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add: _uuid module #3974
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
Add: _uuid module #3974
Conversation
rimi0108
commented
Jul 26, 2022
- Add _uuid module
- Add _uuid function generate_time_safe()
- uuid v1 = generate_time_safe
- Return mac address to node_id if you have mac address or return random generated bytes to node_id if you do not have mac address
@@ -63,6 +63,8 @@ ahash = "0.7.6" | |||
libz-sys = { version = "1.1.5", optional = true } | |||
num_enum = "0.5.7" | |||
ascii = "1.0.0" | |||
once_cell = "1.13.0" | |||
mac_address = "1.1.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this line instead of dependencies.uuid
section
will be more easy to read
uuid = { version = "1.1.2", features = ["v1", "fast-rng", "macro-diagnostics"] }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mac_address
needs more cfg guard for more OS.
See mmap
case. By referring CI failure, we (at least) need to exclude iOS and android.
stdlib/src/lib.rs
Outdated
@@ -51,6 +51,8 @@ mod select; | |||
mod ssl; | |||
#[cfg(all(unix, not(target_os = "redox")))] | |||
mod termios; | |||
#[cfg(target_os = "macos")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the blocker for linux and windows?
stdlib/src/uuid.rs
Outdated
|
||
#[pymodule] | ||
mod _uuid { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdlib/src/uuid.rs
Outdated
match get_mac_address() { | ||
Ok(Some(_ma)) => { | ||
let node_id = NODE_ID.get_or_init(|| get_mac_address().unwrap().unwrap().bytes()); | ||
node_id | ||
} | ||
Ok(None) => { | ||
let node_id = NODE_ID.get_or_init(|| rand::thread_rng().gen::<[u8; 6]>()); | ||
node_id | ||
} | ||
Err(_e) => { | ||
let node_id = NODE_ID.get_or_init(|| rand::thread_rng().gen::<[u8; 6]>()); | ||
node_id | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is checking get_mac_address
every time. That probably is not expectation for node id. NODE_ID.get_or_init
must be the outmost wrapper of this function
|
stdlib/src/uuid.rs
Outdated
Ok(None) => { | ||
let node_id = rand::thread_rng().gen::<[u8; 6]>(); | ||
node_id | ||
} | ||
Err(_e) => { | ||
let node_id = rand::thread_rng().gen::<[u8; 6]>(); | ||
node_id | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok(None) => { | |
let node_id = rand::thread_rng().gen::<[u8; 6]>(); | |
node_id | |
} | |
Err(_e) => { | |
let node_id = rand::thread_rng().gen::<[u8; 6]>(); | |
node_id | |
} | |
_ => { | |
let node_id = rand::thread_rng().gen::<[u8; 6]>(); | |
node_id | |
} |
stdlib/src/uuid.rs
Outdated
let now = now_unix_duration(); | ||
static CONTEXT: Context = Context::new(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let now = now_unix_duration(); | |
static CONTEXT: Context = Context::new(0); | |
static CONTEXT: Context = Context::new(0); | |
let now = now_unix_duration(); |
technically this is ok but I feel uncomfortable to have static below without a good reason
stdlib/src/uuid.rs
Outdated
static CONTEXT: Context = Context::new(0); | ||
let ts = Timestamp::from_unix(&CONTEXT, now.as_secs(), now.subsec_nanos()); | ||
|
||
let node_id = get_node_id(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we will not use the value from second call, why do we call this function every time?
You can move this line in get_or_init().
stdlib/src/uuid.rs
Outdated
} | ||
|
||
#[pyfunction] | ||
fn generate_time_safe(_vm: &VirtualMachine) -> (Vec<u8>, PyNone) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn generate_time_safe(_vm: &VirtualMachine) -> (Vec<u8>, PyNone) { | |
fn generate_time_safe() -> (Vec<u8>, PyNone) { |
stdlib/src/uuid.rs
Outdated
} | ||
|
||
#[pyattr] | ||
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt { | |
fn has_uuid_generate_time_safe() -> u32 { |
stdlib/src/uuid.rs
Outdated
|
||
#[pyattr] | ||
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt { | ||
PyInt::from(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting. What this value is meaning? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote the code like that because it was printed below the cpython
>>> _uuid.has_uuid_generate_time_safe
0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem didn't set git email correctly. Check git log
and if it matches your actual email (and github email)
@@ -63,6 +63,8 @@ ahash = "0.7.6" | |||
libz-sys = { version = "1.1.5", optional = true } | |||
num_enum = "0.5.7" | |||
ascii = "1.0.0" | |||
once_cell = "1.13.0" | |||
mac_address = "1.1.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mac_address
needs more cfg guard for more OS.
See mmap
case. By referring CI failure, we (at least) need to exclude iOS and android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, thank you for long time effort