[#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" accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph"
) )
type cfgAccounting struct {
scriptHash string
fee util.Fixed8
}
func initAccountingService(c *cfg) { func initAccountingService(c *cfg) {
if c.morphClient == nil { if c.cfgMorph.client == nil {
initMorphComponents(c) initMorphComponents(c)
} }
u160, err := util.Uint160DecodeStringLE(c.cfgAccounting.scriptHash) u160, err := util.Uint160DecodeStringLE(c.cfgAccounting.scriptHash)
fatalOnErr(err) 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) fatalOnErr(err)
balanceClient, err := balance.New(staticClient) balanceClient, err := balance.New(staticClient)
@ -37,7 +31,7 @@ func initAccountingService(c *cfg) {
xHdr.SetValue("test X-Header value") xHdr.SetValue("test X-Header value")
metaHdr.SetXHeaders([]*session.XHeader{xHdr}) metaHdr.SetXHeaders([]*session.XHeader{xHdr})
accountingGRPC.RegisterAccountingServiceServer(c.grpcSrv, accountingGRPC.RegisterAccountingServiceServer(c.cfgGRPC.server,
accountingTransportGRPC.New( accountingTransportGRPC.New(
accountingService.NewSignService( accountingService.NewSignService(
c.key, c.key,

View file

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"net"
"sync" "sync"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -16,17 +17,33 @@ type cfg struct {
wg *sync.WaitGroup wg *sync.WaitGroup
grpcAddr string
key *ecdsa.PrivateKey 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 { func defaultCfg() *cfg {
@ -34,12 +51,16 @@ func defaultCfg() *cfg {
fatalOnErr(err) fatalOnErr(err)
return &cfg{ return &cfg{
ctx: context.Background(), ctx: context.Background(),
wg: new(sync.WaitGroup), wg: new(sync.WaitGroup),
grpcAddr: "127.0.0.1:50501", key: key,
key: key, cfgGRPC: cfgGRPC{
morphEndpoint: "http://morph_chain.localtest.nspcc.ru:30333/", endpoint: "127.0.0.1:50501",
cfgAccounting: &cfgAccounting{ },
cfgMorph: cfgMorph{
endpoint: "http://morph_chain.localtest.nspcc.ru:30333/",
},
cfgAccounting: cfgAccounting{
scriptHash: "1aeefe1d0dfade49740fff779c02cd4a0538ffb1", scriptHash: "1aeefe1d0dfade49740fff779c02cd4a0538ffb1",
fee: util.Fixed8(1), fee: util.Fixed8(1),
}, },

View file

@ -5,22 +5,13 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container" 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" 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" "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" "github.com/pkg/errors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
type accountingSvcExec struct{}
type sessionSvc struct{} type sessionSvc struct{}
type containerSvc 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) 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) { func (s *sessionSvc) Create(context.Context, *session.CreateRequest) (*session.CreateResponse, error) {
return nil, unimplementedErr("Session", "Create") return nil, unimplementedErr("Session", "Create")
} }
@ -91,39 +78,24 @@ func (s *objectSvc) GetRangeHash(context.Context, *objectGRPC.GetRangeHashReques
return nil, unimplementedErr("Object", "GetRangeHash") return nil, unimplementedErr("Object", "GetRangeHash")
} }
func serveGRPC(c *cfg) { func initGRPC(c *cfg) {
lis, err := net.Listen("tcp", c.grpcAddr) var err error
c.cfgGRPC.listener, err = net.Listen("tcp", c.cfgGRPC.endpoint)
fatalOnErr(err) fatalOnErr(err)
c.grpcSrv = grpc.NewServer() c.cfgGRPC.server = 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)))
func serveGRPC(c *cfg) {
go func() { go func() {
c.wg.Add(1) c.wg.Add(1)
defer func() { defer func() {
c.wg.Done() 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) 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 package main
import ( import (
"fmt"
"log" "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" "github.com/nspcc-dev/neofs-node/pkg/util/grace"
) )
@ -15,11 +22,38 @@ func fatalOnErr(err error) {
func main() { func main() {
c := defaultCfg() c := defaultCfg()
init_(c)
bootUp(c)
wait(c)
shutdown(c)
}
func init_(c *cfg) {
c.ctx = grace.NewGracefulContext(nil) 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() <-c.ctx.Done()
}
func shutdown(c *cfg) {
c.cfgGRPC.server.GracefulStop()
fmt.Println("gRPC server stopped")
c.wg.Wait() c.wg.Wait()
} }

View file

@ -7,7 +7,7 @@ import (
func initMorphComponents(c *cfg) { func initMorphComponents(c *cfg) {
var err error 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) fatalOnErr(err)
} }