did

package module
v0.0.0-...-8b885ba Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: MIT Imports: 6 Imported by: 0

README

分布式ID生成器服务

基于twitter snowflake的算法实现的ID生成器高性能的健壮的可容错的网络服务。

它提供了:

  • 可配置的节点bit数和最大序列数
  • 可以批量获取ID
  • 基于rpcx提供网络服务,可以提供一组服务节点
  • 基于rpcx,可以提供分布式的、容错的网络服务

snowflake算法的实现基于bwmarrin/snowflake, 额外提供了批量获取ID的方法。

例子

server

server 提供了一个简单的服务节点,你可以提供多个服务节点以便提供高可用性。

你还可以配置插件,使用ZooKeeper、Etcd、Consul、Nacos等作为注册中心,配置插件进行trace监控,设置黑白名单等操作,这些微服务的特性通过rpcx来实现。

client实现远程调用ID生成器的服务,这个例子采用服务地址硬编码的方式实现。如果你的服务使用了etcd、nacos等服务中心,你可以配置client使用注册中心自动获取服务节点。

客户端的例子演示了获取单个ID和获取批量ID的方法。

memcached 协议访问

除了使用 rpcx访问外,你还可以通过memcached协议获取id。

  • get id\r\n: 获取一个id
  • gets 100\r\n: 一次获取100个id

性能

单个节点每秒可以产生400万左右的ID。

BenchmarkGenerate-4                 	 4918761	       244 ns/op	   4097826 ids/s	     115 B/op	       1 allocs/op
BenchmarkGenerateBatch-4            	   49309	     24472 ns/op	   4086058 ids/s	   10811 B/op	     104 allocs/op
BenchmarkGenerate_Parallel-4        	 4903029	       244 ns/op	   4093939 ids/s	      88 B/op	       0 allocs/op
BenchmarkGenerateBatch_Parallel-4   	   49328	     24402 ns/op	   4097864 ids/s	   12904 B/op	     126 allocs/op
使用网络服务获取ID

1、256个client并发,每次只获取1个ID, ID的产生速度是 12万个ID/秒

./bclient -addr 10.41.15.225:8972 -n 100000
total IDs: 25600000, duration: 3m31.581592489s, id/s: 120993

2、如果采用批量获取,尽量减少网络消耗,256个client并发,每次只获取100个ID, ID的产生速度是 297万个ID/秒

./bclient -addr 10.41.15.225:8972 -n 1000000 -b 100
total IDs: 256000000, duration: 1m26.178942509s, id/s: 2970563

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

This section is empty.

Types

type ID

type ID int64

An ID is a custom type used for a generated ID. This is used so we can attach methods onto the ID.

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase32

func ParseBase32(b []byte) (ID, error)

ParseBase32 parses a base32 []byte into a snowflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (ID, error)

ParseBase36 converts a Base36 string into a snowflake ID

func ParseBase58

func ParseBase58(b []byte) (ID, error)

ParseBase58 parses a base58 []byte into a snowflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

func ParseBytes(id []byte) (ID, error)

ParseBytes converts a byte slice into a snowflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (ID) Base2

func (f ID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (ID) Base32

func (f ID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (ID) Base36

func (f ID) Base36() string

Base36 returns a base36 string of the snowflake ID

func (ID) Base58

func (f ID) Base58() string

Base58 returns a base58 string of the snowflake ID

func (ID) Base64

func (f ID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (ID) Bytes

func (f ID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake ID, encoded as a big endian integer.

func (ID) String

func (f ID) String() string

String returns a string of the snowflake ID

type IDGen

type IDGen interface {
	GetServerID(ctx context.Context, dummy string, serverID *string) error
	Get(ctx context.Context, dummy int8, id *int64) error
	GetBatch(ctx context.Context, count uint16, ids *[]int64) error
}

IDGen is the interface of ID generator. You can get one ID by `Get` or get some IDs by GetBatch().

type JSONSyntaxError

type JSONSyntaxError struct {
	// contains filtered or unexported fields
}

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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