• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Clojure
  • License
    BSD 3-Clause "New...
  • Created about 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Decentralized, k-ordered unique IDs in Clojure

Flake

Clojars Project Build Status Dependencies Status

Decentralized, k-ordered unique ID generator.

This is a Clojure implementation of Boundary's Erlang Flake ID service.

Usage

New flake IDs can be generated with the generate! fn from flake's core namespace.

Note that in order to prevent generation of duplicate IDs, init! must be called prior to generating IDs for the first time.

For example:

=> (require '[flake.core :as flake])
=> (flake/init!)
=> (map flake/flake->bigint (take 3 (repeatedly flake/generate!)))
(25978563106299135585558915252224N
 25978563106299135585558915252225N
 25978563106299135585558915252226N)

Here we have generated three BigIntegers which are flake IDs. Note how they are ordered.

It may be desirable to encode these IDs in a shorter representation, such as Base62. The utils namespace provides an encoder:

=> (require '[flake.utils :as utils])
=> (->> (repeatedly flake/generate!)
        first
        flake/flake->bigint
        utils/base62-encode)
"8mwFA958SJ2CZVu9nk"

A flake's middle-most bits are derived from a hardware address, e.g. MAC. If this is not desirable or the caller wishes to have more granular control over which bits are used here, a custom worker-id may be provided:

(import '[java.security SecureRandom])

(defn rand-bytes
  "Return `n` random bytes in an array."
  [n]
  (let [bs (byte-array n)]
    (.nextBytes (SecureRandom.) bs)
    bs))

(def worker-id (rand-bytes 6))

(first (repeatedly (partial flake/generate! worker-id)))

The above specifies a random array of bytes to be used as the worker-id.

Specification

Flakes are byte sequences composed of 128 bits. These sequences are structured such that the first 64 bits are a timestamp, i.e. the time since an epoch in milliseconds, the next 48 bits are a unique, machine-specific bitset, normally the MAC, and finally the remaining 16 bits are a monotonically increasing short.

A diagram of the flake byte structure:

[timestamp:64][MAC:48][sequence:16]

Exceptions

Exceptions may occur if a single machine generates more than 65,535 flakes within a millisecond. That puts the upper-bound on flake generation per machine at ~65 million flakes per second. If this limit is reached a IllegalArgumentException will be raised.

Additionally flake makes an effort to detect drift in system time and will raise IllegalStateException if time appears to be flowing in the wrong direction. Note that init! must be used for this to work properly!

More Repositories

1

flask-login

Flask user session management.
Python
3,569
star
2

axum-login

🪪 User identification, authentication, and authorization for Axum.
Rust
550
star
3

flask-bcrypt

Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.
Python
324
star
4

tower-sessions

🥠 Sessions as a `tower` and `axum` middleware.
Rust
214
star
5

flask-uploads

File uploads for Flask.
Python
208
star
6

flask-seasurf

SeaSurf is a Flask extension for preventing cross-site request forgery (CSRF).
Python
190
star
7

logmon

Realtime log reader in Flask
Python
176
star
8

atomos

Atomic primitives for Python.
Python
119
star
9

warc-parquet

🗄️ A simple CLI for converting WARC to Parquet.
Rust
103
star
10

axum-sessions

🥠 Cookie-based sessions for Axum via async-session.
Rust
74
star
11

aquamarine

A demo of zero-downtime deploys with Docker Compose and Traefik
Shell
53
star
12

irctk

A simple framework for writing IRC applications
Python
44
star
13

quanta

Distributed CRDT of sparse integer vectors.
Clojure
33
star
14

forma

🐚 An opinionated SQL formatter.
Rust
27
star
15

axum-messages

🛎️ One-time notification messages for Axum.
Rust
26
star
16

tower-sessions-stores

🚃 Previously bundled session stores for `tower-sessions`.
Rust
23
star
17

hyperlight

A performance-focused HTTP reverse proxy
Clojure
19
star
18

flask-themes

Flask Themes
Python
19
star
19

cryptotrade

A simple Python API wrapper for Bitcoin trading platforms such as MtGox and TradeHill
Python
14
star
20

flog

A blog written with Flask
Python
9
star
21

flask-wepay

A Flask wrapper for WePay's Python API
Python
8
star
22

blizzard

HTTP unique ID generation service
Clojure
8
star
23

st

Fast and simple statistics on the command line.
Rust
6
star
24

markov-domains

Finds available domains using Markov chains.
Clojure
6
star
25

nautilus

User authentication and management service
Clojure
5
star
26

yelp-api

A wrapper for Yelp's public API
PHP
4
star
27

affinis

An IRC library for Clojure.
Clojure
4
star
28

wtforms

Python
4
star
29

rauth

A Python library for OAuth 1.0/a, 2.0, and Ofly
Python
4
star
30

simpleirc

An IRC connection layer written in Python.
Python
4
star
31

headers-accept

🤝 The missing `Accept` implementation for `headers::Header`.
Rust
4
star
32

pyxine-branch

Branch of the Python extension for xine
Python
3
star
33

fluyt

ClojureScript HTTP requests
Clojure
3
star
34

cozy

A modern Node API template for the weary traveller
JavaScript
3
star
35

dotfiles

Development environment configuration files.
Shell
3
star
36

ewt

EDN Web Tokens
Clojure
3
star
37

flask-simpleoauth

A dead simple OAuth 1.0a provider in Flask
Python
3
star
38

simpleoauth

Simple, correct OAuth 1.0 and 2.0 signing methods.
Python
2
star
39

kaa

Kaa is the resident IRC bot on VoxInfinitus, written with IrcTK
Python
2
star
40

voxinfinitus

Basic Django apps providing CMS and blog functionality for Voxi
Python
2
star
41

mage

A Clojure-like Lisp.
Python
2
star
42

tasker

simple task manager
Python
2
star
43

ChatOnMacWebAPI-Swift

Swift
2
star
44

chatter

Chatter is a quick and dirty realtime chat application written in Flask
Python
2
star
45

bitpit-https-bridge

A simple Flask app to bridge the unsecured service with a secured page
Python
1
star
46

conceptis.org

My personal site and blog
Python
1
star
47

primes

A simple Clojure program for generating a multiplication table of primes
Clojure
1
star
48

konvej

Httpbin in Clojure.
Clojure
1
star
49

atrium

HTTP Authentication Service
1
star
50

clasp

A dead simple routing DSL for Clojure's ring.
Clojure
1
star
51

celeb

Incomplete Flask gallery project, now abandoned
Python
1
star
52

accord

A simple OAuth 1.0/a, 2.0 consumer client for Clojure.
Clojure
1
star
53

locksmithing

Lock-free, concurrent data structure experiments.
Clojure
1
star