{"id":17717584,"url":"https://github.com/al8n/flake","last_synced_at":"2025-07-16T09:35:10.195Z","repository":{"id":62440873,"uuid":"393079343","full_name":"al8n/flake","owner":"al8n","description":"Distributed unique ID generator.","archived":false,"fork":false,"pushed_at":"2021-09-25T12:22:20.000Z","size":38,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-26T13:06:46.070Z","etag":null,"topics":["distributed-id-generator","rust-lang","sonyflake","unique-id","unique-id-generator"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/al8n.png","metadata":{"files":{"readme":"README-zh_CN.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"al8n"}},"created_at":"2021-08-05T14:49:04.000Z","updated_at":"2021-09-25T12:22:23.000Z","dependencies_parsed_at":"2022-11-01T22:16:07.960Z","dependency_job_id":null,"html_url":"https://github.com/al8n/flake","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/al8n/flake","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/al8n%2Fflake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/al8n%2Fflake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/al8n%2Fflake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/al8n%2Fflake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/al8n","download_url":"https://codeload.github.com/al8n/flake/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/al8n%2Fflake/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265500182,"owners_count":23777423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed-id-generator","rust-lang","sonyflake","unique-id","unique-id-generator"],"created_at":"2024-10-25T14:26:58.771Z","updated_at":"2025-07-16T09:35:10.150Z","avatar_url":"https://github.com/al8n.png","language":"Rust","funding_links":["https://github.com/sponsors/al8n"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eInfallible-SonyFlake\u003c/h1\u003e\n\u003c/div\u003e\n\u003cdiv align=\"center\"\u003e\n\nA distributed unique ID generator inspired by [Twitter's Snowflake](https://blog.twitter.com/2010/announcing-snowflake).\n\nThis is a Rust implementation of the original [sony/sonyflake](https://github.com/sony/sonyflake), which is written in Go.\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/GITHUB-infallible--sonyflake-8da0cb?style=for-the-badge\u0026logo=Github\" height=\"22\"\u003e][Github-url]\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-infallible--sonyflake-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logoColor=white\u0026logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K\" height=\"20\"\u003e](https://docs.rs/infallible-sonyflake)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/infallible-sonyflake?logo=rust\u0026style=for-the-badge\" height=\"22\"\u003e][crates-url]\n\n[\u003cimg alt=\"Build\" src=\"https://img.shields.io/badge/Build-passing-brightgreen?style=for-the-badge\u0026logo=Github-Actions\" height=\"22\"\u003e][CI-url]\n[\u003cimg alt=\"codecov\" src=\"https://img.shields.io/codecov/c/gh/al8n/flake?style=for-the-badge\u0026token=N7EPJLUZ0G\u0026logo=codecov\" height=\"22\"\u003e][codecov-url]\n[\u003cimg alt=\"license\" src=\"https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=for-the-badge\u0026logo=Apache\" height=\"22\"\u003e][license-url]\n\n[English](README.md) | 简体中文\n\nA Sonyflake ID is composed of\n\n```\n39 bits for time in units of 10 msec\n 8 bits for a sequence number\n16 bits for a machine id\n```\n\u003c/div\u003e\n\n\n\u003cdiv align=\"center\"\u003e\n\u003ch2\u003eInstall\u003c/h2\u003e\n\u003c/div\u003e\n\nAdd the following to your `Cargo.toml`:\n```toml\n[dependencies]\ninfallible-sonyflake = \"0.1\"\n```\n\n\u003cdiv align=\"center\"\u003e\n\u003ch2\u003eQuickstart\u003c/h2\u003e\n\u003c/div\u003e\n\n1. **Fallible SonyFlake**  \n   `Sonyflake` may fail to generate a unique ID when we call `next_id` if time overflows.\n   ```rust\n   use infallible_sonyflake::{SonyFlake, Settings};\n   use chrono::Utc;\n   \n   fn main() {\n       let now = Utc::now();\n       let mut sf = Settings::new().set_start_time(now).into_sonyflake().unwrap();\n       let next_id = sf.next_id().unwrap();\n       println!(\"{}\", next_id); \n   }\n   ```\n2. **Infallible SonyFlake**   \n   `InfaillibleSonyFlake` will always generate a unique ID when we call `next_id` if time overflow happens, it will refresh the `start_time` to the current time.\n   ```rust\n   use infallible_sonyflake::{InfallibleSonyFlake, Settings};\n   use chrono::Utc;\n   \n   fn main() {\n       let now = Utc::now();\n       let mut sf = Settings::new().set_start_time(now).into_infallible_sonyflake().unwrap();\n       let next_id = sf.next_id();\n       println!(\"{}\", next_id); \n   } \n   ```\n3. **Custom machine ID and machine ID checker**\n   ```rust\n   use infallible_sonyflake::{InfallibleSonyFlake, Settings, MachineID, MachineIDChecker, IDParts, Error};\n   use chrono::Utc;\n   \n   struct CustomMachineID {\n       counter: u64,\n       id: u16,\n   }\n   \n   impl MachineID for CustomMachineID {\n       fn machine_id(\u0026mut self) -\u003e Result\u003cu16, Box\u003cdyn std::error::Error + Send + Sync + 'static\u003e\u003e {\n           self.counter += 1;\n           if self.counter % 2 != 0 {\n               Ok(self.id)\n           } else {\n               Err(Box::new(\"NaN\".parse::\u003cu32\u003e().unwrap_err()))\n           }\n       }\n   }\n   \n   struct CustomMachineIDChecker;\n   \n   impl MachineIDChecker for CustomMachineIDChecker {\n       fn check_machine_id(\u0026self, id: u16) -\u003e bool {\n           if id % 2 != 0 {\n               true\n           } else {\n               false\n           }\n       }\n   }\n   \n   fn main() {\n       let mut sf = Settings::new()\n           .set_machine_id(Box::new(CustomMachineID { counter: 0, id: 1 }))\n           .set_check_machine_id(Box::new(CustomMachineIDChecker {}))\n           .into_infallible_sonyflake().unwrap();\n       let id = sf.next_id();\n       let parts = IDParts::decompose(id);\n       assert_eq!(parts.get_machine_id(), 1);\n   \n       let err = Settings::new()\n           .set_machine_id(Box::new(CustomMachineID { counter: 0, id: 2 }))\n           .set_check_machine_id(Box::new(CustomMachineIDChecker {}))\n           .into_infallible_sonyflake().unwrap_err();\n   \n       assert_eq!(format!(\"{}\", err), Error::InvalidMachineID(2).to_string());\n   }\n   ```\n\n\u003cdiv align=\"center\"\u003e\n\u003ch4\u003eLicense\u003c/h4\u003e\n\n\u003csup\u003e\nLicensed under \u003ca href=\"LICENSE\"\u003eApache License, Version\n2.0\u003c/a\u003e.\n\u003c/sup\u003e\n\u003cbr\u003e\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license.\n\u003c/sub\u003e\n\u003c/div\u003e\n\n[Github-url]: https://github.com/al8n/flake/\n[CI-url]: https://github.com/al8n/flake/actions/workflows/ci.yml\n[codecov-url]: https://app.codecov.io/gh/al8n/flake/\n[license-url]: https://opensource.org/licenses/Apache-2.0\n[crates-url]: https://crates.io/crates/infallible-sonyflake\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fal8n%2Fflake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fal8n%2Fflake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fal8n%2Fflake/lists"}