-
Notifications
You must be signed in to change notification settings - Fork 24
/
grpc.go
37 lines (30 loc) · 875 Bytes
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package serve
import (
"net"
"github.com/powerman/structlog"
"google.golang.org/grpc"
"github.com/powerman/go-monolith-example/pkg/def"
"github.com/powerman/go-monolith-example/pkg/netx"
)
// GRPC starts gRPC server on addr, logged as service.
// It runs until failed or ctx.Done.
func GRPC(ctx Ctx, addr netx.Addr, srv *grpc.Server, service string) (err error) {
log := structlog.FromContext(ctx, nil).New(def.LogServer, service)
ln, err := net.Listen("tcp", addr.String())
if err != nil {
return err
}
log.Info("serve", def.LogHost, addr.Host(), def.LogPort, addr.Port())
errc := make(chan error, 1)
go func() { errc <- srv.Serve(ln) }()
select {
case err = <-errc:
case <-ctx.Done():
srv.GracefulStop() // It will not interrupt streaming.
}
if err != nil {
return log.Err("failed to serve", "err", err)
}
log.Info("shutdown")
return nil
}