GithubHelp home page GithubHelp logo

k2tzumi / chocoflake Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ada-u/chocoflake

0.0 1.0 0.0 76 KB

chocoflake is an implementation of twitter Snowflake concept.

License: MIT License

PHP 99.13% Dockerfile 0.87%

chocoflake's Introduction

chocoflake

Build Status Github Actions Coverage Status

64bit ID Generator for PHP

chocoflake is an implementation of twitter Snowflake concept. This provides generating IDs based on time in a distributed environment.

Usage

Prerequisites

  • PHP 8.0 or later

Installation

Command

$ composer.phar require ada-u/chocoflake:dev-master

composer.json

{
  "require": {
    "ada-u/chocoflake": "dev-master"
  }
}
$ composer.phar install

Sample

Configuration

  • 41 bit - for timestamp
  • 5 bit - for region id
  • 5 bit - for server id
  • 12 bit - for sequence per milliseconds
  • 1414334507356 - service start epoch (unix timestamp)
$config = new IdConfig(41, 5, 5, 12, 1414334507356);
$service = new ChocoflakeService($config);

$worker = $service->createIdWorkerOnSharedMemory(new RegionId(1), new ServerId(1));

$id = $worker->generate();
4194439168

ID Generator

I implemented two ID generators, Redis and SharedMemory version.

SharedMemory version

Using shared memory and semaphore (as mutex) to prevent multiple processes are in the critical section at the same time.

Redis version

Using Redis atomic increment operation to count up sequence.

ID Specification

The IDs consist of four elements:

  • timestamp
  • region id
  • server id
  • sequence

You can specify any bit length to each element.

Test

$ docker-compose run php vendor/bin/phpunit
Creating chocoflake_php_run ... done
PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 00:00.353, Memory: 6.00 MB

OK (4 tests, 4 assertions)

License

This software is released under the MIT License, see LICENSE

chocoflake's People

Contributors

ada-u avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.