Skip to content

Better ID generation  #16

@quantumish

Description

@quantumish

The current ID generation function is a mild atrocity:

pub fn gen_id() -> i64 {
    static STATE: Mutex<Option<Snowflake>> = Mutex::new(None);

    STATE
        .lock()
        .unwrap()
        .get_or_insert_with(|| Snowflake::default())
        .generate()
}

Making this all gated behind a mutex effectively defeats the purpose of parallelism in other parts of the program, yet finding a better solution is challenging. poem doesn't let servers have mutable state, yet the Snowflake ID generator needs to be mutable in order to update itself.

You can do something very questionable with a mix of dependency injection via poem's Data, RefCells, and unstable rust, but it isn't a good solution either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions