add comments, code refactor
This commit is contained in:
@@ -17,16 +17,20 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/utils"
|
"github.com/mayswind/lab/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const REQUEST_ID_LENGTH = 36
|
// Length and mask of all information in request id
|
||||||
const SECONDS_TODAY_BITS = 17
|
const (
|
||||||
const SECONDS_TODAY_BITS_MASK = (1 << SECONDS_TODAY_BITS) - 1
|
REQUEST_ID_LENGTH = 36
|
||||||
const RANDOM_NUMBER_BITS = 15
|
SECONDS_TODAY_BITS = 17
|
||||||
const RANDOM_NUMBER_BITS_MASK = (1 << RANDOM_NUMBER_BITS) - 1
|
SECONDS_TODAY_BITS_MASK = (1 << SECONDS_TODAY_BITS) - 1
|
||||||
const REQ_SEQ_NUMBER_BITS = 31
|
RANDOM_NUMBER_BITS = 15
|
||||||
const REQ_SEQ_NUMBER_BITS_MASK = (1 << REQ_SEQ_NUMBER_BITS) - 1
|
RANDOM_NUMBER_BITS_MASK = (1 << RANDOM_NUMBER_BITS) - 1
|
||||||
const CLIENT_IPV6_BIT = 1
|
REQ_SEQ_NUMBER_BITS = 31
|
||||||
const CLIENT_IPV6_BIT_MASK = 1
|
REQ_SEQ_NUMBER_BITS_MASK = (1 << REQ_SEQ_NUMBER_BITS) - 1
|
||||||
|
CLIENT_IPV6_BIT = 1
|
||||||
|
CLIENT_IPV6_BIT_MASK = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
// RequestIdInfo represents a struct which has all information in request id
|
||||||
type RequestIdInfo struct {
|
type RequestIdInfo struct {
|
||||||
ServerUniqId uint16
|
ServerUniqId uint16
|
||||||
InstanceUniqId uint16
|
InstanceUniqId uint16
|
||||||
@@ -37,12 +41,14 @@ type RequestIdInfo struct {
|
|||||||
ClientIp uint32
|
ClientIp uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultRequestIdGenerator represents default request id generator
|
||||||
type DefaultRequestIdGenerator struct {
|
type DefaultRequestIdGenerator struct {
|
||||||
serverUniqId uint16
|
serverUniqId uint16
|
||||||
instanceUniqId uint16
|
instanceUniqId uint16
|
||||||
requestSeqId uint32
|
requestSeqId uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewDefaultRequestIdGenerator returns a new default request id generator
|
||||||
func NewDefaultRequestIdGenerator(config *settings.Config) (*DefaultRequestIdGenerator, error) {
|
func NewDefaultRequestIdGenerator(config *settings.Config) (*DefaultRequestIdGenerator, error) {
|
||||||
serverUniqId, err := getServerUniqId(config)
|
serverUniqId, err := getServerUniqId(config)
|
||||||
|
|
||||||
@@ -94,6 +100,7 @@ func getInstanceUniqId(config *settings.Config) uint16 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseRequestIdInfo returns a info struct which contains all information in request id
|
||||||
func (r *DefaultRequestIdGenerator) ParseRequestIdInfo(requestId string) (*RequestIdInfo, error) {
|
func (r *DefaultRequestIdGenerator) ParseRequestIdInfo(requestId string) (*RequestIdInfo, error) {
|
||||||
if requestId == "" || len(requestId) != REQUEST_ID_LENGTH {
|
if requestId == "" || len(requestId) != REQUEST_ID_LENGTH {
|
||||||
return nil, errs.ErrRequestIdInvalid
|
return nil, errs.ErrRequestIdInvalid
|
||||||
@@ -103,14 +110,17 @@ func (r *DefaultRequestIdGenerator) ParseRequestIdInfo(requestId string) (*Reque
|
|||||||
return r.parseRequestIdInfo(requestIdData), nil
|
return r.parseRequestIdInfo(requestIdData), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCurrentServerUniqId returns current server unique id
|
||||||
func (r *DefaultRequestIdGenerator) GetCurrentServerUniqId() uint16 {
|
func (r *DefaultRequestIdGenerator) GetCurrentServerUniqId() uint16 {
|
||||||
return r.serverUniqId
|
return r.serverUniqId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCurrentInstanceUniqId returns current application instance unique id
|
||||||
func (r *DefaultRequestIdGenerator) GetCurrentInstanceUniqId() uint16 {
|
func (r *DefaultRequestIdGenerator) GetCurrentInstanceUniqId() uint16 {
|
||||||
return r.instanceUniqId
|
return r.instanceUniqId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateRequestId returns a new request id
|
||||||
func (r *DefaultRequestIdGenerator) GenerateRequestId(clientIpAddr string) string {
|
func (r *DefaultRequestIdGenerator) GenerateRequestId(clientIpAddr string) string {
|
||||||
ip := net.ParseIP(clientIpAddr)
|
ip := net.ParseIP(clientIpAddr)
|
||||||
isClientIpv6 := ip.To4() == nil
|
isClientIpv6 := ip.To4() == nil
|
||||||
|
|||||||
@@ -4,14 +4,17 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/settings"
|
"github.com/mayswind/lab/pkg/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RequestIdContainer contains the current request id generator
|
||||||
type RequestIdContainer struct {
|
type RequestIdContainer struct {
|
||||||
Current RequestIdGenerator
|
Current RequestIdGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a request id container singleton instance
|
||||||
var (
|
var (
|
||||||
Container = &RequestIdContainer{}
|
Container = &RequestIdContainer{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InitializeRequestIdGenerator initialized the current request id generator according to the config
|
||||||
func InitializeRequestIdGenerator(config *settings.Config) error {
|
func InitializeRequestIdGenerator(config *settings.Config) error {
|
||||||
generator, err := NewDefaultRequestIdGenerator(config)
|
generator, err := NewDefaultRequestIdGenerator(config)
|
||||||
|
|
||||||
@@ -23,6 +26,7 @@ func InitializeRequestIdGenerator(config *settings.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateRequestId returns a new request id by the current request id generator
|
||||||
func (u *RequestIdContainer) GenerateRequestId(clientIpAddr string) string {
|
func (u *RequestIdContainer) GenerateRequestId(clientIpAddr string) string {
|
||||||
return u.Current.GenerateRequestId(clientIpAddr)
|
return u.Current.GenerateRequestId(clientIpAddr)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package requestid
|
package requestid
|
||||||
|
|
||||||
|
// RequestIdGenerator is common request generator interface
|
||||||
type RequestIdGenerator interface {
|
type RequestIdGenerator interface {
|
||||||
GenerateRequestId(clientIpAddr string) string
|
GenerateRequestId(clientIpAddr string) string
|
||||||
GetCurrentServerUniqId() uint16
|
GetCurrentServerUniqId() uint16
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/settings"
|
"github.com/mayswind/lab/pkg/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Length and mask of all information in uuid
|
||||||
const (
|
const (
|
||||||
INTERNAL_UUID_UNIX_TIME_BITS = 32
|
INTERNAL_UUID_UNIX_TIME_BITS = 32
|
||||||
INTERNAL_UUID_UNIX_TIME_MASK = (1 << INTERNAL_UUID_UNIX_TIME_BITS) - 1
|
INTERNAL_UUID_UNIX_TIME_MASK = (1 << INTERNAL_UUID_UNIX_TIME_BITS) - 1
|
||||||
@@ -24,6 +25,7 @@ const (
|
|||||||
SEQ_NUMBER_ID_MASK = (1 << SEQ_NUMBER_ID_BITS) - 1
|
SEQ_NUMBER_ID_MASK = (1 << SEQ_NUMBER_ID_BITS) - 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InternalUuidInfo represents a struct which has all information in uuid
|
||||||
type InternalUuidInfo struct {
|
type InternalUuidInfo struct {
|
||||||
UnixTime uint32
|
UnixTime uint32
|
||||||
UuidType uint8
|
UuidType uint8
|
||||||
@@ -31,11 +33,13 @@ type InternalUuidInfo struct {
|
|||||||
SequentialId uint32
|
SequentialId uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InternalUuidGenerator represents internal bundled uuid generator
|
||||||
type InternalUuidGenerator struct {
|
type InternalUuidGenerator struct {
|
||||||
uuidServerId uint8
|
uuidServerId uint8
|
||||||
uuidSeqNumbers [1 << INTERNAL_UUID_TYPE_BITS]uint64
|
uuidSeqNumbers [1 << INTERNAL_UUID_TYPE_BITS]uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewInternalUuidGenerator returns a new internal uuid generator
|
||||||
func NewInternalUuidGenerator(config *settings.Config) (*InternalUuidGenerator, error) {
|
func NewInternalUuidGenerator(config *settings.Config) (*InternalUuidGenerator, error) {
|
||||||
generator := &InternalUuidGenerator{
|
generator := &InternalUuidGenerator{
|
||||||
uuidServerId: config.UuidServerId,
|
uuidServerId: config.UuidServerId,
|
||||||
@@ -44,6 +48,7 @@ func NewInternalUuidGenerator(config *settings.Config) (*InternalUuidGenerator,
|
|||||||
return generator, nil
|
return generator, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateUuid returns a new uuid
|
||||||
func (u *InternalUuidGenerator) GenerateUuid(idType UuidType) int64 {
|
func (u *InternalUuidGenerator) GenerateUuid(idType UuidType) int64 {
|
||||||
// 63bits = unixTime(32bits) + uuidType(4bits) + uuidServerId(8bits) + sequentialNumber(19bits)
|
// 63bits = unixTime(32bits) + uuidType(4bits) + uuidServerId(8bits) + sequentialNumber(19bits)
|
||||||
|
|
||||||
@@ -79,6 +84,7 @@ func (u *InternalUuidGenerator) GenerateUuid(idType UuidType) int64 {
|
|||||||
return uuid
|
return uuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseUuidInfo returns a info struct which contains all information in uuid
|
||||||
func (u *InternalUuidGenerator) ParseUuidInfo(uuid int64) *InternalUuidInfo {
|
func (u *InternalUuidGenerator) ParseUuidInfo(uuid int64) *InternalUuidInfo {
|
||||||
seqId := uint32(uuid & INTERNAL_UUID_SEQ_ID_MASK)
|
seqId := uint32(uuid & INTERNAL_UUID_SEQ_ID_MASK)
|
||||||
uuid = uuid >> INTERNAL_UUID_SEQ_ID_BITS
|
uuid = uuid >> INTERNAL_UUID_SEQ_ID_BITS
|
||||||
|
|||||||
@@ -5,14 +5,17 @@ import (
|
|||||||
"github.com/mayswind/lab/pkg/settings"
|
"github.com/mayswind/lab/pkg/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UuidContainer contains the current uuid generator
|
||||||
type UuidContainer struct {
|
type UuidContainer struct {
|
||||||
Current UuidGenerator
|
Current UuidGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize a uuid container singleton instance
|
||||||
var (
|
var (
|
||||||
Container = &UuidContainer{}
|
Container = &UuidContainer{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InitializeUuidGenerator initialized the current uuid generator according to the config
|
||||||
func InitializeUuidGenerator(config *settings.Config) error {
|
func InitializeUuidGenerator(config *settings.Config) error {
|
||||||
if config.UuidGeneratorType == settings.UUID_GENERATOR_TYPE_INTERNAL {
|
if config.UuidGeneratorType == settings.UUID_GENERATOR_TYPE_INTERNAL {
|
||||||
generator, err := NewInternalUuidGenerator(config)
|
generator, err := NewInternalUuidGenerator(config)
|
||||||
@@ -24,6 +27,7 @@ func InitializeUuidGenerator(config *settings.Config) error {
|
|||||||
return errs.ErrInvalidUuidMode
|
return errs.ErrInvalidUuidMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateUuid returns a new uuid by the current uuid generator
|
||||||
func (u *UuidContainer) GenerateUuid(uuidType UuidType) int64 {
|
func (u *UuidContainer) GenerateUuid(uuidType UuidType) int64 {
|
||||||
return u.Current.GenerateUuid(uuidType)
|
return u.Current.GenerateUuid(uuidType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package uuid
|
package uuid
|
||||||
|
|
||||||
|
// UuidGenerator is common uuid generator interface
|
||||||
type UuidGenerator interface {
|
type UuidGenerator interface {
|
||||||
GenerateUuid(uuidType UuidType) int64
|
GenerateUuid(uuidType UuidType) int64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package uuid
|
|||||||
|
|
||||||
type UuidType uint8
|
type UuidType uint8
|
||||||
|
|
||||||
|
// Types of uuid
|
||||||
const (
|
const (
|
||||||
UUID_TYPE_DEFAULT UuidType = 0
|
UUID_TYPE_DEFAULT UuidType = 0
|
||||||
UUID_TYPE_USER UuidType = 1
|
UUID_TYPE_USER UuidType = 1
|
||||||
|
|||||||
Reference in New Issue
Block a user