wuid

package
v0.0.0-...-cd457a8 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Example
newDB := func() (*sql.DB, bool, error) {
	var db *sql.DB
	// ...
	return db, true, nil
}

// Setup
g := NewWUID("default", nil)
_ = g.LoadH28FromMysql(newDB, "wuid")

// Generate
for i := 0; i < 10; i++ {
	fmt.Printf("%#016x\n", g.Next())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NewDB

type NewDB func() (client *sql.DB, autoDisconnect bool, err error)

type Option

type Option = internal.Option

func WithH28Verifier

func WithH28Verifier(cb func(h28 int64) error) Option

WithH28Verifier sets your own h28 verifier

func WithSection

func WithSection(section int8) Option

WithSection adds a section ID to the generated numbers. The section ID must be in between [1, 7].

func WithStep

func WithStep(step int64) Option

WithStep sets the step of Next()

type WUID

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

WUID is an extremely fast unique number generator.

func NewWUID

func NewWUID(tag string, logger slog.Logger, opts ...Option) *WUID

NewWUID creates a new WUID instance.

func (*WUID) LoadH28FromMysql

func (this *WUID) LoadH28FromMysql(newDB NewDB, table string) error

LoadH28FromMysql adds 1 to a specific number in your MySQL, fetches its new value, and then sets that as the high 28 bits of the unique numbers that Next generates.

func (*WUID) Next

func (this *WUID) Next() int64

Next returns the next unique number.

func (*WUID) RenewNow

func (this *WUID) RenewNow() error

RenewNow reacquires the high 28 bits from your data store immediately

Jump to

Keyboard shortcuts

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