Repository navigation
Expand file tree
/
Copy pathgrpc.go
More file actions
57 lines (47 loc) · 1.41 KB
/
Copy pathgrpc.go
File metadata and controls
57 lines (47 loc) · 1.41 KB
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
//go:generate go-bindata data/...
package main
import (
"crypto/tls"
"errors"
"net"
log "github.com/Sirupsen/logrus"
"github.com/larskluge/babl/bablmodule"
pb "github.com/larskluge/babl/protobuf"
pbm "github.com/larskluge/babl/protobuf/messages"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type server struct {
busy bool
}
func startGrpcServer(address string, module *bablmodule.Module) {
lis, err := net.Listen("tcp", address)
if err != nil {
log.WithFields(log.Fields{"error": err, "address": address}).Fatal("Failed to listen at port")
}
certPEMBlock, _ := Asset("data/server.pem")
keyPEMBlock, _ := Asset("data/server.key")
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
panic(err)
}
creds := credentials.NewServerTLSFromCert(&cert)
opts := []grpc.ServerOption{grpc.Creds(creds)}
opts = append(opts, grpc.MaxMsgSize(MaxGrpcMessageSize))
s := grpc.NewServer(opts...)
pb.RegisterBinaryServer((*module).GrpcServiceName(), s, &server{busy: false})
s.Serve(lis)
}
func (s *server) IO(ctx context.Context, in *pbm.BinRequest) (*pbm.BinReply, error) {
if s.busy {
return nil, errors.New("Busy calculating, please try again later")
} else {
s.busy = true
defer func() { s.busy = false }()
return IO(in, MaxGrpcMessageSize)
}
}
func (s *server) Ping(ctx context.Context, in *pbm.Empty) (*pbm.Pong, error) {
return Ping(in)
}