-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
113 lines (95 loc) · 3.15 KB
/
conn.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
package relay_grpc
import (
"context"
"fmt"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
)
// GRPC dial options
const (
windowSize = 1024 * 1024 * 3 // 3 MB
bufferSize = 0 // to disallow batching data before writing
)
var DefaultKeepaliveParams = keepalive.ClientParameters{
Time: time.Minute, // send pings every minute if there is no activity
Timeout: 20 * time.Second, // wait 20 seconds for ping ack before considering the connection dead
PermitWithoutStream: true, // send pings even without active streams
}
func NewRelayConnection(host string) (RelayClient, error) {
dialOptions := []grpc.DialOption{
grpc.WithInitialConnWindowSize(windowSize),
grpc.WithWriteBufferSize(bufferSize),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(DefaultKeepaliveParams),
}
// Check initial connection for approval
conn, err := grpc.Dial(host, dialOptions...)
if err != nil {
return nil, err
}
if conn == nil {
newConn, err := grpc.Dial(host, dialOptions...)
if err != nil {
fmt.Println("failed to connect to grpc service with error", "error", err)
return nil, err
}
conn = newConn
}
return NewRelayClient(conn), nil
}
func NewConnection(host, authToken string, useGzipCompression bool) (chan *SubmitBlockRequest, error) {
dialOptions := []grpc.DialOption{
grpc.WithInitialConnWindowSize(windowSize),
grpc.WithWriteBufferSize(bufferSize),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(DefaultKeepaliveParams),
}
if useGzipCompression {
dialOptions = append(dialOptions, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
}
// Check initial connection for approval
conn, err := grpc.Dial(host, dialOptions...)
if err != nil {
return nil, err
}
bodyChan := make(chan *SubmitBlockRequest, 100)
go ConnectToGRPCService(host, authToken, &bodyChan, conn, dialOptions)
return bodyChan, err
}
func ConnectToGRPCService(host, authToken string, bodyChan *chan *SubmitBlockRequest, conn *grpc.ClientConn, dialOptions []grpc.DialOption) {
if conn == nil {
newConn, err := grpc.Dial(host, dialOptions...)
if err != nil {
fmt.Println("failed to connect to grpc service with error", "error", err)
return
}
conn = newConn
}
defer conn.Close()
defer func() {
if r := recover(); r != nil {
fmt.Println("grpc panicked but recovered!", "error", r)
go ConnectToGRPCService(host, authToken, bodyChan, nil, dialOptions)
} else {
fmt.Println("grpc closed without panic, restarting to be safe")
go ConnectToGRPCService(host, authToken, bodyChan, nil, dialOptions)
}
}()
ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", authToken)
client := NewRelayClient(conn)
for {
body := <-*bodyChan
go func() {
_, err := client.SubmitBlock(ctx, body)
if err != nil {
fmt.Println("failed to submit block over grpc with error", "error", err)
return
}
fmt.Println("successfully submitted block using grpc")
}()
}
}