@@ -51,9 +51,11 @@ type grpcTunnel struct {
51
51
connsLock sync.RWMutex
52
52
}
53
53
54
- // CreateGrpcTunnel creates a Tunnel to dial to a remote server through a
54
+ // CreateSingleUseGrpcTunnel creates a Tunnel to dial to a remote server through a
55
55
// gRPC based proxy service.
56
- func CreateGrpcTunnel (address string , opts ... grpc.DialOption ) (Tunnel , error ) {
56
+ // Currently, a single tunnel supports a single connection, and the tunnel is closed when the connection is terminated
57
+ // The Dial() method of the returned tunnel should only be called once
58
+ func CreateSingleUseGrpcTunnel (address string , opts ... grpc.DialOption ) (Tunnel , error ) {
57
59
c , err := grpc .Dial (address , opts ... )
58
60
if err != nil {
59
61
return nil , err
@@ -72,12 +74,14 @@ func CreateGrpcTunnel(address string, opts ...grpc.DialOption) (Tunnel, error) {
72
74
conns : make (map [int64 ]* conn ),
73
75
}
74
76
75
- go tunnel .serve ()
77
+ go tunnel .serve (c )
76
78
77
79
return tunnel , nil
78
80
}
79
81
80
- func (t * grpcTunnel ) serve () {
82
+ func (t * grpcTunnel ) serve (c * grpc.ClientConn ) {
83
+ defer c .Close ()
84
+
81
85
for {
82
86
pkt , err := t .stream .Recv ()
83
87
if err == io .EOF {
@@ -130,9 +134,9 @@ func (t *grpcTunnel) serve() {
130
134
t .connsLock .Lock ()
131
135
delete (t .conns , resp .ConnectID )
132
136
t .connsLock .Unlock ()
133
- } else {
134
- klog .Warningf ("connection id %d not recognized" , resp .ConnectID )
137
+ return
135
138
}
139
+ klog .Warningf ("connection id %d not recognized" , resp .ConnectID )
136
140
}
137
141
}
138
142
}
0 commit comments