aboutsummaryrefslogtreecommitdiff
path: root/weed/operation/grpc_client.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2018-12-07 01:25:01 -0800
committerChris Lu <chris.lu@gmail.com>2018-12-07 01:25:01 -0800
commit29f1673d9766f11b256ca1c0d653aaa7d99e13aa (patch)
tree72c3967aa5338519effcd08c063e8d6f598fcdaa /weed/operation/grpc_client.go
parent6946c51430473739ae4819c30458ff7edd107bdd (diff)
downloadseaweedfs-29f1673d9766f11b256ca1c0d653aaa7d99e13aa.tar.xz
seaweedfs-29f1673d9766f11b256ca1c0d653aaa7d99e13aa.zip
refactoring
Diffstat (limited to 'weed/operation/grpc_client.go')
-rw-r--r--weed/operation/grpc_client.go44
1 files changed, 6 insertions, 38 deletions
diff --git a/weed/operation/grpc_client.go b/weed/operation/grpc_client.go
index b1d6a633e..300f78b58 100644
--- a/weed/operation/grpc_client.go
+++ b/weed/operation/grpc_client.go
@@ -25,27 +25,11 @@ func WithVolumeServerClient(volumeServer string, fn func(volume_server_pb.Volume
return err
}
- grpcClientsLock.Lock()
-
- existingConnection, found := grpcClients[grpcAddress]
- if found {
- grpcClientsLock.Unlock()
- client := volume_server_pb.NewVolumeServerClient(existingConnection)
+ return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
+ client := volume_server_pb.NewVolumeServerClient(grpcConnection)
return fn(client)
- }
-
- grpcConnection, err := util.GrpcDial(grpcAddress)
- if err != nil {
- grpcClientsLock.Unlock()
- return fmt.Errorf("fail to dial %s: %v", grpcAddress, err)
- }
-
- grpcClients[grpcAddress] = grpcConnection
- grpcClientsLock.Unlock()
-
- client := volume_server_pb.NewVolumeServerClient(grpcConnection)
+ }, grpcAddress)
- return fn(client)
}
func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err error) {
@@ -60,25 +44,9 @@ func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err err
func withMasterServerClient(masterServer string, fn func(masterClient master_pb.SeaweedClient) error) error {
- grpcClientsLock.Lock()
-
- existingConnection, found := grpcClients[masterServer]
- if found {
- grpcClientsLock.Unlock()
- client := master_pb.NewSeaweedClient(existingConnection)
+ return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
+ client := master_pb.NewSeaweedClient(grpcConnection)
return fn(client)
- }
-
- grpcConnection, err := util.GrpcDial(masterServer)
- if err != nil {
- grpcClientsLock.Unlock()
- return fmt.Errorf("fail to dial %s: %v", masterServer, err)
- }
-
- grpcClients[masterServer] = grpcConnection
- grpcClientsLock.Unlock()
-
- client := master_pb.NewSeaweedClient(grpcConnection)
+ }, masterServer)
- return fn(client)
}