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"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2020-10-14 20:35:48 +00:00
|
|
|
"google.golang.org/grpc/reflection"
|
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
|
|
|
|
2020-10-02 08:01:54 +00:00
|
|
|
c.cfgGRPC.server = grpc.NewServer(
|
|
|
|
grpc.MaxSendMsgSize(c.viper.GetInt(cfgMaxMsgSize)),
|
|
|
|
)
|
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-08-22 11:03:45 +00:00
|
|
|
go func() {
|
|
|
|
c.wg.Add(1)
|
|
|
|
defer func() {
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
|
|
|
|
2020-10-14 20:35:48 +00:00
|
|
|
// read more about reflect service at
|
|
|
|
// https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md
|
|
|
|
if c.cfgGRPC.enableReflectService {
|
|
|
|
reflection.Register(c.cfgGRPC.server)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|