Documentation
¶
Overview ¶
Package sonyflake implements Sonyflake, a distributed unique ID generator inspired by Twitter's Snowflake.
A Sonyflake ID is composed of
39 bits for time in units of 10 msec 8 bits for a sequence number 16 bits for a machine id
Index ¶
- Constants
- Variables
- func Decompose(id uint64) map[string]uint64
- func ElapsedTime(id uint64) time.Duration
- func MachineID(id uint64) uint64
- func RegisterSonyflakeServiceServer(s grpc.ServiceRegistrar, srv SonyflakeServiceServer)
- func SequenceNumber(id uint64) uint64
- type GrpcSonyflakeService
- type Settings
- type SonyFlakeRequest
- type SonyFlakeResponse
- func (*SonyFlakeResponse) Descriptor() ([]byte, []int)deprecated
- func (x *SonyFlakeResponse) GetId() uint64
- func (x *SonyFlakeResponse) GetMachineID() uint64
- func (x *SonyFlakeResponse) GetMsb() uint64
- func (x *SonyFlakeResponse) GetSequence() uint64
- func (x *SonyFlakeResponse) GetTime() uint64
- func (*SonyFlakeResponse) ProtoMessage()
- func (x *SonyFlakeResponse) ProtoReflect() protoreflect.Message
- func (x *SonyFlakeResponse) Reset()
- func (x *SonyFlakeResponse) String() string
- type Sonyflake
- type SonyflakeServiceClient
- type SonyflakeServiceServer
- type UnimplementedSonyflakeServiceServer
- type UnsafeSonyflakeServiceServer
Constants ¶
const ( BitLenTime = 39 // bit length of time BitLenSequence = 8 // bit length of sequence number BitLenMachineID = 63 - BitLenTime - BitLenSequence // bit length of machine id )
These constants are the bit lengths of Sonyflake ID parts.
const (
SonyflakeService_NextID_FullMethodName = "/sonyflake.SonyflakeService/NextID"
)
Variables ¶
var File_grpc_service_proto protoreflect.FileDescriptor
var SonyflakeService_ServiceDesc = grpc.ServiceDesc{ ServiceName: "sonyflake.SonyflakeService", HandlerType: (*SonyflakeServiceServer)(nil), Methods: []grpc.MethodDesc{ { MethodName: "NextID", Handler: _SonyflakeService_NextID_Handler, }, }, Streams: []grpc.StreamDesc{}, Metadata: "grpc_service.proto", }
SonyflakeService_ServiceDesc is the grpc.ServiceDesc for SonyflakeService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)
Functions ¶
func ElapsedTime ¶
ElapsedTime returns the elapsed time when the given Sonyflake ID was generated.
func RegisterSonyflakeServiceServer ¶
func RegisterSonyflakeServiceServer(s grpc.ServiceRegistrar, srv SonyflakeServiceServer)
func SequenceNumber ¶
SequenceNumber returns the sequence number of a Sonyflake ID.
Types ¶
type GrpcSonyflakeService ¶
type GrpcSonyflakeService struct {
UnimplementedSonyflakeServiceServer
Sf *Sonyflake
}
GrpcSonyflakeService implement SonyflakeServiceServer interface
func (*GrpcSonyflakeService) NextID ¶
func (rpcS *GrpcSonyflakeService) NextID(ctx context.Context, req *SonyFlakeRequest) (*SonyFlakeResponse, error)
NextID generate sequence
type Settings ¶
type Settings struct {
StartTime time.Time
MachineID func() (uint16, error)
CheckMachineID func(uint16) bool
}
Settings configures Sonyflake:
StartTime is the time since which the Sonyflake time is defined as the elapsed time. If StartTime is 0, the start time of the Sonyflake is set to "2014-09-01 00:00:00 +0000 UTC". If StartTime is ahead of the current time, Sonyflake is not created.
MachineID returns the unique ID of the Sonyflake instance. If MachineID returns an error, Sonyflake is not created. If MachineID is nil, default MachineID is used. Default MachineID returns the lower 16 bits of the private IP address.
CheckMachineID validates the uniqueness of the machine ID. If CheckMachineID returns false, Sonyflake is not created. If CheckMachineID is nil, no validation is done.
type SonyFlakeRequest ¶
type SonyFlakeRequest struct {
Num uint32 `protobuf:"varint,1,opt,name=num,proto3" json:"num,omitempty"`
// contains filtered or unexported fields
}
func (*SonyFlakeRequest) Descriptor
deprecated
func (*SonyFlakeRequest) Descriptor() ([]byte, []int)
Deprecated: Use SonyFlakeRequest.ProtoReflect.Descriptor instead.
func (*SonyFlakeRequest) GetNum ¶
func (x *SonyFlakeRequest) GetNum() uint32
func (*SonyFlakeRequest) ProtoMessage ¶
func (*SonyFlakeRequest) ProtoMessage()
func (*SonyFlakeRequest) ProtoReflect ¶
func (x *SonyFlakeRequest) ProtoReflect() protoreflect.Message
func (*SonyFlakeRequest) Reset ¶
func (x *SonyFlakeRequest) Reset()
func (*SonyFlakeRequest) String ¶
func (x *SonyFlakeRequest) String() string
type SonyFlakeResponse ¶
type SonyFlakeResponse struct {
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Msb uint64 `protobuf:"varint,2,opt,name=msb,proto3" json:"msb,omitempty"`
Time uint64 `protobuf:"varint,3,opt,name=time,proto3" json:"time,omitempty"`
Sequence uint64 `protobuf:"varint,4,opt,name=sequence,proto3" json:"sequence,omitempty"`
MachineID uint64 `protobuf:"varint,5,opt,name=machineID,proto3" json:"machineID,omitempty"`
// contains filtered or unexported fields
}
func (*SonyFlakeResponse) Descriptor
deprecated
func (*SonyFlakeResponse) Descriptor() ([]byte, []int)
Deprecated: Use SonyFlakeResponse.ProtoReflect.Descriptor instead.
func (*SonyFlakeResponse) GetId ¶
func (x *SonyFlakeResponse) GetId() uint64
func (*SonyFlakeResponse) GetMachineID ¶
func (x *SonyFlakeResponse) GetMachineID() uint64
func (*SonyFlakeResponse) GetMsb ¶
func (x *SonyFlakeResponse) GetMsb() uint64
func (*SonyFlakeResponse) GetSequence ¶
func (x *SonyFlakeResponse) GetSequence() uint64
func (*SonyFlakeResponse) GetTime ¶
func (x *SonyFlakeResponse) GetTime() uint64
func (*SonyFlakeResponse) ProtoMessage ¶
func (*SonyFlakeResponse) ProtoMessage()
func (*SonyFlakeResponse) ProtoReflect ¶
func (x *SonyFlakeResponse) ProtoReflect() protoreflect.Message
func (*SonyFlakeResponse) Reset ¶
func (x *SonyFlakeResponse) Reset()
func (*SonyFlakeResponse) String ¶
func (x *SonyFlakeResponse) String() string
type Sonyflake ¶
type Sonyflake struct {
// contains filtered or unexported fields
}
Sonyflake is a distributed unique ID generator.
func NewSonyflake ¶
NewSonyflake returns a new Sonyflake configured with the given Settings. NewSonyflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.CheckMachineID returns false.
type SonyflakeServiceClient ¶
type SonyflakeServiceClient interface {
NextID(ctx context.Context, in *SonyFlakeRequest, opts ...grpc.CallOption) (*SonyFlakeResponse, error)
}
SonyflakeServiceClient is the client API for SonyflakeService service.
For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
func NewSonyflakeServiceClient ¶
func NewSonyflakeServiceClient(cc grpc.ClientConnInterface) SonyflakeServiceClient
type SonyflakeServiceServer ¶
type SonyflakeServiceServer interface {
NextID(context.Context, *SonyFlakeRequest) (*SonyFlakeResponse, error)
// contains filtered or unexported methods
}
SonyflakeServiceServer is the server API for SonyflakeService service. All implementations must embed UnimplementedSonyflakeServiceServer for forward compatibility
type UnimplementedSonyflakeServiceServer ¶
type UnimplementedSonyflakeServiceServer struct {
}
UnimplementedSonyflakeServiceServer must be embedded to have forward compatible implementations.
func (UnimplementedSonyflakeServiceServer) NextID ¶
func (UnimplementedSonyflakeServiceServer) NextID(context.Context, *SonyFlakeRequest) (*SonyFlakeResponse, error)
type UnsafeSonyflakeServiceServer ¶
type UnsafeSonyflakeServiceServer interface {
// contains filtered or unexported methods
}
UnsafeSonyflakeServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to SonyflakeServiceServer will result in compilation errors.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package awsutil provides utility functions for using Sonyflake on AWS.
|
Package awsutil provides utility functions for using Sonyflake on AWS. |
|
Package mock offers implementations of interfaces defined in types.go This allows complete control over input / output for any given method that consumes a given type
|
Package mock offers implementations of interfaces defined in types.go This allows complete control over input / output for any given method that consumes a given type |
|
Package Types defines type signatures used throughout SonyFlake.
|
Package Types defines type signatures used throughout SonyFlake. |