suid

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

go-suid

Documentation

Index

Constants

View Source
const (
	MAX_HOST  int64 = 0x7f        // 7 bits
	MAX_SEQ   int64 = 0x7ffff     // 19 bits
	MAX_TIME  int64 = 0x3ffffffff // 34 bits
	MAX_GROUP int64 = 0x7         // 3 bits
)

Variables

This section is empty.

Functions

func HostID added in v1.3.0

func HostID() int64

Get current Host ID.

Types

type SUID

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

Snowflake Unique Identifier SUID is a 64-bit integer. The data structure is symbol(1)-group(3)-time(34)-seq(19)-host(7)

  • The host_id will read `SUID_HOST_ID` `POD_NAME` `HOSTNAME` from environment, and pick last part ( separator "-" )
  • If run in kubenetes, recommend to use StatefulSet to provide a unique host_id.
  • Otherwise, you must set `SUID_HOST_ID` manually to provide a unique host_id.
  • The max date for SUID will be `2514-05-30 01:53:03 +0000`
  • The max number of concurrent transactions is 524288 per second. It will wait for next second automatically
  • Recommend to use SUID as primary key for database table to ensure the uniqueness and performance.
  • The SUID will keep threaded-safe and safe for concurrent access.

func FromHex added in v1.2.0

func FromHex(str string) (SUID, error)

FromHex creates a SUID from a hex string.

func FromInt added in v1.2.0

func FromInt(value int64) SUID

FromInt creates a SUID from an int64 value.

func FromStr added in v1.2.0

func FromStr(str string, base ...int) (SUID, error)

FromStr creates a SUID from a string with the given base. If base is not given, it will use base 10.

func New

func New(group ...int64) SUID

New a SUID with the given group. If group is not given, it will use the default group 0. The max group value is 7

func (SUID) Description

func (s SUID) Description() string

Get the description of the SUID.

func (SUID) GormDataType added in v1.1.0

func (s SUID) GormDataType() string

GormDataType implements the gorm.DataTypeInterface interface.

func (SUID) Group

func (s SUID) Group() int64

Group returns the group of the SUID.

func (SUID) Hex added in v1.3.0

func (s SUID) Hex() string

Hex returns the hex string representation of the SUID.

func (SUID) Host

func (s SUID) Host() int64

Host returns the host ID of the SUID.

func (SUID) Int added in v1.1.0

func (s SUID) Int() int64

Int returns the int64 value of the SUID.

func (SUID) MarshalJSON

func (s SUID) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*SUID) Scan added in v1.1.0

func (s *SUID) Scan(value any) error

Scan implements the sql.Scanner interface.

func (SUID) Seq

func (s SUID) Seq() int64

Seq returns the sequence number of the SUID.

func (SUID) Str added in v1.4.0

func (s SUID) Str(base ...int) string

Str returns the string representation of the SUID with the given base. If base is not given, it will use base 10.

func (SUID) Time

func (s SUID) Time() int64

Time returns the timestamp of the SUID.

func (*SUID) UnmarshalJSON

func (s *SUID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (SUID) Value

func (s SUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

func (SUID) Verify

func (s SUID) Verify() bool

Verify the SUID is valid or not.

Jump to

Keyboard shortcuts

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