2020-08-21 15:01:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-22 11:03:45 +00:00
|
|
|
"fmt"
|
2020-08-21 15:01:59 +00:00
|
|
|
"net"
|
|
|
|
|
2021-06-01 18:53:31 +00:00
|
|
|
grpcconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/grpc"
|
2021-01-18 08:56:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.uber.org/zap"
|
2020-08-21 15:01:59 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-05-14 09:11:49 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2020-08-21 15:01:59 +00:00
|
|
|
)
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
func initGRPC(c *cfg) {
|
2021-06-22 17:25:18 +00:00
|
|
|
grpcconfig.IterateEndpoints(c.appCfg, func(sc *grpcconfig.Config) {
|
|
|
|
lis, err := net.Listen("tcp", sc.Endpoint())
|
|
|
|
fatalOnErr(err)
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.cfgGRPC.listeners = append(c.cfgGRPC.listeners, lis)
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
serverOpts := []grpc.ServerOption{
|
|
|
|
grpc.MaxSendMsgSize(maxMsgSize),
|
|
|
|
}
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
tlsCfg := sc.TLS()
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
if tlsCfg != nil {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(tlsCfg.CertificateFile(), tlsCfg.KeyFile())
|
|
|
|
fatalOnErrDetails("could not read credentials from file", err)
|
|
|
|
|
|
|
|
serverOpts = append(serverOpts, grpc.Creds(creds))
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := grpc.NewServer(serverOpts...)
|
2021-05-14 09:11:49 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.onShutdown(func() {
|
|
|
|
stopGRPC("NeoFS Public API", srv, c.log)
|
|
|
|
})
|
2021-01-18 08:56:14 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
c.cfgGRPC.servers = append(c.cfgGRPC.servers, srv)
|
2021-01-18 08:56:14 +00:00
|
|
|
})
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
2020-08-22 11:03:45 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
func serveGRPC(c *cfg) {
|
2021-06-22 17:25:18 +00:00
|
|
|
for i := range c.cfgGRPC.servers {
|
|
|
|
c.wg.Add(1)
|
2020-12-29 15:50:41 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
srv := c.cfgGRPC.servers[i]
|
|
|
|
lis := c.cfgGRPC.listeners[i]
|
2020-08-22 11:03:45 +00:00
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := srv.Serve(lis); err != nil {
|
|
|
|
fmt.Println("gRPC server error", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-08-21 15:01:59 +00:00
|
|
|
}
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
func stopGRPC(name string, s *grpc.Server, l *logger.Logger) {
|
|
|
|
l = l.With(zap.String("name", name))
|
|
|
|
|
|
|
|
l.Info("stopping gRPC server...")
|
|
|
|
|
|
|
|
s.GracefulStop()
|
|
|
|
|
|
|
|
l.Info("gRPC server stopped successfully")
|
|
|
|
}
|