From 3308fcf56d12a5e3d213407af2b2b7f0c034c9df Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 24 Aug 2020 12:40:32 +0300 Subject: [PATCH] [#11] neofs-node: Improve sources Signed-off-by: Leonard Lyubich --- cmd/neofs-node/accounting.go | 12 +++------- cmd/neofs-node/config.go | 45 ++++++++++++++++++++++++++---------- cmd/neofs-node/grpc.go | 44 +++++++---------------------------- cmd/neofs-node/main.go | 36 ++++++++++++++++++++++++++++- cmd/neofs-node/morph.go | 2 +- 5 files changed, 80 insertions(+), 59 deletions(-) diff --git a/cmd/neofs-node/accounting.go b/cmd/neofs-node/accounting.go index 6e576f453..6b8a3700d 100644 --- a/cmd/neofs-node/accounting.go +++ b/cmd/neofs-node/accounting.go @@ -11,21 +11,15 @@ import ( accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph" ) -type cfgAccounting struct { - scriptHash string - - fee util.Fixed8 -} - func initAccountingService(c *cfg) { - if c.morphClient == nil { + if c.cfgMorph.client == nil { initMorphComponents(c) } u160, err := util.Uint160DecodeStringLE(c.cfgAccounting.scriptHash) fatalOnErr(err) - staticClient, err := client.NewStatic(c.morphClient, u160, c.cfgAccounting.fee) + staticClient, err := client.NewStatic(c.cfgMorph.client, u160, c.cfgAccounting.fee) fatalOnErr(err) balanceClient, err := balance.New(staticClient) @@ -37,7 +31,7 @@ func initAccountingService(c *cfg) { xHdr.SetValue("test X-Header value") metaHdr.SetXHeaders([]*session.XHeader{xHdr}) - accountingGRPC.RegisterAccountingServiceServer(c.grpcSrv, + accountingGRPC.RegisterAccountingServiceServer(c.cfgGRPC.server, accountingTransportGRPC.New( accountingService.NewSignService( c.key, diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 9eac49be3..04049cc7f 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -3,6 +3,7 @@ package main import ( "context" "crypto/ecdsa" + "net" "sync" "github.com/nspcc-dev/neo-go/pkg/util" @@ -16,17 +17,33 @@ type cfg struct { wg *sync.WaitGroup - grpcAddr string - key *ecdsa.PrivateKey - grpcSrv *grpc.Server + cfgGRPC cfgGRPC - morphEndpoint string + cfgMorph cfgMorph - morphClient *client.Client + cfgAccounting cfgAccounting +} - cfgAccounting *cfgAccounting +type cfgGRPC struct { + endpoint string + + listener net.Listener + + server *grpc.Server +} + +type cfgMorph struct { + endpoint string + + client *client.Client +} + +type cfgAccounting struct { + scriptHash string + + fee util.Fixed8 } func defaultCfg() *cfg { @@ -34,12 +51,16 @@ func defaultCfg() *cfg { fatalOnErr(err) return &cfg{ - ctx: context.Background(), - wg: new(sync.WaitGroup), - grpcAddr: "127.0.0.1:50501", - key: key, - morphEndpoint: "http://morph_chain.localtest.nspcc.ru:30333/", - cfgAccounting: &cfgAccounting{ + ctx: context.Background(), + wg: new(sync.WaitGroup), + key: key, + cfgGRPC: cfgGRPC{ + endpoint: "127.0.0.1:50501", + }, + cfgMorph: cfgMorph{ + endpoint: "http://morph_chain.localtest.nspcc.ru:30333/", + }, + cfgAccounting: cfgAccounting{ scriptHash: "1aeefe1d0dfade49740fff779c02cd4a0538ffb1", fee: util.Fixed8(1), }, diff --git a/cmd/neofs-node/grpc.go b/cmd/neofs-node/grpc.go index f8b68bc28..e1a9552f8 100644 --- a/cmd/neofs-node/grpc.go +++ b/cmd/neofs-node/grpc.go @@ -5,22 +5,13 @@ import ( "fmt" "net" - "github.com/nspcc-dev/neofs-api-go/v2/accounting" containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container" - 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" "github.com/nspcc-dev/neofs-api-go/v2/session" - sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session/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" "google.golang.org/grpc" ) -type accountingSvcExec struct{} - type sessionSvc struct{} type containerSvc struct{} @@ -31,10 +22,6 @@ func unimplementedErr(srv, call string) error { return errors.Errorf("unimplemented API service call %s.%s", srv, call) } -func (s *accountingSvcExec) Balance(context.Context, *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error) { - return new(accounting.BalanceResponseBody), nil -} - func (s *sessionSvc) Create(context.Context, *session.CreateRequest) (*session.CreateResponse, error) { return nil, unimplementedErr("Session", "Create") } @@ -91,39 +78,24 @@ func (s *objectSvc) GetRangeHash(context.Context, *objectGRPC.GetRangeHashReques return nil, unimplementedErr("Object", "GetRangeHash") } -func serveGRPC(c *cfg) { - lis, err := net.Listen("tcp", c.grpcAddr) +func initGRPC(c *cfg) { + var err error + + c.cfgGRPC.listener, err = net.Listen("tcp", c.cfgGRPC.endpoint) fatalOnErr(err) - c.grpcSrv = grpc.NewServer() - - initAccountingService(c) - - container.RegisterContainerServiceServer(c.grpcSrv, containerTransport.New(new(containerSvc))) - sessionGRPC.RegisterSessionServiceServer(c.grpcSrv, sessionTransport.New(new(sessionSvc))) - object.RegisterObjectServiceServer(c.grpcSrv, objectTransport.New(new(objectSvc))) + c.cfgGRPC.server = grpc.NewServer() +} +func serveGRPC(c *cfg) { go func() { c.wg.Add(1) defer func() { c.wg.Done() }() - if err := c.grpcSrv.Serve(lis); err != nil { + if err := c.cfgGRPC.server.Serve(c.cfgGRPC.listener); err != nil { fmt.Println("gRPC server error", err) } }() - - go func() { - c.wg.Add(1) - defer func() { - fmt.Println("gRPC server stopped gracefully") - fmt.Println("net listener stopped", lis.Addr()) - c.wg.Done() - }() - - <-c.ctx.Done() - - c.grpcSrv.GracefulStop() - }() } diff --git a/cmd/neofs-node/main.go b/cmd/neofs-node/main.go index 2f83e9d60..c6f58f6e2 100644 --- a/cmd/neofs-node/main.go +++ b/cmd/neofs-node/main.go @@ -1,8 +1,15 @@ package main import ( + "fmt" "log" + container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" + object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" + session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" + containerGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc" + objectGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc" + sessionGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc" "github.com/nspcc-dev/neofs-node/pkg/util/grace" ) @@ -15,11 +22,38 @@ func fatalOnErr(err error) { func main() { c := defaultCfg() + init_(c) + + bootUp(c) + + wait(c) + + shutdown(c) +} + +func init_(c *cfg) { c.ctx = grace.NewGracefulContext(nil) - serveGRPC(c) + initGRPC(c) + initAccountingService(c) + + container.RegisterContainerServiceServer(c.cfgGRPC.server, containerGRPC.New(new(containerSvc))) + session.RegisterSessionServiceServer(c.cfgGRPC.server, sessionGRPC.New(new(sessionSvc))) + object.RegisterObjectServiceServer(c.cfgGRPC.server, objectGRPC.New(new(objectSvc))) +} + +func bootUp(c *cfg) { + serveGRPC(c) +} + +func wait(c *cfg) { <-c.ctx.Done() +} + +func shutdown(c *cfg) { + c.cfgGRPC.server.GracefulStop() + fmt.Println("gRPC server stopped") c.wg.Wait() } diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index f713bd2aa..4c924c47b 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -7,7 +7,7 @@ import ( func initMorphComponents(c *cfg) { var err error - c.morphClient, err = client.New(c.key, c.morphEndpoint) + c.cfgMorph.client, err = client.New(c.key, c.cfgMorph.endpoint) fatalOnErr(err) }