mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-19 22:57:56 +08:00
refactoring
This commit is contained in:
@@ -76,43 +76,33 @@ func GrpcDial(ctx context.Context, address string, opts ...grpc.DialOption) (*gr
|
|||||||
return grpc.DialContext(ctx, address, options...)
|
return grpc.DialContext(ctx, address, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithCachedGrpcClient(fn func(*grpc.ClientConn) error, address string, opts ...grpc.DialOption) error {
|
func getOrCreateConnection(address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||||
|
|
||||||
grpcClientsLock.Lock()
|
grpcClientsLock.Lock()
|
||||||
|
defer grpcClientsLock.Unlock()
|
||||||
|
|
||||||
existingConnection, found := grpcClients[address]
|
existingConnection, found := grpcClients[address]
|
||||||
if found {
|
if found {
|
||||||
grpcClientsLock.Unlock()
|
return existingConnection, nil
|
||||||
err := fn(existingConnection)
|
|
||||||
if err != nil {
|
|
||||||
grpcClientsLock.Lock()
|
|
||||||
// delete(grpcClients, address)
|
|
||||||
grpcClientsLock.Unlock()
|
|
||||||
// println("closing existing connection to", existingConnection.Target())
|
|
||||||
// existingConnection.Close()
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
grpcConnection, err := GrpcDial(context.Background(), address, opts...)
|
grpcConnection, err := GrpcDial(context.Background(), address, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
grpcClientsLock.Unlock()
|
return nil, fmt.Errorf("fail to dial %s: %v", address, err)
|
||||||
return fmt.Errorf("fail to dial %s: %v", address, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
grpcClients[address] = grpcConnection
|
grpcClients[address] = grpcConnection
|
||||||
grpcClientsLock.Unlock()
|
|
||||||
|
|
||||||
err = fn(grpcConnection)
|
return grpcConnection, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCachedGrpcClient(fn func(*grpc.ClientConn) error, address string, opts ...grpc.DialOption) error {
|
||||||
|
|
||||||
|
grpcConnection, err := getOrCreateConnection(address, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
grpcClientsLock.Lock()
|
return fmt.Errorf("getOrCreateConnection %s: %v", address, err)
|
||||||
// delete(grpcClients, address)
|
|
||||||
grpcClientsLock.Unlock()
|
|
||||||
// println("closing created new connection to", grpcConnection.Target())
|
|
||||||
// grpcConnection.Close()
|
|
||||||
}
|
}
|
||||||
|
return fn(grpcConnection)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseServerToGrpcAddress(server string) (serverGrpcAddress string, err error) {
|
func ParseServerToGrpcAddress(server string) (serverGrpcAddress string, err error) {
|
||||||
|
Reference in New Issue
Block a user