-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
59 lines (46 loc) · 1.69 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package component
import (
"math"
"github.com/TheThingsNetwork/go-utils/grpc/rpclog"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/mwitkow/go-grpc-middleware"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func (c *Component) ServerOptions() []grpc.ServerOption {
unaryLog := rpclog.UnaryServerInterceptor(c.Ctx)
unaryErr := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
iface, err := handler(ctx, req)
err = errors.BuildGRPCError(err)
return iface, err
}
streamLog := rpclog.StreamServerInterceptor(c.Ctx)
streamErr := func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, stream)
err = errors.BuildGRPCError(err)
return err
}
opts := []grpc.ServerOption{
grpc.MaxConcurrentStreams(math.MaxUint16),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryErr, unaryLog)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamErr, streamLog)),
}
if c.tlsConfig != nil {
opts = append(opts, grpc.Creds(credentials.NewTLS(c.tlsConfig)))
}
return opts
}
func init() {
// Disable gRPC tracing
// SEE: https://github.com/grpc/grpc-go/issues/695
grpc.EnableTracing = false
// Initialize TTN tracing
OnInitialize(func(c *Component) error {
trace.SetComponent(c.Identity.ServiceName, c.Identity.Id)
return nil
})
}