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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package sidechannel
import (
"bytes"
"context"
"io"
"math/rand"
"net"
"sync"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/backchannel"
"gitlab.com/gitlab-org/gitaly/v14/internal/listenmux"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
func TestSidechannel(t *testing.T) {
const blobSize = 1024 * 1024
in := make([]byte, blobSize)
_, err := rand.Read(in)
require.NoError(t, err)
var out []byte
require.NotEqual(t, in, out)
addr := startServer(
t,
func(context context.Context, request *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
conn, err := OpenSidechannel(context)
if err != nil {
return nil, err
}
defer conn.Close()
if _, err = io.CopyN(conn, conn, blobSize); err != nil {
return nil, err
}
return &healthpb.HealthCheckResponse{}, conn.Close()
},
)
conn, registry := dial(t, addr)
err = call(
context.Background(), conn, registry,
func(conn net.Conn) error {
errC := make(chan error, 1)
go func() {
var err error
out, err = io.ReadAll(conn)
errC <- err
}()
_, err = io.Copy(conn, bytes.NewReader(in))
require.NoError(t, err)
require.NoError(t, <-errC)
return nil
},
)
require.NoError(t, err)
require.Equal(t, in, out, "byte stream works")
}
// Conduct multiple requests with sidechannel included on the same grpc
// connection.
func TestSidechannelConcurrency(t *testing.T) {
const concurrency = 10
const blobSize = 1024 * 1024
ins := make([][]byte, concurrency)
for i := 0; i < concurrency; i++ {
ins[i] = make([]byte, blobSize)
_, err := rand.Read(ins[i])
require.NoError(t, err)
}
outs := make([][]byte, concurrency)
for i := 0; i < concurrency; i++ {
require.NotEqual(t, ins[i], outs[i])
}
addr := startServer(
t,
func(context context.Context, request *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
conn, err := OpenSidechannel(context)
if err != nil {
return nil, err
}
defer conn.Close()
if _, err = io.CopyN(conn, conn, blobSize); err != nil {
return nil, err
}
return &healthpb.HealthCheckResponse{}, conn.Close()
},
)
conn, registry := dial(t, addr)
errors := make(chan error, concurrency)
wg := sync.WaitGroup{}
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
err := call(
context.Background(), conn, registry,
func(conn net.Conn) error {
errC := make(chan error, 1)
go func() {
var err error
outs[i], err = io.ReadAll(conn)
errC <- err
}()
if _, err := io.Copy(conn, bytes.NewReader(ins[i])); err != nil {
return err
}
if err := <-errC; err != nil {
return err
}
return nil
},
)
errors <- err
}(i)
}
wg.Wait()
for i := 0; i < concurrency; i++ {
require.Equal(t, ins[i], outs[i], "byte stream works")
require.NoError(t, <-errors)
}
}
func startServer(t *testing.T, th testHandler, opts ...grpc.ServerOption) string {
t.Helper()
logger := logrus.NewEntry(logrus.New())
lm := listenmux.New(insecure.NewCredentials())
lm.Register(backchannel.NewServerHandshaker(logger, backchannel.NewRegistry(), nil))
opts = append(opts, grpc.Creds(lm))
s := grpc.NewServer(opts...)
t.Cleanup(func() { s.Stop() })
handler := &server{testHandler: th}
healthpb.RegisterHealthServer(s, handler)
lis, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
t.Cleanup(func() { lis.Close() })
go func() { s.Serve(lis) }()
return lis.Addr().String()
}
func dial(t *testing.T, addr string) (*grpc.ClientConn, *Registry) {
registry := NewRegistry()
logger := logrus.NewEntry(logrus.New())
factory := func() backchannel.Server {
lm := listenmux.New(insecure.NewCredentials())
lm.Register(NewServerHandshaker(registry))
return grpc.NewServer(grpc.Creds(lm))
}
clientHandshaker := backchannel.NewClientHandshaker(logger, factory)
dialOpt := grpc.WithTransportCredentials(clientHandshaker.ClientHandshake(insecure.NewCredentials()))
conn, err := grpc.Dial(addr, dialOpt)
require.NoError(t, err)
t.Cleanup(func() { conn.Close() })
return conn, registry
}
func call(ctx context.Context, conn *grpc.ClientConn, registry *Registry, handler func(net.Conn) error) error {
client := healthpb.NewHealthClient(conn)
ctxOut, waiter := RegisterSidechannel(ctx, registry, handler)
defer waiter.Close()
if _, err := client.Check(ctxOut, &healthpb.HealthCheckRequest{}); err != nil {
return err
}
if err := waiter.Wait(); err != nil {
return err
}
return nil
}
type testHandler func(context.Context, *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error)
type server struct {
healthpb.UnimplementedHealthServer
testHandler
}
func (s *server) Check(context context.Context, request *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
return s.testHandler(context, request)
}
|