rid

A pratical distributed id generator by rust

Snowflake based unique ID generator. It works as a component, and allows users to override workId bits and initialization strategy. As a result, it is much more suitable for virtualization environment, such as docker.

Snowflake

Snowflake algorithm: An unique id consists of worker node, timestamp and sequence within that timestamp. Usually, it is a 64 bits number(long), and the default bits of that three fields are as follows: xml +------+----------------------+----------------+-----------+ | sign | delta seconds | worker node id | sequence | +------+----------------------+----------------+-----------+ 1bit 30bits 20bits 13bits

sign(1bit) The highest bit is always 0.

delta seconds (30 bits) The next 30 bits, represents delta seconds since a customer epoch(2016-05-20). The maximum time will be 34 years.

worker id (20 bits) The next 20 bits, represents the worker node id, maximum value will be 1.04 million. UidGenerator uses a build-in database based worker id assigner when startup by default, and it will reuse previous work node id after reboot.

sequence (13 bits) the last 13 bits, represents sequence within the one second, maximum is 8192 per second(per server)by default.

Features

Design

Quick Start

Step1: Install rust, Mysql

Step2: Create table worker_node

``sql DROP TABLE IF EXISTSworkernode; CREATE TABLEworkernode( idbigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'auto increment id', hostnamevarchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'host name', portvarchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'port', typeint NOT NULL COMMENT 'node type: CONTAINER(1), ACTUAL(2), FAKE(3)', modifiedtimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'modified time', createdtimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'created time', PRIMARY KEY (id), UNIQUE KEYUNIQIDXHOSTPORT(hostname,port`) USING BTREE COMMENT 'host和端口的唯一索引' ) ENGINE=InnoDB AUTOINCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4generalci COMMENT='DB WorkerID Assigner for UID Generator';

```

Step3: Install Lib

Step4: Usage

```rust let config = ridconfig::UidConfig::new("5000".tostring()); let rb: Rbatis = Rbatis::new(); rb.link("mysql://root:root@127.0.0.1:3306/test") .await .expect("Couldn't open database"); let mut idg = rid_generator::UidGenerator::new(&config, Arc::new(rb)).await;

let start = Local::now().timestampmillis(); for _ in 1..10000 { //println!("{}", &idg.getuid()); let _ = &idg.get_uid(); }

```

Customization

Change the timebits, workerbits, seq_bits of 'UidConfig' to get your customer uid, especially shorter uid.

```rust let mut config = ridconfig::UidConfig::new("5000".tostring()); config.workerbits = 10; config.seqbits = 23;

let rb: Rbatis = Rbatis::new(); rb.link("mysql://root:root@127.0.0.1:3306/test") .await .expect("Couldn't open database"); let mut idg = rid_generator::UidGenerator::new(&config, Arc::new(rb)).await;

let start = Local::now().timestampmillis(); for _ in 1..1000000 { //println!("{}", &idg.getuid()); let _ = &idg.get_uid(); }

```

ChangeLog

License

Gid is MIT licensed.