Skip to content

Commit 695f991

Browse files
authored
Revert recent API change to TunnelServiceHandler (#13)
Let's not make any API changes until after we get flow control implemented and released.
1 parent f912134 commit 695f991

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

internal/cmd/tunneltestsvr/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
return vals[0]
3434
},
3535
})
36-
tunnelpb.RegisterTunnelServiceServer(svr, tunnelSvc)
36+
tunnelpb.RegisterTunnelServiceServer(svr, tunnelSvc.Service())
3737
gen.RegisterTunnelTestServiceServer(svr, &tunnelTester{tunnelSvc: tunnelSvc})
3838

3939
// Over the tunnel, we just expose this simple test service

service.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
//
2727
// See NewTunnelServiceHandler.
2828
type TunnelServiceHandler struct {
29-
tunnelpb.UnimplementedTunnelServiceServer
30-
3129
handlers grpchan.HandlerMap
3230
noReverseTunnels bool
3331
onReverseTunnelConnect func(TunnelChannel)
@@ -96,6 +94,14 @@ func (s *TunnelServiceHandler) RegisterService(desc *grpc.ServiceDesc, srv inter
9694
s.handlers.RegisterService(desc, srv)
9795
}
9896

97+
// Service returns the actual tunnel service implementation to register with a
98+
// [grpc.ServiceRegistrar].
99+
func (s *TunnelServiceHandler) Service() tunnelpb.TunnelServiceServer {
100+
return &tunnelServiceHandler{
101+
h: s,
102+
}
103+
}
104+
99105
// InitiateShutdown starts the graceful shutdown process and returns
100106
// immediately. This should be called when the server wants to shut down. This
101107
// complements the normal process initiated by calling the GracefulStop method
@@ -107,10 +113,10 @@ func (s *TunnelServiceHandler) InitiateShutdown() {
107113
s.stopping.Store(true)
108114
}
109115

110-
// OpenTunnel creates a forward tunnel from the RPC client to this server. Any
116+
// openTunnel creates a forward tunnel from the RPC client to this server. Any
111117
// services registered with this handler will be accessible to RPCs issued over
112118
// the tunnel.
113-
func (s *TunnelServiceHandler) OpenTunnel(stream tunnelpb.TunnelService_OpenTunnelServer) error {
119+
func (s *TunnelServiceHandler) openTunnel(stream tunnelpb.TunnelService_OpenTunnelServer) error {
114120
if len(s.handlers) == 0 {
115121
return status.Error(codes.Unimplemented, "forward tunnels not supported")
116122
}
@@ -126,12 +132,12 @@ func (s *TunnelServiceHandler) OpenTunnel(stream tunnelpb.TunnelService_OpenTunn
126132
return serveTunnel(stream, md, s.handlers, s.stopping.Load)
127133
}
128134

129-
// OpenReverseTunnel creates a reverse tunnel from this server to the RPC client.
135+
// openReverseTunnel creates a reverse tunnel from this server to the RPC client.
130136
// This handler can be used as an RPC client connection, via AsChannel and
131137
// KeyAsChannel, to send RPCs to the client on the other end of the reverse
132138
// tunnel. The RPC client acts as the server and uses NewReverseTunnelServer to
133139
// create a server and register exposed RPC services with it.
134-
func (s *TunnelServiceHandler) OpenReverseTunnel(stream tunnelpb.TunnelService_OpenReverseTunnelServer) error {
140+
func (s *TunnelServiceHandler) openReverseTunnel(stream tunnelpb.TunnelService_OpenReverseTunnelServer) error {
135141
if s.noReverseTunnels {
136142
return status.Error(codes.Unimplemented, "reverse tunnels not supported")
137143
}
@@ -183,6 +189,19 @@ func (s *TunnelServiceHandler) unregister(ch *tunnelChannel) {
183189
}
184190
}
185191

192+
type tunnelServiceHandler struct {
193+
tunnelpb.UnimplementedTunnelServiceServer
194+
h *TunnelServiceHandler
195+
}
196+
197+
func (s *tunnelServiceHandler) OpenTunnel(stream tunnelpb.TunnelService_OpenTunnelServer) error {
198+
return s.h.openTunnel(stream)
199+
}
200+
201+
func (s *tunnelServiceHandler) OpenReverseTunnel(stream tunnelpb.TunnelService_OpenReverseTunnelServer) error {
202+
return s.h.openReverseTunnel(stream)
203+
}
204+
186205
type reverseChannels struct {
187206
mu sync.Mutex
188207
avail chan struct{}

tunnel_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func TestTunnelServiceHandler(t *testing.T) {
3737
grpchantesting.RegisterTestServiceServer(ts, &svr)
3838
// recursive: tunnels can be run on top of tunnels
3939
// (not realistic, but fun exercise to verify soundness of protocol)
40-
tunnelpb.RegisterTunnelServiceServer(ts, ts)
40+
tunnelpb.RegisterTunnelServiceServer(ts, ts.Service())
4141

4242
l, err := net.Listen("tcp", "127.0.0.1:0")
4343
require.NoError(t, err, "failed to listen")
4444
gs := grpc.NewServer()
45-
tunnelpb.RegisterTunnelServiceServer(gs, ts)
45+
tunnelpb.RegisterTunnelServiceServer(gs, ts.Service())
4646
serveDone := make(chan struct{})
4747
go func() {
4848
defer close(serveDone)
@@ -104,7 +104,7 @@ func runTests(ctx context.Context, t *testing.T, nested bool, cli tunnelpb.Tunne
104104
revSvr := NewReverseTunnelServer(cli)
105105
if !nested {
106106
// we need this to run the nested/recursive tunnel test
107-
tunnelpb.RegisterTunnelServiceServer(revSvr, ts)
107+
tunnelpb.RegisterTunnelServiceServer(revSvr, ts.Service())
108108
}
109109
grpchantesting.RegisterTestServiceServer(revSvr, testSvr)
110110
serveDone := make(chan struct{})
@@ -156,7 +156,7 @@ func TestTunnelServiceHandler_Concurrency(t *testing.T) {
156156
l, err := net.Listen("tcp", "127.0.0.1:0")
157157
require.NoError(t, err, "failed to listen")
158158
gs := grpc.NewServer()
159-
tunnelpb.RegisterTunnelServiceServer(gs, ts)
159+
tunnelpb.RegisterTunnelServiceServer(gs, ts.Service())
160160
serveDone := make(chan struct{})
161161
go func() {
162162
defer close(serveDone)

0 commit comments

Comments
 (0)