diff --git a/Dockerfile b/Dockerfile index 8b9843a..bdef71e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,17 @@ -FROM golang:1.14-alpine as dev +FROM golang:alpine as dev WORKDIR /go/src/snowflake COPY ./go.mod . COPY ./go.sum . RUN go mod download +RUN apk update +RUN apk add protobuf +RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26 +RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1 COPY . . +RUN protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative snowflake/snowflake.proto RUN go install github.com/dustinpianalto/snowflake/... CMD [ "go", "run", "cmd/snowflake/main.go" ] @@ -17,4 +22,4 @@ WORKDIR /bin COPY --from=dev /go/bin/snowflake ./snowflake -CMD [ "snowflake" ] +CMD [ "/bin/snowflake" ] diff --git a/cmd/snowflake/main.go b/cmd/snowflake/main.go index 1cbda2f..38c2ec0 100644 --- a/cmd/snowflake/main.go +++ b/cmd/snowflake/main.go @@ -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) - } + generator.CreateGenerator(1) - requestChan := make(chan chan uint64) - outputChan := make(chan uint64) + go generator.Generator.Run() - go func() { - for id := range outputChan { - fmt.Println(id) - } - }() - go gen.Run(requestChan) - - 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() } diff --git a/deployment.yml b/deployment.yml index 9d31313..bada3e0 100644 --- a/deployment.yml +++ b/deployment.yml @@ -1,3 +1,26 @@ +kind: Service +apiVersion: v1 +metadata: + name: snowflake-server + namespace: dca + labels: + environment: production +spec: + type: "LoadBalancer" + ports: + - name: "snowflake-grpc" + port: 50051 + targetPort: 50051 + - name: "snowflake-rest" + port: 50052 + targetPort: 50052 + selector: + app: snowflake-server +status: + loadBalancer: {} + +--- + apiVersion: v1 apiVersion: batch/v1beta1 kind: CronJob @@ -16,6 +39,9 @@ spec: containers: - name: snowflake-server image: + ports: + - containerPort: 50051 + - containerPort: 50052 env: - name: WORKER_ID valueFrom: diff --git a/go.mod b/go.mod index a5279d2..722987d 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index d718f26..3296a1d 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ClickHouse/clickhouse-go v1.3.12/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI= github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/arrow v0.0.0-20200601151325-b2287a20f230/go.mod h1:QNYViu/X0HXDHw7m3KXzWSVXIbfUvJqBFe6Gj8/pYA0= github.com/aws/aws-sdk-go v1.17.7/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= @@ -54,6 +55,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/cockroachdb/cockroach-go v0.0.0-20190925194419-606b3d062051/go.mod h1:XGLbWH/ujMcbPbhZq52Nv6UrCghb1yGn//133kEsvDk= github.com/containerd/containerd v1.4.0/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= @@ -77,9 +80,12 @@ github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsouza/fake-gcs-server v1.17.0/go.mod h1:D1rTE4YCyHFNa99oyJJ5HyclvN/0uQR+pM/VdlL83bw= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -119,6 +125,8 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= @@ -131,6 +139,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -153,6 +162,7 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -234,6 +244,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= @@ -270,6 +281,7 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= @@ -347,6 +359,8 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201029221708-28c70e62bb1d/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -392,6 +406,7 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -403,6 +418,7 @@ golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201029080932-201ba4db2418/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -510,7 +526,9 @@ google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfG google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= @@ -533,6 +551,9 @@ google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI= +google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -543,6 +564,9 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= @@ -551,6 +575,7 @@ gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:a gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/generator/generator.go b/internal/generator/generator.go index fd84067..af5a7a2 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -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 { diff --git a/internal/grpc_server/grpc_server.go b/internal/grpc_server/grpc_server.go new file mode 100644 index 0000000..953979d --- /dev/null +++ b/internal/grpc_server/grpc_server.go @@ -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) + } + +} diff --git a/internal/rest_server/rest_server.go b/internal/rest_server/rest_server.go new file mode 100644 index 0000000..e4f5601 --- /dev/null +++ b/internal/rest_server/rest_server.go @@ -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)) +} diff --git a/proto/snowflake.pb.go b/proto/snowflake.pb.go deleted file mode 100644 index 7f71d9f..0000000 --- a/proto/snowflake.pb.go +++ /dev/null @@ -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 -} diff --git a/proto/snowflake_grpc.pb.go b/proto/snowflake_grpc.pb.go deleted file mode 100644 index 8f8fb20..0000000 --- a/proto/snowflake_grpc.pb.go +++ /dev/null @@ -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", -} diff --git a/proto/snowflake.proto b/snowflake/snowflake.proto similarity index 100% rename from proto/snowflake.proto rename to snowflake/snowflake.proto