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.

52 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package operation
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "google.golang.org/grpc"
  12. )
  13. var (
  14. grpcClients = make(map[string]*grpc.ClientConn)
  15. grpcClientsLock sync.Mutex
  16. )
  17. func WithVolumeServerClient(volumeServer string, fn func(volume_server_pb.VolumeServerClient) error) error {
  18. grpcAddress, err := toVolumeServerGrpcAddress(volumeServer)
  19. if err != nil {
  20. return err
  21. }
  22. return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  23. client := volume_server_pb.NewVolumeServerClient(grpcConnection)
  24. return fn(client)
  25. }, grpcAddress)
  26. }
  27. func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err error) {
  28. sepIndex := strings.LastIndex(volumeServer, ":")
  29. port, err := strconv.Atoi(volumeServer[sepIndex+1:])
  30. if err != nil {
  31. glog.Errorf("failed to parse volume server address: %v", volumeServer)
  32. return "", err
  33. }
  34. return fmt.Sprintf("%s:%d", volumeServer[0:sepIndex], port+10000), nil
  35. }
  36. func withMasterServerClient(masterServer string, fn func(masterClient master_pb.SeaweedClient) error) error {
  37. return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  38. client := master_pb.NewSeaweedClient(grpcConnection)
  39. return fn(client)
  40. }, masterServer)
  41. }