forked from course/hse-2026
Add week 2 materials
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# Dockerfile was generated from
|
||||
# https://github.com/lodthe/dockerfiles/blob/main/go/Dockerfile
|
||||
|
||||
FROM golang:1.21-alpine3.19 AS builder
|
||||
|
||||
# Setup base software for building an app.
|
||||
RUN apk update && apk add ca-certificates git gcc g++ libc-dev binutils
|
||||
|
||||
WORKDIR /opt
|
||||
|
||||
# Download dependencies.
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download && go mod verify
|
||||
|
||||
# Copy application source.
|
||||
COPY . .
|
||||
|
||||
# Build the application.
|
||||
RUN go build -o bin/application .
|
||||
|
||||
# Prepare executor image.
|
||||
FROM alpine:3.19 AS runner
|
||||
|
||||
RUN apk update && apk add ca-certificates libc6-compat openssh bash && rm -rf /var/cache/apk/*
|
||||
|
||||
WORKDIR /opt
|
||||
|
||||
COPY --from=builder /opt/bin/application ./
|
||||
|
||||
# Add required static files.
|
||||
#COPY assets assets
|
||||
|
||||
# Run the application.
|
||||
CMD ["./application"]
|
||||
@@ -0,0 +1,45 @@
|
||||
# gRPC сервер
|
||||
|
||||
Пример gRPC сервера, реализующего 2 rpc: положить и получить значение.
|
||||
|
||||
В env переменной `SERVER_ADDR` передается адрес и порт, которые будет "слушать" сервер.
|
||||
Если переменная не задана, используется параметр по умолчанию — `0.0.0.0:51000`.
|
||||
|
||||
## protobuf
|
||||
|
||||
protobuf спецификация хранится в [storage.proto](./proto/storage.proto).
|
||||
Для перегенерации *pb.go-файлов, необходимо в корне проекта вызвать protoc:
|
||||
```bash
|
||||
protoc --go_out=. --go_opt=paths=source_relative \
|
||||
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
||||
proto/storage.proto
|
||||
```
|
||||
|
||||
Подробнее о gRPC можно узнать в [документации](https://grpc.io/docs/languages/go/basics/) (там же есть блок про stream'ы). С более сложным примером proto-файла можно ознакомиться [здесь](https://github.com/paralin/raft-grpc/blob/master/raft-grpc.proto).
|
||||
|
||||
## Тестирование
|
||||
|
||||
Сервер можно запустить с помощью команды `go run main.go`.
|
||||
|
||||
Подключиться к серверу можно с помощью [grpcurl](https://github.com/fullstorydev/grpcurl):
|
||||
```bash
|
||||
# Установить значение 100500
|
||||
grpcurl -d '{"value": {"payload": 100500}}' -plaintext localhost:51000 storage.Storage/PutValue
|
||||
|
||||
# Получить актуальное значение
|
||||
grpcurl -d '{}' -plaintext localhost:51000 storage.Storage/GetValue
|
||||
```
|
||||
## Docker
|
||||
|
||||
```bash
|
||||
# Собрать Docker образ с сервером
|
||||
docker build -t hse-grpc-server .
|
||||
|
||||
# Запустить Docker контейнер:
|
||||
# -d - запуск в фоне
|
||||
# -p 50000:51000 - проброс портов: по порту 50000 с хоста будет доступен порт 51000 из контейнера
|
||||
docker run -d -p 50000:51000 hse-grpc-server
|
||||
|
||||
docker ps
|
||||
docker stop CONTAINER_ID
|
||||
```
|
||||
@@ -0,0 +1,16 @@
|
||||
module hsegrpc
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
google.golang.org/grpc v1.58.1
|
||||
google.golang.org/protobuf v1.31.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
golang.org/x/net v0.12.0 // indirect
|
||||
golang.org/x/sys v0.10.0 // indirect
|
||||
golang.org/x/text v0.11.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
|
||||
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
|
||||
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
|
||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
|
||||
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM=
|
||||
google.golang.org/grpc v1.58.1 h1:OL+Vz23DTtrrldqHK49FUOPHyY75rvFqJfXC84NYW58=
|
||||
google.golang.org/grpc v1.58.1/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
@@ -0,0 +1,44 @@
|
||||
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/storage.proto
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
storagepb "hsegrpc/proto"
|
||||
"hsegrpc/storage"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Reading server address from env.
|
||||
addr := os.Getenv("SERVER_ADDR")
|
||||
if addr == "" {
|
||||
addr = "0.0.0.0:51000"
|
||||
|
||||
fmt.Println("Missing SERVER_ADDR, using default value: " + addr)
|
||||
}
|
||||
|
||||
// Creating a TCP socket.
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
// If you want to connect to the server via grpcurl, you have to register the reflection service.
|
||||
reflection.Register(grpcServer)
|
||||
|
||||
// Creating and registering implementation of the storage service.
|
||||
storageService := storage.NewServer()
|
||||
storagepb.RegisterStorageServer(grpcServer, storageService)
|
||||
|
||||
// Starting the server.
|
||||
err = grpcServer.Serve(lis)
|
||||
if err != nil {
|
||||
log.Fatalf("server failed")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,416 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.26.0
|
||||
// protoc v4.25.2
|
||||
// source: proto/storage.proto
|
||||
|
||||
package storagepb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
|
||||
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 Value struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Payload uint64 `protobuf:"varint,1,opt,name=payload,proto3" json:"payload,omitempty"`
|
||||
UpdatedAt *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=updated_at,json=updatedAt,proto3,oneof" json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Value) Reset() {
|
||||
*x = Value{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_storage_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Value) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Value) ProtoMessage() {}
|
||||
|
||||
func (x *Value) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_storage_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 Value.ProtoReflect.Descriptor instead.
|
||||
func (*Value) Descriptor() ([]byte, []int) {
|
||||
return file_proto_storage_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Value) GetPayload() uint64 {
|
||||
if x != nil {
|
||||
return x.Payload
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Value) GetUpdatedAt() *timestamppb.Timestamp {
|
||||
if x != nil {
|
||||
return x.UpdatedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PutRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Value *Value `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PutRequest) Reset() {
|
||||
*x = PutRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_storage_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PutRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PutRequest) ProtoMessage() {}
|
||||
|
||||
func (x *PutRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_storage_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 PutRequest.ProtoReflect.Descriptor instead.
|
||||
func (*PutRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_storage_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *PutRequest) GetValue() *Value {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PutResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Value uint64 `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PutResponse) Reset() {
|
||||
*x = PutResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_storage_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PutResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PutResponse) ProtoMessage() {}
|
||||
|
||||
func (x *PutResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_storage_proto_msgTypes[2]
|
||||
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 PutResponse.ProtoReflect.Descriptor instead.
|
||||
func (*PutResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_storage_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *PutResponse) GetValue() uint64 {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type GetRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *GetRequest) Reset() {
|
||||
*x = GetRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_storage_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_storage_proto_msgTypes[3]
|
||||
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 GetRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_storage_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
type GetResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Value *Value `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetResponse) Reset() {
|
||||
*x = GetResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_storage_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_storage_proto_msgTypes[4]
|
||||
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 GetResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_storage_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *GetResponse) GetValue() *Value {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_proto_storage_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_proto_storage_proto_rawDesc = []byte{
|
||||
0x0a, 0x13, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x1a, 0x1f,
|
||||
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f,
|
||||
0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
|
||||
0x70, 0x0a, 0x05, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x79, 0x6c,
|
||||
0x6f, 0x61, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x07, 0x70, 0x61, 0x79, 0x6c, 0x6f,
|
||||
0x61, 0x64, 0x12, 0x3e, 0x0a, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74,
|
||||
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61,
|
||||
0x6d, 0x70, 0x48, 0x00, 0x52, 0x09, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x88,
|
||||
0x01, 0x01, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61,
|
||||
0x74, 0x22, 0x32, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
|
||||
0x24, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e,
|
||||
0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x05,
|
||||
0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x23, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70,
|
||||
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x04, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x0c, 0x0a, 0x0a, 0x47, 0x65,
|
||||
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x33, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52,
|
||||
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
|
||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65,
|
||||
0x2e, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32, 0x77, 0x0a,
|
||||
0x07, 0x53, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x12, 0x35, 0x0a, 0x08, 0x50, 0x75, 0x74, 0x56,
|
||||
0x61, 0x6c, 0x75, 0x65, 0x12, 0x13, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x50,
|
||||
0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x73, 0x74, 0x6f, 0x72,
|
||||
0x61, 0x67, 0x65, 0x2e, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
|
||||
0x35, 0x0a, 0x08, 0x47, 0x65, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x13, 0x2e, 0x73, 0x74,
|
||||
0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
|
||||
0x1a, 0x14, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65,
|
||||
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x14, 0x5a, 0x12, 0x68, 0x73, 0x65, 0x67, 0x72, 0x70,
|
||||
0x63, 0x2f, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_proto_storage_proto_rawDescOnce sync.Once
|
||||
file_proto_storage_proto_rawDescData = file_proto_storage_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_proto_storage_proto_rawDescGZIP() []byte {
|
||||
file_proto_storage_proto_rawDescOnce.Do(func() {
|
||||
file_proto_storage_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_storage_proto_rawDescData)
|
||||
})
|
||||
return file_proto_storage_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_proto_storage_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_proto_storage_proto_goTypes = []interface{}{
|
||||
(*Value)(nil), // 0: storage.Value
|
||||
(*PutRequest)(nil), // 1: storage.PutRequest
|
||||
(*PutResponse)(nil), // 2: storage.PutResponse
|
||||
(*GetRequest)(nil), // 3: storage.GetRequest
|
||||
(*GetResponse)(nil), // 4: storage.GetResponse
|
||||
(*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp
|
||||
}
|
||||
var file_proto_storage_proto_depIdxs = []int32{
|
||||
5, // 0: storage.Value.updated_at:type_name -> google.protobuf.Timestamp
|
||||
0, // 1: storage.PutRequest.value:type_name -> storage.Value
|
||||
0, // 2: storage.GetResponse.value:type_name -> storage.Value
|
||||
1, // 3: storage.Storage.PutValue:input_type -> storage.PutRequest
|
||||
3, // 4: storage.Storage.GetValue:input_type -> storage.GetRequest
|
||||
2, // 5: storage.Storage.PutValue:output_type -> storage.PutResponse
|
||||
4, // 6: storage.Storage.GetValue:output_type -> storage.GetResponse
|
||||
5, // [5:7] is the sub-list for method output_type
|
||||
3, // [3:5] is the sub-list for method input_type
|
||||
3, // [3:3] is the sub-list for extension type_name
|
||||
3, // [3:3] is the sub-list for extension extendee
|
||||
0, // [0:3] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_proto_storage_proto_init() }
|
||||
func file_proto_storage_proto_init() {
|
||||
if File_proto_storage_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_proto_storage_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Value); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_storage_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PutRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_storage_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PutResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_storage_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_storage_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
file_proto_storage_proto_msgTypes[0].OneofWrappers = []interface{}{}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_proto_storage_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_proto_storage_proto_goTypes,
|
||||
DependencyIndexes: file_proto_storage_proto_depIdxs,
|
||||
MessageInfos: file_proto_storage_proto_msgTypes,
|
||||
}.Build()
|
||||
File_proto_storage_proto = out.File
|
||||
file_proto_storage_proto_rawDesc = nil
|
||||
file_proto_storage_proto_goTypes = nil
|
||||
file_proto_storage_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package storage;
|
||||
|
||||
option go_package = "hsegrpc/;storagepb";
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
service Storage {
|
||||
rpc PutValue(PutRequest) returns (PutResponse);
|
||||
rpc GetValue(GetRequest) returns (GetResponse);
|
||||
}
|
||||
|
||||
message Value {
|
||||
uint64 payload = 1;
|
||||
optional google.protobuf.Timestamp updated_at = 2;
|
||||
}
|
||||
|
||||
message PutRequest {
|
||||
Value value = 1;
|
||||
}
|
||||
|
||||
message PutResponse {
|
||||
uint64 value = 1;
|
||||
}
|
||||
|
||||
message GetRequest {
|
||||
}
|
||||
|
||||
message GetResponse {
|
||||
Value value = 1;
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v4.25.2
|
||||
// source: proto/storage.proto
|
||||
|
||||
package storagepb
|
||||
|
||||
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.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// StorageClient is the client API for Storage 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 StorageClient interface {
|
||||
PutValue(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error)
|
||||
GetValue(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error)
|
||||
}
|
||||
|
||||
type storageClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewStorageClient(cc grpc.ClientConnInterface) StorageClient {
|
||||
return &storageClient{cc}
|
||||
}
|
||||
|
||||
func (c *storageClient) PutValue(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) {
|
||||
out := new(PutResponse)
|
||||
err := c.cc.Invoke(ctx, "/storage.Storage/PutValue", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *storageClient) GetValue(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) {
|
||||
out := new(GetResponse)
|
||||
err := c.cc.Invoke(ctx, "/storage.Storage/GetValue", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// StorageServer is the server API for Storage service.
|
||||
// All implementations must embed UnimplementedStorageServer
|
||||
// for forward compatibility
|
||||
type StorageServer interface {
|
||||
PutValue(context.Context, *PutRequest) (*PutResponse, error)
|
||||
GetValue(context.Context, *GetRequest) (*GetResponse, error)
|
||||
mustEmbedUnimplementedStorageServer()
|
||||
}
|
||||
|
||||
// UnimplementedStorageServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedStorageServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedStorageServer) PutValue(context.Context, *PutRequest) (*PutResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PutValue not implemented")
|
||||
}
|
||||
func (UnimplementedStorageServer) GetValue(context.Context, *GetRequest) (*GetResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetValue not implemented")
|
||||
}
|
||||
func (UnimplementedStorageServer) mustEmbedUnimplementedStorageServer() {}
|
||||
|
||||
// UnsafeStorageServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to StorageServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeStorageServer interface {
|
||||
mustEmbedUnimplementedStorageServer()
|
||||
}
|
||||
|
||||
func RegisterStorageServer(s grpc.ServiceRegistrar, srv StorageServer) {
|
||||
s.RegisterService(&Storage_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Storage_PutValue_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PutRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StorageServer).PutValue(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/storage.Storage/PutValue",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StorageServer).PutValue(ctx, req.(*PutRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Storage_GetValue_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StorageServer).GetValue(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/storage.Storage/GetValue",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StorageServer).GetValue(ctx, req.(*GetRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Storage_ServiceDesc is the grpc.ServiceDesc for Storage service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Storage_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "storage.Storage",
|
||||
HandlerType: (*StorageServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "PutValue",
|
||||
Handler: _Storage_PutValue_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetValue",
|
||||
Handler: _Storage_GetValue_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "proto/storage.proto",
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
storagepb "hsegrpc/proto"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type Value struct {
|
||||
payload uint64
|
||||
updatedAt time.Time
|
||||
}
|
||||
|
||||
// toProto transforms struct into protobuf wrapper.
|
||||
func (v *Value) toProto() *storagepb.Value {
|
||||
return &storagepb.Value{
|
||||
Payload: v.payload,
|
||||
UpdatedAt: timestamppb.New(v.updatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
// Server must implement the Storage protobuf interface.
|
||||
storagepb.UnimplementedStorageServer
|
||||
|
||||
// We use mutex to synchronize access to the value.
|
||||
valueLocker sync.RWMutex
|
||||
value Value
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{}
|
||||
}
|
||||
|
||||
func (s *Server) PutValue(_ context.Context, request *storagepb.PutRequest) (*storagepb.PutResponse, error) {
|
||||
s.valueLocker.Lock()
|
||||
defer s.valueLocker.Unlock()
|
||||
|
||||
// Check if the given value not empty.
|
||||
if request.GetValue() == nil {
|
||||
return nil, errors.New("missed value")
|
||||
}
|
||||
|
||||
s.value = Value{
|
||||
payload: request.GetValue().GetPayload(),
|
||||
updatedAt: time.Now(),
|
||||
}
|
||||
|
||||
return &storagepb.PutResponse{
|
||||
Value: s.value.payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) GetValue(_ context.Context, _ *storagepb.GetRequest) (*storagepb.GetResponse, error) {
|
||||
s.valueLocker.RLock()
|
||||
defer s.valueLocker.RUnlock()
|
||||
|
||||
return &storagepb.GetResponse{
|
||||
Value: s.value.toProto(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user