Initial Golang version
This commit is contained in:
@@ -5,20 +5,23 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var Generator *generator
|
||||
|
||||
var EPOCH_TIME time.Time = time.Unix(0, 1609459200000*int64(time.Millisecond))
|
||||
var TIME_MASK uint64 = 0x1FFFFFFFFFF
|
||||
|
||||
var tooManyRequests = errors.New("Too many requests in the current ms")
|
||||
var incorrectSystemTime = errors.New("The current time is less than the last generated time! Check the system time.")
|
||||
|
||||
type Generator struct {
|
||||
type generator struct {
|
||||
LastGeneratedTime time.Duration
|
||||
Counter uint64
|
||||
LastCounterRollover time.Duration
|
||||
WorkerID uint64
|
||||
RequestChan chan chan uint64
|
||||
}
|
||||
|
||||
func (g *Generator) GenerateSnowflake() (uint64, error) {
|
||||
func (g *generator) GenerateSnowflake() (uint64, error) {
|
||||
time := time.Since(EPOCH_TIME)
|
||||
if time < g.LastGeneratedTime {
|
||||
return 0, incorrectSystemTime
|
||||
@@ -36,20 +39,18 @@ func (g *Generator) GenerateSnowflake() (uint64, error) {
|
||||
return (((uint64(time.Milliseconds()) & TIME_MASK) << 22) + g.WorkerID + g.Counter), nil
|
||||
}
|
||||
|
||||
func CreateGenerator(workerID uint64) (*Generator, error) {
|
||||
if workerID <= 0 {
|
||||
return nil, errors.New("Worker ID must be greater than 0")
|
||||
}
|
||||
return &Generator{
|
||||
func CreateGenerator(workerID uint64) {
|
||||
Generator = &generator{
|
||||
LastGeneratedTime: time.Since(EPOCH_TIME),
|
||||
Counter: 0,
|
||||
LastCounterRollover: time.Since(EPOCH_TIME),
|
||||
WorkerID: workerID,
|
||||
}, nil
|
||||
RequestChan: make(chan chan uint64),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) Run(request chan chan uint64) {
|
||||
for output := range request {
|
||||
func (g *generator) Run() {
|
||||
for output := range g.RequestChan {
|
||||
id, err := g.GenerateSnowflake()
|
||||
if err != nil {
|
||||
if err == tooManyRequests {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package grpc_server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/dustinpianalto/snowflake/internal/generator"
|
||||
"github.com/dustinpianalto/snowflake/snowflake"
|
||||
)
|
||||
|
||||
const (
|
||||
GRPC_PORT = ":50051"
|
||||
)
|
||||
|
||||
type SnowflakeServer struct {
|
||||
snowflake.UnimplementedSnowflakeServer
|
||||
}
|
||||
|
||||
func (s *SnowflakeServer) GetSnowflake(ctx context.Context, in *snowflake.Empty) (*snowflake.SnowflakeReply, error) {
|
||||
var id uint64
|
||||
outputChan := make(chan uint64, 1)
|
||||
defer close(outputChan)
|
||||
generator.Generator.RequestChan <- outputChan
|
||||
id = <-outputChan
|
||||
return &snowflake.SnowflakeReply{Id: id, IdStr: strconv.FormatUint(id, 10)}, nil
|
||||
}
|
||||
|
||||
func RunGRPCServer() {
|
||||
grpcListener, err := net.Listen("tcp", GRPC_PORT)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to listen: %v", err)
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
snowflake.RegisterSnowflakeServer(grpcServer, &SnowflakeServer{})
|
||||
log.Printf("GRPC Server Listening on %v", grpcListener.Addr())
|
||||
if err := grpcServer.Serve(grpcListener); err != nil {
|
||||
log.Fatalf("Failed to serve: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package rest_server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/dustinpianalto/snowflake/internal/generator"
|
||||
)
|
||||
|
||||
const (
|
||||
REST_PORT = ":50052"
|
||||
)
|
||||
|
||||
type snowflake struct {
|
||||
Id uint64 `json:"id"`
|
||||
IdStr string `json:"id_str"`
|
||||
}
|
||||
|
||||
func getSnowflake(w http.ResponseWriter, r *http.Request) {
|
||||
outputChan := make(chan uint64, 1)
|
||||
defer close(outputChan)
|
||||
|
||||
generator.Generator.RequestChan <- outputChan
|
||||
id := <-outputChan
|
||||
s := snowflake{
|
||||
Id: id,
|
||||
IdStr: strconv.FormatUint(id, 10),
|
||||
}
|
||||
json.NewEncoder(w).Encode(s)
|
||||
}
|
||||
|
||||
func RunRESTServer() {
|
||||
http.HandleFunc("/snowflake", getSnowflake)
|
||||
|
||||
log.Printf("REST Server Listening on 0.0.0.0%s", REST_PORT)
|
||||
log.Fatal(http.ListenAndServe(REST_PORT, nil))
|
||||
}
|
||||
Reference in New Issue
Block a user