mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-07-05 16:38:17 -04:00
cancel failed grpc connection
This commit is contained in:
parent
54fc21e537
commit
e00d80eac4
@ -36,6 +36,8 @@ func init() {
|
|||||||
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dialerCanceller func()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
globalDialerMap map[net.Destination]*grpc.ClientConn
|
globalDialerMap map[net.Destination]*grpc.ClientConn
|
||||||
globalDialerAccess sync.Mutex
|
globalDialerAccess sync.Mutex
|
||||||
@ -51,19 +53,20 @@ func dialgRPC(ctx context.Context, dest net.Destination, streamSettings *interne
|
|||||||
dialOption = grpc.WithTransportCredentials(credentials.NewTLS(config.GetTLSConfig()))
|
dialOption = grpc.WithTransportCredentials(credentials.NewTLS(config.GetTLSConfig()))
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := getGrpcClient(ctx, dest, dialOption)
|
conn, canceller, err := getGrpcClient(ctx, dest, dialOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, newError("Cannot dial grpc").Base(err)
|
return nil, newError("Cannot dial grpc").Base(err)
|
||||||
}
|
}
|
||||||
client := encoding.NewGunServiceClient(conn)
|
client := encoding.NewGunServiceClient(conn)
|
||||||
gunService, err := client.(encoding.GunServiceClientX).TunCustomName(ctx, grpcSettings.ServiceName)
|
gunService, err := client.(encoding.GunServiceClientX).TunCustomName(ctx, grpcSettings.ServiceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
canceller()
|
||||||
return nil, newError("Cannot dial grpc").Base(err)
|
return nil, newError("Cannot dial grpc").Base(err)
|
||||||
}
|
}
|
||||||
return encoding.NewGunConn(gunService, nil), nil
|
return encoding.NewGunConn(gunService, nil), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.DialOption) (*grpc.ClientConn, error) {
|
func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.DialOption) (*grpc.ClientConn, dialerCanceller, error) {
|
||||||
globalDialerAccess.Lock()
|
globalDialerAccess.Lock()
|
||||||
defer globalDialerAccess.Unlock()
|
defer globalDialerAccess.Unlock()
|
||||||
|
|
||||||
@ -71,9 +74,15 @@ func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.Di
|
|||||||
globalDialerMap = make(map[net.Destination]*grpc.ClientConn)
|
globalDialerMap = make(map[net.Destination]*grpc.ClientConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canceller := func() {
|
||||||
|
globalDialerAccess.Lock()
|
||||||
|
defer globalDialerAccess.Unlock()
|
||||||
|
delete(globalDialerMap, dest)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Should support chain proxy to the same destination
|
// TODO Should support chain proxy to the same destination
|
||||||
if client, found := globalDialerMap[dest]; found && client.GetState() != connectivity.Shutdown {
|
if client, found := globalDialerMap[dest]; found && client.GetState() != connectivity.Shutdown {
|
||||||
return client, nil
|
return client, canceller, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := grpc.Dial(
|
conn, err := grpc.Dial(
|
||||||
@ -106,5 +115,5 @@ func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.Di
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
globalDialerMap[dest] = conn
|
globalDialerMap[dest] = conn
|
||||||
return conn, err
|
return conn, canceller, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user