[#11] neofs-node: Improve sources

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-08-24 12:40:32 +03:00 committed by Alex Vanin
parent 21c750016b
commit 3308fcf56d
5 changed files with 80 additions and 59 deletions

View file

@ -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,

View file

@ -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 {
@ -36,10 +53,14 @@ func defaultCfg() *cfg {
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{
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),
},

View file

@ -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()
}()
}

View file

@ -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()
}

View file

@ -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)
}