aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2018-11-14 23:31:39 -0800
committerChris Lu <chris.lu@gmail.com>2018-11-14 23:31:39 -0800
commitd89774cb7f4105223fde3ef0a2fcd85d90524e9c (patch)
tree51956cc4a6ad4dc19b57f51c40643a2dde5c8e22
parent2e6a3c7b160e499d91e990d9c65dab9ff1fd6be7 (diff)
downloadseaweedfs-d89774cb7f4105223fde3ef0a2fcd85d90524e9c.tar.xz
seaweedfs-d89774cb7f4105223fde3ef0a2fcd85d90524e9c.zip
cache grpc client connections to improve performance
-rw-r--r--weed/filesys/wfs.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/weed/filesys/wfs.go b/weed/filesys/wfs.go
index eca74d1d6..7fb3e0141 100644
--- a/weed/filesys/wfs.go
+++ b/weed/filesys/wfs.go
@@ -11,6 +11,7 @@ import (
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/karlseguin/ccache"
+ "google.golang.org/grpc"
)
type Option struct {
@@ -33,6 +34,10 @@ type WFS struct {
handles []*FileHandle
pathToHandleIndex map[string]int
pathToHandleLock sync.Mutex
+
+ // cache grpc connections
+ grpcClients map[string]*grpc.ClientConn
+ grpcClientsLock sync.Mutex
}
func NewSeaweedFileSystem(option *Option) *WFS {
@@ -40,6 +45,7 @@ func NewSeaweedFileSystem(option *Option) *WFS {
option: option,
listDirectoryEntriesCache: ccache.New(ccache.Configure().MaxSize(int64(option.DirListingLimit) + 200).ItemsToPrune(100)),
pathToHandleIndex: make(map[string]int),
+ grpcClients: make(map[string]*grpc.ClientConn),
}
}
@@ -49,11 +55,22 @@ func (wfs *WFS) Root() (fs.Node, error) {
func (wfs *WFS) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
+ wfs.grpcClientsLock.Lock()
+
+ existingConnection, found := wfs.grpcClients[wfs.option.FilerGrpcAddress]
+ if found {
+ wfs.grpcClientsLock.Unlock()
+ client := filer_pb.NewSeaweedFilerClient(existingConnection)
+ return fn(client)
+ }
+
grpcConnection, err := util.GrpcDial(wfs.option.FilerGrpcAddress)
if err != nil {
return fmt.Errorf("fail to dial %s: %v", wfs.option.FilerGrpcAddress, err)
}
- defer grpcConnection.Close()
+
+ wfs.grpcClients[wfs.option.FilerGrpcAddress] = grpcConnection
+ wfs.grpcClientsLock.Unlock()
client := filer_pb.NewSeaweedFilerClient(grpcConnection)