[#11] Use gRPC handlers in neofs-node application

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-08-22 14:03:45 +03:00 committed by Alex Vanin
parent c2954aab20
commit 65eb1181e9
3 changed files with 87 additions and 57 deletions

View file

@ -1,11 +1,22 @@
package main package main
import (
"context"
"sync"
)
type cfg struct { type cfg struct {
ctx context.Context
wg *sync.WaitGroup
grpcAddr string grpcAddr string
} }
func defaultCfg() *cfg { func defaultCfg() *cfg {
return &cfg{ return &cfg{
grpcAddr: ":50501", ctx: context.Background(),
wg: new(sync.WaitGroup),
grpcAddr: "127.0.0.1:50501",
} }
} }

View file

@ -2,112 +2,129 @@ package main
import ( import (
"context" "context"
"fmt"
"net" "net"
accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc" "github.com/nspcc-dev/neofs-api-go/v2/accounting"
accountingGRPC "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container"
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object"
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session"
session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc"
accountingTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc"
containerTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc"
objectTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc"
sessionTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc"
"github.com/pkg/errors" "github.com/pkg/errors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
type unimplementedServer struct { type accountingSvc struct{}
accServer
cnrServer
objServer
}
type accServer struct{} type sessionSvc struct{}
type sesServer struct{} type containerSvc struct{}
type cnrServer struct{} type objectSvc struct{}
type objServer struct{}
func unimplementedErr(srv, call string) error { func unimplementedErr(srv, call string) error {
return errors.Errorf("unimplemented service call %s.%s", srv, call) return errors.Errorf("unimplemented API service call %s.%s", srv, call)
} }
func (*accServer) Balance(context.Context, *accounting.BalanceRequest) (*accounting.BalanceResponse, error) { func (s *accountingSvc) Balance(context.Context, *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
return nil, unimplementedErr("Accounting", "Balance") return nil, unimplementedErr("Accounting", "Balance")
} }
func (*sesServer) Create(context.Context, *session.CreateRequest) (*session.CreateResponse, error) { func (s *sessionSvc) Create(context.Context, *sessionGRPC.CreateRequest) (*sessionGRPC.CreateResponse, error) {
return nil, unimplementedErr("Session", "Create") return nil, unimplementedErr("Session", "Create")
} }
func (*cnrServer) Put(context.Context, *container.PutRequest) (*container.PutResponse, error) { func (s *containerSvc) Put(context.Context, *containerGRPC.PutRequest) (*containerGRPC.PutResponse, error) {
return nil, unimplementedErr("Contianer", "Put") return nil, unimplementedErr("Container", "Put")
} }
func (*cnrServer) Delete(context.Context, *container.DeleteRequest) (*container.DeleteResponse, error) { func (s *containerSvc) Delete(context.Context, *containerGRPC.DeleteRequest) (*containerGRPC.DeleteResponse, error) {
return nil, unimplementedErr("Contianer", "Delete") return nil, unimplementedErr("Container", "Delete")
} }
func (*cnrServer) Get(context.Context, *container.GetRequest) (*container.GetResponse, error) { func (s *containerSvc) Get(context.Context, *containerGRPC.GetRequest) (*containerGRPC.GetResponse, error) {
return nil, unimplementedErr("Contianer", "Get") return nil, unimplementedErr("Container", "Get")
} }
func (*cnrServer) List(context.Context, *container.ListRequest) (*container.ListResponse, error) { func (s *containerSvc) List(context.Context, *containerGRPC.ListRequest) (*containerGRPC.ListResponse, error) {
return nil, unimplementedErr("Contianer", "List") return nil, unimplementedErr("Container", "List")
} }
func (*cnrServer) SetExtendedACL(context.Context, *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) { func (s *containerSvc) SetExtendedACL(context.Context, *containerGRPC.SetExtendedACLRequest) (*containerGRPC.SetExtendedACLResponse, error) {
return nil, unimplementedErr("Contianer", "SetExtendedACL") return nil, unimplementedErr("Container", "SetExtendedACL")
} }
func (*cnrServer) GetExtendedACL(context.Context, *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) { func (s *containerSvc) GetExtendedACL(context.Context, *containerGRPC.GetExtendedACLRequest) (*containerGRPC.GetExtendedACLResponse, error) {
return nil, unimplementedErr("Contianer", "GetExtendedACL") return nil, unimplementedErr("Container", "GetExtendedACL")
} }
func (*objServer) Get(*object.GetRequest, object.ObjectService_GetServer) error { func (s *objectSvc) Get(context.Context, *objectGRPC.GetRequest) (objectGRPC.GetObjectStreamer, error) {
return unimplementedErr("Object", "Get") return nil, unimplementedErr("Object", "Get")
} }
func (*objServer) Put(object.ObjectService_PutServer) error { func (s *objectSvc) Put(context.Context) (objectGRPC.PutObjectStreamer, error) {
return unimplementedErr("Object", "Put") return nil, unimplementedErr("Object", "Put")
} }
func (*objServer) Delete(context.Context, *object.DeleteRequest) (*object.DeleteResponse, error) { func (s *objectSvc) Head(context.Context, *objectGRPC.HeadRequest) (*objectGRPC.HeadResponse, error) {
return nil, unimplementedErr("Object", "Put")
}
func (s *objectSvc) Search(context.Context, *objectGRPC.SearchRequest) (objectGRPC.SearchObjectStreamer, error) {
return nil, unimplementedErr("Object", "Search")
}
func (s *objectSvc) Delete(context.Context, *objectGRPC.DeleteRequest) (*objectGRPC.DeleteResponse, error) {
return nil, unimplementedErr("Object", "Delete") return nil, unimplementedErr("Object", "Delete")
} }
func (*objServer) Head(context.Context, *object.HeadRequest) (*object.HeadResponse, error) { func (s *objectSvc) GetRange(context.Context, *objectGRPC.GetRangeRequest) (objectGRPC.GetRangeObjectStreamer, error) {
return nil, unimplementedErr("Object", "Head") return nil, unimplementedErr("Object", "GetRange")
} }
func (*objServer) Search(*object.SearchRequest, object.ObjectService_SearchServer) error { func (s *objectSvc) GetRangeHash(context.Context, *objectGRPC.GetRangeHashRequest) (*objectGRPC.GetRangeHashResponse, error) {
return unimplementedErr("Object", "Search")
}
func (*objServer) GetRange(*object.GetRangeRequest, object.ObjectService_GetRangeServer) error {
return unimplementedErr("Object", "GetRange")
}
func (*objServer) GetRangeHash(context.Context, *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
return nil, unimplementedErr("Object", "GetRangeHash") return nil, unimplementedErr("Object", "GetRangeHash")
} }
func serveGRPC(c *cfg) error { func serveGRPC(c *cfg) {
lis, err := net.Listen("tcp", c.grpcAddr) lis, err := net.Listen("tcp", c.grpcAddr)
fatalOnErr(err) fatalOnErr(err)
srv := grpc.NewServer() srv := grpc.NewServer()
s := new(unimplementedServer) accountingGRPC.RegisterAccountingServiceServer(srv, accountingTransport.New(new(accountingSvc)))
container.RegisterContainerServiceServer(srv, containerTransport.New(new(containerSvc)))
session.RegisterSessionServiceServer(srv, sessionTransport.New(new(sessionSvc)))
object.RegisterObjectServiceServer(srv, objectTransport.New(new(objectSvc)))
accounting.RegisterAccountingServiceServer(srv, s) go func() {
container.RegisterContainerServiceServer(srv, s) c.wg.Add(1)
session.RegisterSessionServiceServer(srv, s) defer func() {
object.RegisterObjectServiceServer(srv, s) c.wg.Done()
}()
if err := srv.Serve(lis); err != nil { if err := srv.Serve(lis); err != nil {
return err fmt.Println("gRPC server error", err)
} }
}()
lis.Close() go func() {
c.wg.Add(1)
defer func() {
fmt.Println("gRPC server stopped gracefully")
fmt.Println("net listener stopped", lis.Addr())
c.wg.Done()
}()
return nil <-c.ctx.Done()
srv.GracefulStop()
}()
} }

View file

@ -15,9 +15,11 @@ func fatalOnErr(err error) {
func main() { func main() {
c := defaultCfg() c := defaultCfg()
fatalOnErr(serveGRPC(c)) c.ctx = grace.NewGracefulContext(nil)
ctx := grace.NewGracefulContext(nil) serveGRPC(c)
<-ctx.Done() <-c.ctx.Done()
c.wg.Wait()
} }