ensure cached client with updated storage conf

This commit is contained in:
Chris Lu 2021-09-01 01:27:45 -07:00
parent 8e125339d5
commit 3faaa6e360

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/remote_pb" "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
"github.com/golang/protobuf/proto"
"io" "io"
"strings" "strings"
"sync" "sync"
@ -76,9 +77,14 @@ type RemoteStorageClientMaker interface {
HasBucket() bool HasBucket() bool
} }
type CachedRemoteStorageClient struct {
*remote_pb.RemoteConf
RemoteStorageClient
}
var ( var (
RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker) RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
remoteStorageClients = make(map[string]RemoteStorageClient) remoteStorageClients = make(map[string]CachedRemoteStorageClient)
remoteStorageClientsLock sync.Mutex remoteStorageClientsLock sync.Mutex
) )
@ -107,8 +113,8 @@ func GetRemoteStorage(remoteConf *remote_pb.RemoteConf) (RemoteStorageClient, er
defer remoteStorageClientsLock.Unlock() defer remoteStorageClientsLock.Unlock()
existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name] existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
if found { if found && proto.Equal(existingRemoteStorageClient.RemoteConf, remoteConf) {
return existingRemoteStorageClient, nil return existingRemoteStorageClient.RemoteStorageClient, nil
} }
newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf) newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
@ -116,7 +122,10 @@ func GetRemoteStorage(remoteConf *remote_pb.RemoteConf) (RemoteStorageClient, er
return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err) return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
} }
remoteStorageClients[remoteConf.Name] = newRemoteStorageClient remoteStorageClients[remoteConf.Name] = CachedRemoteStorageClient{
RemoteConf: remoteConf,
RemoteStorageClient: newRemoteStorageClient,
}
return newRemoteStorageClient, nil return newRemoteStorageClient, nil
} }