/
subscribe_grpc.go
61 lines (51 loc) · 1.54 KB
/
subscribe_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
package proxy
import (
"context"
"fmt"
"time"
"github.com/centrifugal/centrifugo/v5/internal/proxyproto"
"google.golang.org/grpc"
)
// GRPCSubscribeProxy ...
type GRPCSubscribeProxy struct {
config Config
client proxyproto.CentrifugoProxyClient
}
var _ SubscribeProxy = (*GRPCSubscribeProxy)(nil)
// NewGRPCSubscribeProxy ...
func NewGRPCSubscribeProxy(p Config) (*GRPCSubscribeProxy, error) {
host, err := getGrpcHost(p.Endpoint)
if err != nil {
return nil, fmt.Errorf("error getting grpc host: %v", err)
}
dialOpts, err := getDialOpts(p)
if err != nil {
return nil, fmt.Errorf("error creating GRPC dial options: %v", err)
}
conn, err := grpc.NewClient(host, dialOpts...)
if err != nil {
return nil, fmt.Errorf("error connecting to GRPC proxy server: %v", err)
}
return &GRPCSubscribeProxy{
config: p,
client: proxyproto.NewCentrifugoProxyClient(conn),
}, nil
}
// ProxySubscribe proxies Subscribe to application backend.
func (p *GRPCSubscribeProxy) ProxySubscribe(ctx context.Context, req *proxyproto.SubscribeRequest) (*proxyproto.SubscribeResponse, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(p.config.Timeout))
defer cancel()
return p.client.Subscribe(grpcRequestContext(ctx, p.config), req, grpc.ForceCodec(grpcCodec))
}
// Protocol ...
func (p *GRPCSubscribeProxy) Protocol() string {
return "grpc"
}
// UseBase64 ...
func (p *GRPCSubscribeProxy) UseBase64() bool {
return p.config.BinaryEncoding
}
// IncludeMeta ...
func (p *GRPCSubscribeProxy) IncludeMeta() bool {
return p.config.IncludeConnectionMeta
}