forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gRPC.go
68 lines (53 loc) · 1.55 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
60
61
62
63
64
65
66
67
68
package gRPC
import (
"context"
"net"
"time"
"github.com/cosmos/cosmos-sdk/codec"
proto "github.com/maticnetwork/polyproto/heimdall"
tmLog "github.com/tendermint/tendermint/libs/log"
"google.golang.org/grpc"
)
const (
spanURL = "/bor/span/%v"
eventRecordList = "/clerk/event-record/list"
fetchCheckpointCount = "/checkpoints/count"
fetchCheckpoint = "/checkpoints/%s"
)
var logger tmLog.Logger
type HeimdallGRPCServer struct {
proto.UnimplementedHeimdallServer
cdc *codec.Codec
}
func SetupGRPCServer(shutDownCtx context.Context, cdc *codec.Codec, addr string, lggr tmLog.Logger) error {
logger = lggr
grpcServer := grpc.NewServer(withLoggingUnaryInterceptor())
proto.RegisterHeimdallServer(grpcServer,
&HeimdallGRPCServer{
cdc: cdc,
})
lis, err := net.Listen("tcp", addr)
if err != nil {
return err
}
go func() {
if err := grpcServer.Serve(lis); err != nil {
logger.Error("failed to serve grpc server", "err", err)
}
<-shutDownCtx.Done()
grpcServer.Stop()
lis.Close()
logger.Info("GRPC Server stopped", "addr", addr)
}()
logger.Info("GRPC Server started", "addr", addr)
return nil
}
func withLoggingUnaryInterceptor() grpc.ServerOption {
return grpc.UnaryInterceptor(loggingServerInterceptor)
}
func loggingServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
start := time.Now()
h, err := handler(ctx, req)
logger.Info("Request", "method", info.FullMethod, "duration", time.Since(start), "error", err)
return h, err
}