Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

cache grpc client connections to improve performance

Chris Lud89774cb7f41
diff --git a/weed/filesys/wfs.go b/weed/filesys/wfs.go
index eca74d1d6c..7fb3e0141f 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)