@@ -51,9 +51,11 @@ type grpcTunnel struct {
5151 connsLock sync.RWMutex
5252}
5353
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
5555// 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 ) {
5759 c , err := grpc .Dial (address , opts ... )
5860 if err != nil {
5961 return nil , err
@@ -72,12 +74,14 @@ func CreateGrpcTunnel(address string, opts ...grpc.DialOption) (Tunnel, error) {
7274 conns : make (map [int64 ]* conn ),
7375 }
7476
75- go tunnel .serve ()
77+ go tunnel .serve (c )
7678
7779 return tunnel , nil
7880}
7981
80- func (t * grpcTunnel ) serve () {
82+ func (t * grpcTunnel ) serve (c * grpc.ClientConn ) {
83+ defer c .Close ()
84+
8185 for {
8286 pkt , err := t .stream .Recv ()
8387 if err == io .EOF {
@@ -130,9 +134,9 @@ func (t *grpcTunnel) serve() {
130134 t .connsLock .Lock ()
131135 delete (t .conns , resp .ConnectID )
132136 t .connsLock .Unlock ()
133- } else {
134- klog .Warningf ("connection id %d not recognized" , resp .ConnectID )
137+ return
135138 }
139+ klog .Warningf ("connection id %d not recognized" , resp .ConnectID )
136140 }
137141 }
138142}
0 commit comments