sequence

package
v0.0.1-rc5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextSequence

func NextSequence() string

NextSequence 获取下一个序列值(使用全局序列)

Types

type Sequence

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

Sequence a sequence generator. A quick note on the statistics here: we're trying to calculate the chance that two randomly generated base62 prefixes will collide. We use the formula from http://en.wikipedia.org/wiki/Birthday_problem

P[m, n] \approx 1 - e^{-m^2/2n}

We ballpark an upper bound for $m$ by imagining (for whatever reason) a server that restarts every second over 10 years, for $m = 86400 * 365 * 10 = 315360000$

For a $k$ character base-62 identifier, we have $n(k) = 62^k$

Plugging this in, we find $P[m, n(10)] \approx 5.75%$, which is good enough for our purposes, and is surely more than anyone would ever need in practice -- a process that is rebooted a handful of times a day for a hundred years has less than a millionth of a percent chance of generating two colliding IDs.

func New

func New() *Sequence

New new sequence with prefix '{hostname}-{pid}-{init-rand-value}-' and zero sequence id.

func (*Sequence) NewSequence

func (s *Sequence) NewSequence() string

NewSequence generates the next sequence id format {hostname}-{pid}-{init-rand-value}-{sequence}. A sequence id is a string where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.

Jump to

Keyboard shortcuts

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