Uniq id generator based on Twitter's snowflake in Erlang
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
include
src
test
.gitignore
.travis.yml
LICENSE
Makefile
README.md
elvis.config
rebar.config
rebar.lock

README.md

Build Status

esnowflake

esnowflake is Erlang/OTP application to generate uniq ID.
Original design: Twitter IDs (snowflake).
Documentation

Usage

> application:start(esnowflake).
ok

> Id = esnowflake:generate_id().
896221795344384

> esnowflake:generate_ids(2).
[896498611015681,896498611015680]

> esnowflake:to_unixtime(Id).
1509193995927

> esnowflake:stats().
[{version, undefined},
 {worker_num,10},
 {worker_ids,[0,1,2,3,4,5,6,7,8,9]}]

1> {Start, End} = {os:system_time(seconds)-3600*24, os:system_time(seconds)}.
{1528603140,1528689540}
2> esnowflake:range_ids(Start,End, seconds).
[82304072417280000,82666460287074303]

Config

worker id

application environment variable

This must be specified as not to duplicate worker ids if you use multi nodes.

params default explain
worker_num 2 number of generate id workers
worker_min_max_id [0, 1] worker ids
redis - eredis config for assigning worker ids automatically
  • redis config
params default
host localhost
port 6379
database 0
password (empty)
reconnect_sleep please check eredis:start_link/1
connect_timeout please check eredis:start_link/1
  • app.conf
[
 {esnowflake, [
               {worker_min_max_id, [0, 1]}
              ]}
].
[
 {esnowflake, [
               {redis, [{host, "localhost"}, {port, 26379}]},
               {worker_num, 2}
              ]}
].

Bench

----------------------------------------------------
2017-10-29 16:03:27.671
b_generate_id	100000	4564.13864 ns/op


----------------------------------------------------
2017-10-29 16:03:28.703
b_generate_id	232502 op/sec

%%% esnowflake_SUITE ==> bench.b_generate_id: OK

----------------------------------------------------
2017-10-29 16:03:29.700
b_generate_ids_100	10000	96834.8342 ns/op


----------------------------------------------------
2017-10-29 16:03:30.702
b_generate_ids_100	686 op/sec

%%% esnowflake_SUITE ==> bench.b_generate_ids: OK