You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

4 years ago
  1. package remote_storage
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "sync"
  6. )
  7. type VisitFunc func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error
  8. type RemoteStorageClient interface {
  9. Traverse(rootDir string, visitFn VisitFunc) error
  10. }
  11. type RemoteStorageClientMaker interface {
  12. Make(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error)
  13. }
  14. var (
  15. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  16. remoteStorageClients = make(map[string]RemoteStorageClient)
  17. remoteStorageClientsLock sync.Mutex
  18. )
  19. func makeRemoteStorageClient(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  20. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  21. if !found {
  22. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  23. }
  24. return maker.Make(remoteConf)
  25. }
  26. func GetRemoteStorage(remoteConf *filer_pb.RemoteConf) (RemoteStorageClient, error) {
  27. remoteStorageClientsLock.Lock()
  28. defer remoteStorageClientsLock.Unlock()
  29. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  30. if found {
  31. return existingRemoteStorageClient, nil
  32. }
  33. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  34. if err != nil {
  35. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  36. }
  37. remoteStorageClients[remoteConf.Name] = newRemoteStorageClient
  38. return newRemoteStorageClient, nil
  39. }