sanic

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2019 License: ISC Imports: 6 Imported by: 0

README

sanic

GoDoc

sanic is a clone of Twitter snowflake (the 2010 version), written in Golang. More specifically, the IdWorker section of snowflake.

Usage

To use sanic, either make a new worker, or select a premade one:

package main

import (
	"fmt"

	"github.com/ifo/sanic"
)

func main() {
	worker := sanic.NewWorker7()
	// equivalent to:
	// worker := sanic.NewWorker(0, 1451606400, 0, 10, 31, time.Second)

	id := worker.NextID()
	idString := worker.IDString(id)
	fmt.Println(id)       // e.g. 5292179457
	fmt.Println(idString) // e.g. "AUBwOwE"
}

Check out the examples for more.

Future Improvements

Many things are missing from sanic, so here's a TODO list of some of those things:

  • Tests: sanic has no tests. This is probably the next thing I should do.
  • More Random Looking IDs: Generated IDs don't change that much. Some creative bit manipulation could likely change this while still ensuring that IDs are unique (so long as you use the same bit manipulation each time).

License

sanic is ISC licensed. Check out the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntToBytes

func IntToBytes(i int64) ([]byte, error)

func IntToString

func IntToString(i int64, totalBits uint64) (string, error)

func RemoveSixTrailingZeroBits

func RemoveSixTrailingZeroBits(s string, totalBits uint64) string

func RemoveUnusedBytes

func RemoveUnusedBytes(bts []byte, totalBits uint64) []byte

Types

type Worker

type Worker struct {
	ID             int64 // 0 - 2 ^ IDBits
	IDBits         uint64
	IDShift        uint64
	Sequence       int64 // 0 - 2 ^ SequenceBits
	SequenceBits   uint64
	LastTimeStamp  int64
	TimeStampBits  uint64
	TimeStampShift uint64
	Frequency      time.Duration
	TotalBits      uint64
	CustomEpoch    int64
	// contains filtered or unexported fields
}

func NewWorker

func NewWorker(
	id, epoch int64, idBits, sequenceBits, timestampBits uint64,
	frequency time.Duration) *Worker

func NewWorker10

func NewWorker10(id int64) *Worker

NewWorker10 will generate up to 4096000 unique ids/second for 69 years NewWorker10 will return nil if the ID is greater than 63 or less than 0

func NewWorker7

func NewWorker7() *Worker

NewWorker7 will generate up to 1024 unique ids/second for 68 years NewWorker7 is the only worker of its size with this configuration

func NewWorker8

func NewWorker8() *Worker

NewWorker8 will generate up to 81920 unique ids/second for 54 years NewWorker8 is the only worker of it's size with this configuration

func NewWorker9

func NewWorker9(id int64) *Worker

NewWorker9 will generate up to 819200 unique ids/second for 87 years NewWorker9 will return nil if the ID is greater than 3 or less than 0

func (*Worker) IDString

func (w *Worker) IDString(id int64) string

func (*Worker) NextID

func (w *Worker) NextID() int64

func (*Worker) Time

func (w *Worker) Time() int64

func (*Worker) UnsafeNextID

func (w *Worker) UnsafeNextID() int64

UnsafeNextID is faster than NextID, but must be called within only one goroutine, otherwise ID uniqueness is not guaranteed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL