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-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) {
|
|
|
|
var err error
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2020-09-16 07:45:08 +00:00
|
|
|
c.cfgGRPC.listener, err = net.Listen("tcp", c.viper.GetString(cfgListenAddress))
|
2020-08-24 09:40:32 +00:00
|
|
|
fatalOnErr(err)
|
2020-08-22 15:20:47 +00:00
|
|
|
|
2021-05-14 09:11:49 +00:00
|
|
|
serverOpts := []grpc.ServerOption{
|
2021-05-12 08:00:46 +00:00
|
|
|
grpc.MaxSendMsgSize(maxMsgSize),
|
2021-05-14 09:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.cfgGRPC.tlsEnabled {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(c.cfgGRPC.tlsCertFile, c.cfgGRPC.tlsKeyFile)
|
2021-05-31 05:11:23 +00:00
|
|
|
fatalOnErrDetails("could not read credentials from file", err)
|
2021-05-14 09:11:49 +00:00
|
|
|
|
|
|
|
serverOpts = append(serverOpts, grpc.Creds(creds))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgGRPC.server = grpc.NewServer(serverOpts...)
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
c.onShutdown(func() {
|
|
|
|
stopGRPC("NeoFS Public API", c.cfgGRPC.server, c.log)
|
|
|
|
})
|
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) {
|
2020-12-29 15:50:41 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
|
2020-08-22 11:03:45 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
if err := c.cfgGRPC.server.Serve(c.cfgGRPC.listener); err != nil {
|
2020-08-22 11:03:45 +00:00
|
|
|
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")
|
|
|
|
}
|