share

package
v0.0.0-...-8736fb8 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

使用一个简单实现的 Bloom Filter 实现了分享ID 生成器 ID 生成的过程:

1.随机生成 3 个 byte
2.将随 bytes 进行 hash 产生 Bloom Filter 所需要的 hash 数目
3.由 Bloom Filter 进行过滤,如不存在则插入过滤器,否则返回 ErrKeyExist
4.将随机 bytes 转换成字符串 ID 返回

注意:

因为采取了随机字符串,当 ID 分配数接近 1000W 时性能会开始剧烈恶化。
可以通过修改代码提高元素空间数解决。

性能:

因为预计使用量不大,所以没有做太仔细的优化。简单的测试结果如下

单线程连续请求:
	产生约 1400W ID 99% 的请求延迟低于 1ms。
使用 wrk2 进行 5 线程, 10 连接, 1000 QPS 测试:
	99% 请求延迟低于 100ms,99.9% 请求延时低于 380ms。
使用 wrk2 进行 6 线程, 40 连接, 80 QPS 测试:
	99% 请求延迟低于 6ms, 99.9% 请求延时低于 9ms。

以上测试均在 2014 款 MacBook Pro 15 上进行。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyExist     = errors.New("this key exist")
	ErrTooManyRetry = errors.New("too many retry")
	ErrBadValue     = errors.New(`id or num cannot be "" or 0`)
)

Errors

Functions

func IDtoI

func IDtoI(id string) (uint64, error)

将 ID 转化成 uint64

func ItoID

func ItoID(u uint64) (string, error)

将一个 uint64 转化成 ID

func NewID

func NewID() (string, error)

产生随机 ID

Types

This section is empty.

Jump to

Keyboard shortcuts

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