forked from anycable/anycable-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.go
176 lines (132 loc) · 3.73 KB
/
remote.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"net/http"
"time"
"github.com/anycable/anycable-go/pool"
pb "github.com/anycable/anycable-go/protos"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
retryInterval = 500
invokeTimeout = 3000
)
type Remote struct {
pool pool.Pool
}
var rpc = Remote{}
func (rpc *Remote) Init(host string) {
factory := func() (*grpc.ClientConn, error) {
return grpc.Dial(host, grpc.WithInsecure())
}
p, err := pool.NewChannelPool(5, 50, factory)
if err != nil {
log.Criticalf("failed to create pool: %v", err)
}
rpc.pool = p
}
func (rpc *Remote) GetConn() pool.PoolConn {
pc, err := rpc.pool.Get()
if err != nil {
log.Criticalf("failed to retrieve connection: %v", err)
}
return pc
}
func (rpc *Remote) Close() {
rpc.pool.Close()
}
func (rpc *Remote) VerifyConnection(r *http.Request) *pb.ConnectionResponse {
conn := rpc.GetConn()
defer conn.Close()
client := pb.NewRPCClient(conn.Conn)
op := func() (interface{}, error) {
return client.Connect(context.Background(), &pb.ConnectionRequest{Path: r.URL.String(), Headers: GetHeaders(r)})
}
response, err := retry(op)
if err != nil {
log.Errorf("RPC Error: %v", err)
return nil
}
if r, ok := response.(*pb.ConnectionResponse); ok {
return r
} else {
return nil
}
}
func (rpc *Remote) Subscribe(connId string, channelId string) *pb.CommandResponse {
conn := rpc.GetConn()
defer conn.Close()
client := pb.NewRPCClient(conn.Conn)
op := func() (interface{}, error) {
return client.Command(context.Background(), &pb.CommandMessage{Command: "subscribe", Identifier: channelId, ConnectionIdentifiers: connId})
}
response, err := retry(op)
return ParseCommandResponse(response, err)
}
func (rpc *Remote) Unsubscribe(connId string, channelId string) *pb.CommandResponse {
conn := rpc.GetConn()
defer conn.Close()
client := pb.NewRPCClient(conn.Conn)
op := func() (interface{}, error) {
return client.Command(context.Background(), &pb.CommandMessage{Command: "unsubscribe", Identifier: channelId, ConnectionIdentifiers: connId})
}
response, err := retry(op)
return ParseCommandResponse(response, err)
}
func (rpc *Remote) Perform(connId string, channelId string, data string) *pb.CommandResponse {
conn := rpc.GetConn()
defer conn.Close()
client := pb.NewRPCClient(conn.Conn)
op := func() (interface{}, error) {
return client.Command(context.Background(), &pb.CommandMessage{Command: "message", Identifier: channelId, ConnectionIdentifiers: connId, Data: data})
}
response, err := retry(op)
return ParseCommandResponse(response, err)
}
func (rpc *Remote) Disconnect(connId string, subscriptions []string) *pb.DisconnectResponse {
conn := rpc.GetConn()
defer conn.Close()
client := pb.NewRPCClient(conn.Conn)
op := func() (interface{}, error) {
return client.Disconnect(context.Background(), &pb.DisconnectRequest{Identifiers: connId, Subscriptions: subscriptions})
}
response, err := retry(op)
if err != nil {
log.Errorf("RPC Error: %v", err)
return nil
}
if r, ok := response.(*pb.DisconnectResponse); ok {
return r
} else {
return nil
}
}
func retry(callback func() (interface{}, error)) (res interface{}, err error) {
attempts := invokeTimeout / retryInterval
for i := 0; ; i++ {
res, err = callback()
if err == nil {
return res, nil
}
if i >= (attempts - 1) {
return nil, err
}
time.Sleep(retryInterval * time.Millisecond)
}
}
func ParseCommandResponse(response interface{}, err error) *pb.CommandResponse {
if err != nil {
log.Errorf("RPC Error: %v", err)
return nil
}
if r, ok := response.(*pb.CommandResponse); ok {
return r
} else {
return nil
}
}
func GetHeaders(r *http.Request) map[string]string {
res := make(map[string]string)
res["Cookie"] = r.Header.Get("Cookie")
return res
}