parent
04c4ef935f
commit
bff0af76c3
@ -1,29 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/dustinpianalto/snowflake/internal/generator"
|
||||
"github.com/dustinpianalto/snowflake/internal/grpc_server"
|
||||
"github.com/dustinpianalto/snowflake/internal/rest_server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
gen, err := generator.CreateGenerator(1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
requestChan := make(chan chan uint64)
|
||||
outputChan := make(chan uint64)
|
||||
generator.CreateGenerator(1)
|
||||
|
||||
go func() {
|
||||
for id := range outputChan {
|
||||
fmt.Println(id)
|
||||
}
|
||||
}()
|
||||
go gen.Run(requestChan)
|
||||
go generator.Generator.Run()
|
||||
|
||||
for i := 0; i < 500000; i++ {
|
||||
requestChan <- outputChan
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go grpc_server.RunGRPCServer()
|
||||
go rest_server.RunRESTServer()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
module github.com/dustinpianalto/snowflake
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
google.golang.org/grpc v1.39.0
|
||||
google.golang.org/protobuf v1.27.1
|
||||
)
|
||||
|
||||
@ -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))
|
||||
}
|
||||
@ -1,211 +0,0 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.25.0-devel
|
||||
// protoc v3.16.0
|
||||
// source: proto/snowflake.proto
|
||||
|
||||
package snowflake
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type Empty struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *Empty) Reset() {
|
||||
*x = Empty{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_snowflake_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Empty) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Empty) ProtoMessage() {}
|
||||
|
||||
func (x *Empty) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_snowflake_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Empty.ProtoReflect.Descriptor instead.
|
||||
func (*Empty) Descriptor() ([]byte, []int) {
|
||||
return file_proto_snowflake_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type SnowflakeReply struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
IdStr string `protobuf:"bytes,1,opt,name=id_str,json=idStr,proto3" json:"id_str,omitempty"`
|
||||
Id uint64 `protobuf:"varint,2,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SnowflakeReply) Reset() {
|
||||
*x = SnowflakeReply{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_snowflake_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SnowflakeReply) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SnowflakeReply) ProtoMessage() {}
|
||||
|
||||
func (x *SnowflakeReply) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_snowflake_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SnowflakeReply.ProtoReflect.Descriptor instead.
|
||||
func (*SnowflakeReply) Descriptor() ([]byte, []int) {
|
||||
return file_proto_snowflake_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *SnowflakeReply) GetIdStr() string {
|
||||
if x != nil {
|
||||
return x.IdStr
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SnowflakeReply) GetId() uint64 {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_proto_snowflake_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_proto_snowflake_proto_rawDesc = []byte{
|
||||
0x0a, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b,
|
||||
0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x73, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61,
|
||||
0x6b, 0x65, 0x22, 0x07, 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x37, 0x0a, 0x0e, 0x53,
|
||||
0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x15, 0x0a,
|
||||
0x06, 0x69, 0x64, 0x5f, 0x73, 0x74, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x69,
|
||||
0x64, 0x53, 0x74, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04,
|
||||
0x52, 0x02, 0x69, 0x64, 0x32, 0x48, 0x0a, 0x09, 0x53, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b,
|
||||
0x65, 0x12, 0x3b, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x53, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b,
|
||||
0x65, 0x12, 0x10, 0x2e, 0x73, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b, 0x65, 0x2e, 0x45, 0x6d,
|
||||
0x70, 0x74, 0x79, 0x1a, 0x19, 0x2e, 0x73, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b, 0x65, 0x2e,
|
||||
0x53, 0x6e, 0x6f, 0x77, 0x66, 0x6c, 0x61, 0x6b, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x25,
|
||||
0x5a, 0x23, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x75, 0x73,
|
||||
0x74, 0x69, 0x6e, 0x70, 0x69, 0x61, 0x6e, 0x61, 0x6c, 0x74, 0x6f, 0x2f, 0x73, 0x6e, 0x6f, 0x77,
|
||||
0x66, 0x6c, 0x61, 0x6b, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_proto_snowflake_proto_rawDescOnce sync.Once
|
||||
file_proto_snowflake_proto_rawDescData = file_proto_snowflake_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_proto_snowflake_proto_rawDescGZIP() []byte {
|
||||
file_proto_snowflake_proto_rawDescOnce.Do(func() {
|
||||
file_proto_snowflake_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_snowflake_proto_rawDescData)
|
||||
})
|
||||
return file_proto_snowflake_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_proto_snowflake_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_proto_snowflake_proto_goTypes = []interface{}{
|
||||
(*Empty)(nil), // 0: snowflake.Empty
|
||||
(*SnowflakeReply)(nil), // 1: snowflake.SnowflakeReply
|
||||
}
|
||||
var file_proto_snowflake_proto_depIdxs = []int32{
|
||||
0, // 0: snowflake.Snowflake.GetSnowflake:input_type -> snowflake.Empty
|
||||
1, // 1: snowflake.Snowflake.GetSnowflake:output_type -> snowflake.SnowflakeReply
|
||||
1, // [1:2] is the sub-list for method output_type
|
||||
0, // [0:1] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_proto_snowflake_proto_init() }
|
||||
func file_proto_snowflake_proto_init() {
|
||||
if File_proto_snowflake_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_proto_snowflake_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Empty); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_snowflake_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SnowflakeReply); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_proto_snowflake_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_proto_snowflake_proto_goTypes,
|
||||
DependencyIndexes: file_proto_snowflake_proto_depIdxs,
|
||||
MessageInfos: file_proto_snowflake_proto_msgTypes,
|
||||
}.Build()
|
||||
File_proto_snowflake_proto = out.File
|
||||
file_proto_snowflake_proto_rawDesc = nil
|
||||
file_proto_snowflake_proto_goTypes = nil
|
||||
file_proto_snowflake_proto_depIdxs = nil
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
|
||||
package snowflake
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// SnowflakeClient is the client API for Snowflake 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.
|
||||
type SnowflakeClient interface {
|
||||
GetSnowflake(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*SnowflakeReply, error)
|
||||
}
|
||||
|
||||
type snowflakeClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewSnowflakeClient(cc grpc.ClientConnInterface) SnowflakeClient {
|
||||
return &snowflakeClient{cc}
|
||||
}
|
||||
|
||||
func (c *snowflakeClient) GetSnowflake(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*SnowflakeReply, error) {
|
||||
out := new(SnowflakeReply)
|
||||
err := c.cc.Invoke(ctx, "/snowflake.Snowflake/GetSnowflake", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// SnowflakeServer is the server API for Snowflake service.
|
||||
// All implementations must embed UnimplementedSnowflakeServer
|
||||
// for forward compatibility
|
||||
type SnowflakeServer interface {
|
||||
GetSnowflake(context.Context, *Empty) (*SnowflakeReply, error)
|
||||
mustEmbedUnimplementedSnowflakeServer()
|
||||
}
|
||||
|
||||
// UnimplementedSnowflakeServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedSnowflakeServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedSnowflakeServer) GetSnowflake(context.Context, *Empty) (*SnowflakeReply, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetSnowflake not implemented")
|
||||
}
|
||||
func (UnimplementedSnowflakeServer) mustEmbedUnimplementedSnowflakeServer() {}
|
||||
|
||||
// UnsafeSnowflakeServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to SnowflakeServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeSnowflakeServer interface {
|
||||
mustEmbedUnimplementedSnowflakeServer()
|
||||
}
|
||||
|
||||
func RegisterSnowflakeServer(s grpc.ServiceRegistrar, srv SnowflakeServer) {
|
||||
s.RegisterService(&_Snowflake_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Snowflake_GetSnowflake_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(Empty)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SnowflakeServer).GetSnowflake(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/snowflake.Snowflake/GetSnowflake",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SnowflakeServer).GetSnowflake(ctx, req.(*Empty))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Snowflake_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "snowflake.Snowflake",
|
||||
HandlerType: (*SnowflakeServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "GetSnowflake",
|
||||
Handler: _Snowflake_GetSnowflake_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "proto/snowflake.proto",
|
||||
}
|
||||
Loading…
Reference in new issue